There is not inbuilt function or class in PHP to get the snapshot of a website. Most of the time, to take a screenshot of a website, we need to use some online APIs. There are a lot of API’s available. But no one is free. Even if something comes up, it will only last for a few weeks or months due to heavy load on the server. So the only way is to build it in you server itself (unless you have $s to buy the API).
Some Features of the Website to Screenshot PHP Script
- Capture Screenshot of the website from URL 😉
- URL validation and correction (add HTTP if required)
- Specify height and width of the screenshot
- Crop or clip the screenshot
- Cache – (Enabled by default) If the screenshot of a website is already taken, it will show the saved image instead of downloading the site again. Improve the speed by a lot.
- Save the screenshot as a file or display it directly using ‘img src’ tag
How to Take Screenshot of a Website using PHP
The code is bit lengthy and contains many other files and folders (PhantomJs itself is around 42MB). Also, the code is not much import since we aren’t making any changes in the code. So I’m not describing the full code here. But I’ll explain you the files structure, how to integrate this in your website and configure it. It’s very easy to integrate this screenshot generating PHP script.
Capturing Screenshot from URL
- Upload the files to your localhost/server
- Make the bin directory executable (not necessary in localhost)
- Navigate to screenshot.php?url=google.com
To use this in you site you can use <img src=”screenshot.php?url=google.com” />
- Width and Height: screenshot.php?url=google.com&w=1000&h=800
- With cropping: screenshot.php?url=google.com&w=1000&h=800&clipw=800&cliph=600
- Disable cache and load fresh screesnhot: screenshot.php?url=google.com&cache=0
- To download the image: screenshot.php?url=google.com&download=true
If you are not getting the screenshots try making the bin directory executable and whole folder writable. Caching is enabled by default for faster loading. Currently it’s set to 60 seconds. So if you load the same site within 60 seconds it will not a fresh screenshot.
Hope this tutorial help you. Comment below you have any doubts, suggestions or feedback.