Php: How to Capture Browser Window Screen with PHP

PHP: How to capture browser window screen with php?

There is imagegrabscreen() and imagegrabwindow(), which would allow you to programmatically create screenshots from a browser running on the same machine via COM (Win only though). See the comments in the manual for how to omit the browser's chrome. With DCOM enabled, this would also work with remote windows machines that have been setup to allow access through DCOM.

On a sidenote for those that said PHP does not know about the browser, I'd suggest a look at get_browser() in the PHP manual. It's not much, but hey, it's not nothing.

How do I get screen width (or windows width) to PHP ASAP?

While the answer of @avcajaraville is very useful in general, it is the comment of @Paflow that helped me find the solution. No JavaScript involved.

HTML

<?php
...
while ($row = mysqli_fetch_array($result)) {
$mobile_image_path = substr($row['image'], 0, -4).".mob".substr($row['image'], -4);
$image_id = "banner-image-" . $row['id_banner']; ?>

<style>
#<?=$image_id?> {
background-image: url('<?=$row['image']?>');
}
@media (max-width: 640px) {
#<?=$image_id?> {
background-image: url('<?=$mobile_image_path?>');
}
}
</style>
<div class="image" id="<?=$image_id?>"> ... </div>

I first used <style type="text/css" scoped>, but then I read scoped is deprecated and using just <style> inside of <body> is allowed.

Not the fanciest way, but it works!

display browser window width using PHP

I don't think you need to use ajax in this case, the 'outer' (?) page needs to receive the data, not the 'inner' ajax request.

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>

<?php
if (!isset($_POST["window_width"]) && !isset($_SESSION['window_width'])) {
?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="window_width_form" style="display:none;">
<input name="window_width" id="window_width" value="" />
</form>
<script type="text/javascript">
$(function(){
$('#window_width').val($(window).width());
$('#window_width_form').submit();
});
</script>
</body>
<?php
}
elseif (isset($_POST['window_width']) && !isset($_SESSION['window_width']))
{
$_SESSION['window_width'] = $_POST['window_width'];
exit(header("Location: {$_SERVER['PHP_SELF']}\r\n"));
}

if(!isset($_POST["window_width"]) && !isset($_SESSION['window_width'])) {
echo "not set";
}

?>
</html>

How to capture HTML generated by browser with PHP?

you can't capture it since javascript is processed by the client browser, not the server,

a way to do what you want can be to insert javascript code in page to send the entire html to the server:

if you use jquery you can do it with something like this:

var htmlCode = $("body").html();
$.post("recorder.php", { html: htmlCode } );

PHP Using imagegrabscreen

Keep in mind that imagaegrabscreen is Windows-only. If you have multiple displays set up, this function will only grab the primary display. Also, for this to work, your Apache service must be set to Allow service to interact with desktop otherwise you will just get a blank image.

This discussion covers the use of imagegrabscreen pretty well: Getting imagegrabscreen to work

There are a lot of other discussions about saving webpages as images, too - here are a few:

  • Website screenshots
  • Web Page Screenshots with PHP?
  • How can I generate a screenshot of a webpage using a server-side script?
  • PHP: How to capture browser window screen with php?
  • What is the best way to create a web page thumbnail?
  • Screenshot of current page using PHP
  • shell tool which renders web site including javascript
  • In any languages, Can I capture a webpage and save it image file? (no install, no activeX)


Related Topics



Leave a reply



Submit