Capture Screenshot of a Website using PHP without API

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).

After a lot of searches, I found PhantomJS, a browser built with JavaScript. It has a lot of features including taking screenshots of a website, CSS selector and much more. With some help from my friends, I integrated PhantomJS with PHP. I also added some more features to it. I have described each one of them in detail as below.


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.

Download Source Code

Code Structure


Capturing Screenshot from URL

  1. Upload the files to your localhost/server
  2. Make the bin directory executable (not necessary in localhost)
  3. Navigate to screenshot.php?

To use this in you site you can use <img src=”screenshot.php?” />

URL Parameters

  • Width and Height: screenshot.php?
  • With cropping: screenshot.php?
  • Disable cache and load fresh screesnhot: screenshot.php?
  • To download the image: screenshot.php?


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.

  • Sirin Brown

    How to make its api?

    • Your convert this code to an API by uploading it your server so other users can use it by

      That’s just a basic API. To create real API, you need user registration so that each user will have an API key. Only people with a valid API key can access your service. You can also limit the no. of screenshots one can generate in a day by checking each request. So it will be something like You can develop RestAPI and everything for efficiency and usability.

      • Sirin Brown

        I got 403 error even though I am having 777 permissions.

        Can you tell me how to Make the bin directory executable

        • 403 error is due to permission errors. Never use 777 permission. User 755 instead. If it is in the server, you can set the permission from CPanel file manager or you can do that from FileZilla FTP client too. Have you tried this in your localhost? Becuase you don’t have to put any permissions in localhost.

          • Sirin Brown

            yes in local working all fine but when i changed permissions to 755 on server still no change.

            Showing 403 error

          • What type of hosting do you have shared/vps/dedicated? Also is that a linux or windows server?

          • Sirin Brown

            Its shared based on linux.

  • good one

  • Kruxi Nator

    Good job.
    But, if i try and some other sites as url the image is black. I found out that those sites had javascript render blocking enabled. Is there a way to bypass this?

    • Ok. We are looking into it. Will get back to you shortly

      • Stanislav

        I’m having this problem too. I downloaded the new version (PhantomJS 2.1. In post 1.9 version ) from the official site and the problem disappeared.
        And thanks for the solution. You can also modify that was first resize and then crop

        • @sdelfi:disqus thanks a lot. We will soon update it in our tutorial. Hope this helps you @kruxinator:disqus

    • @kruxinator:disqus we have updated the files. It can now take screenshots of Facebook and similar sites that showed blank.

  • It is written in php as an open source. Exactly what I’m looking for.

    • Great! We love your feedback

  • Roy Gaming

    Is there a way to change the name of the jpeg that will be saved

  • Hassan Nomani Alvi

    what if has some parameter like

    • Xavier Birnie-scott

      Encode url with php

  • masatar

    I uploaded the files on my linux server and downloaded the linux version of phantomjs. but the script dont generate a screenshot. is there something to consider when i use that script on a linux machine??? on windows works that script fine.

  • Hi, I have tried and its working, but some sites are black on background.

    • grifare

      In screenshot.php file, find jpg and jpeg replace with png. There are 3 instances. Problem solved.

  • Rahul kannaujiya

    how to run program on server

  • Rahul kannaujiya

    server on only google take screenshot
    ather site is not take screenshot on server

  • Rahul kannaujiya

    plz any one tell me how to take screenshot on server

  • Xavier Birnie-scott

    Really cool, very well done. And I’ve learned how to use phantomjs in a PHP script. Thanks.

  • Patrick

    I am getting a 500 Internal Server Error, but I set the permission to 777 everywhere just to make sure…

  • Zhong Ayi

    installed on Linux server and loaded it with phantomjs-2.1.1instead of the phantomjs.exe that comes with the script. I can load ? and ? and ? but cannot load any other website address. Any idea why?

  • Tayfun Aydin

    what if has some parameter like
    or other (WEBSITE) URL with QueryString, how work ?
    Have anybody PHP code examples, to work with Url + querstring ??
    Please Help….