Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Thursday, May 31, 2012
Website screenshots using PHP
Is there any way of taking a screenshot of a website in PHP, then saving it to a file?
Sure you can, but you'll need to render the page with something. If you really want to only use php, i suggest you HTMLTOPS, wich render the page and output it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and dont support all the CSS).
Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whaterver.. Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine. You have to install it on your server, as well..
UPDATE Now, with new HTML5 and JS feature, is also possible to render the page into a canvas object using Javascript. Here a nice library to do that: Html2Canvas and here an implementatio by the same author to get a feedback like G+. Once you have rendered the dom into the canvas, you can then send to the server via ajax and save it as a jpg.
EDIT: You can use the imagemagick tool for transforming pdf to png. My version of wkhtmltopdf does not support images. E.g. convert html.pdf -append html.png.
EDIT: This small shell script gives a simple / but working usage example on linux with php5-cli and the tools mentioned above.
EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly
It's in Python, but going over the documentation and code you can see exactly how this is done. If you can run python, then it's a ready-made solution for you:
http://browsershots.org/
Note that everything can run on one machine for one platform, or one machine with virtual machines running the other platforms.
Free, open source, scroll to bottom of page for links to documentation, source code, and other information.
Not directly. Software such as Selenium have features like this and can be controlled by PHP but have other dependencys (such as running their java-based server on the computer with the browser you want to screenshot)
I'm on Windows so I was able to use the imagegrabwindow function after reading the tip on here from stephan. I added in cropping (to get rid of the Browser header, scroll bars, etc.) and resizing to get a final image. Here's my code. Hope that helps someone.
I used bluga. The api allows you to take 100 snapshots a month without paying, but sometimes it uses more than 1 credit for a single page. I just finished upgrading a drupal module, Bluga WebThumbs to drupal 7 which allows you to print a thumbnail in a template or input filter.
The main advantage to using this api is that it allows you to specify browser dimensions in case you use adaptive css, so I am using it to get renderings for the mobile and tablet layout as well as the regular one.
There are api clients for the following languages:
PHP, Python, Ruby, Java, .Net C#, Perl and Bash (the shell script looks like it requires perl)
Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html
Try GrabzIt they have a great API and thier service is very fast. I use it on one of my personal websites.
Also because they use a callback when the screenshot is finished you don't have to continously poll them like you do for thumbilizer.
They have simple instructions on their website, to request a screenshot be taken:
include("GrabzItClient.class.php");
//Create the GrabzItClient class //Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account! $grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET"); //Take the picture the method will return the unique identifier assigned to this task $id = $grabzIt->TakePicture("http://www.google.com", "http://www.example.com/GrabzItHandler.php");
Then to save that screenshot create a PHP page to accept the screenshot result:
Sure you can, but you'll need to render the page with something.
ReplyDeleteIf you really want to only use php, i suggest you HTMLTOPS, wich render the page and output it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and dont support all the CSS).
Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whaterver..
Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine.
You have to install it on your server, as well..
UPDATE Now, with new HTML5 and JS feature, is also possible to render the page into a canvas object using Javascript. Here a nice library to do that: Html2Canvas and here an implementatio by the same author to get a feedback like G+.
Once you have rendered the dom into the canvas, you can then send to the server via ajax and save it as a jpg.
EDIT: You can use the imagemagick tool for transforming pdf to png. My version of wkhtmltopdf does not support images. E.g. convert html.pdf -append html.png.
EDIT: This small shell script gives a simple / but working usage example on linux with php5-cli and the tools mentioned above.
EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly
Since PHP 5.2.2 it is possible, to capture a website with PHP solely!
ReplyDeleteimagegrabscreen — Captures the whole screen
<?php
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>
imagegrabwindow - Grabs a window or its client area using a windows handle (HWND property in COM instance)
<?php
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->HWND;
$Browser->Visible = true;
$Browser->Fullscreen = true;
$Browser->Navigate('http://www.stackoverflow.com');
while($Browser->Busy){
com_message_pump(4000);
}
$img = imagegrabwindow($Browserhandle, 0);
$Browser->Quit();
imagepng($img, 'screenshot.png');
?>
Edit: Note, these functions are available on Windows systems ONLY!
This ought to be good for you:
ReplyDeletehttp://code.google.com/p/wkhtmltopdf/
Make sure you download the wkhtmltoimage distribution!
It's in Python, but going over the documentation and code you can see exactly how this is done. If you can run python, then it's a ready-made solution for you:
ReplyDeletehttp://browsershots.org/
Note that everything can run on one machine for one platform, or one machine with virtual machines running the other platforms.
Free, open source, scroll to bottom of page for links to documentation, source code, and other information.
Yes. You will need some things tho:
ReplyDeleteSee khtmld(aemon) on *nx.
See Url2Jpg for Windows but since it is dotNet app you should also chek Url2Bmp
Both are console tools that u can utilise from your web app to get the screenshot.
There are also web services that offer it. Check this out for example.
Edit:
This link is useful to.
A website has to be rendered. PHP does not have website (HTML, CSS, JavaScript, etc) rendering capabilities. So no.
ReplyDeleteNot directly. Software such as Selenium have features like this and can be controlled by PHP but have other dependencys (such as running their java-based server on the computer with the browser you want to screenshot)
ReplyDeleteI've been digging for something that can handle this and I came across this simple PHP class that's absolutely amazing!
ReplyDeletehttps://github.com/mptre/thumbalizr
I'm on Windows so I was able to use the imagegrabwindow function after reading the tip on here from stephan. I added in cropping (to get rid of the Browser header, scroll bars, etc.) and resizing to get a final image. Here's my code. Hope that helps someone.
ReplyDeleteShrink the web has always been what I've used. I think they have a nice API.
ReplyDeleteTry this:
ReplyDeletehttp://www.websnapr.com/
webkit2html works on Mac OS X and Linux, is quite simple to install and to use. See this tutorial.
ReplyDeleteFor Windows, you can go with CutyCapt, which has similar functionality.
I modified webshot, it can be run from PHP with exec https://github.com/umpirsky/webshot
ReplyDeleteI used bluga. The api allows you to take 100 snapshots a month without paying, but sometimes it uses more than 1 credit for a single page. I just finished upgrading a drupal module, Bluga WebThumbs to drupal 7 which allows you to print a thumbnail in a template or input filter.
ReplyDeleteThe main advantage to using this api is that it allows you to specify browser dimensions in case you use adaptive css, so I am using it to get renderings for the mobile and tablet layout as well as the regular one.
There are api clients for the following languages:
PHP,
Python,
Ruby,
Java,
.Net C#,
Perl
and Bash (the shell script looks like it requires perl)
Yes it is. If you only need image of URL try this
ReplyDelete<img src='http://zenithwebtechnologies.com.au/thumbnail.php?url=www.subway.com.au'>
Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html
You can use wimg.ca
ReplyDeleteexample: http://wimg.ca/http://stackoverflow.com/questions/757675/website-screenshots-using-php
:D
Try GrabzIt they have a great API and thier service is very fast. I use it on one of my personal websites.
ReplyDeleteAlso because they use a callback when the screenshot is finished you don't have to continously poll them like you do for thumbilizer.
They have simple instructions on their website, to request a screenshot be taken:
include("GrabzItClient.class.php");
//Create the GrabzItClient class
//Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account!
$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
//Take the picture the method will return the unique identifier assigned to this task
$id = $grabzIt->TakePicture("http://www.google.com", "http://www.example.com/GrabzItHandler.php");
Then to save that screenshot create a PHP page to accept the screenshot result:
include("GrabzItClient.class.php");
$message = $_GET["message"];
$customId = $_GET["customid"];
$id = $_GET["id"];
$filename = $_GET["filename"];
$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
$result = $grabzIt->GetPicture($id);
if (!$result)
{
return;
}
//Ensure that the application has the correct rights for this directory.
file_put_contents("images" . DIRECTORY_SEPARATOR . $filename, $result);
Guys, just use script bellow
ReplyDelete<?php
echo '<script language="javascript" src="http://www.bildesel.de/thumb.php?url='.urlencode($url).'"></script>';
?>