Wkhtmltoimage is a very useful application to create screenshots of a website or webpage. Sometimes we are required to create a screenshot of web pages and store in our system. It uses QT Webkit rendering engine for creating images in various formats. This article will help to capture screenshots of a webpage using PHP script and Linux command line.

Advertisement

Step 1: Install wkhtmltoimage

Visit the wkhtmltopdf.org and install and download the package for your Linux system. Then install it using the standard package manager. For example, to download and install the package on Ubuntu 22.04, execute:

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb 
sudo apt install -f ./wkhtmltox_0.12.6.1-2.jammy_amd64.deb 

Step 2: Capture Screenshot Using Command Line

First, check wkhtmltoimage script using the Linux command line. The following command will take a screenshot of http://google.com and create an image with the name google.jpg.

wkhtmltoimage --no-images --load-error-handling ignore http://google.com google.jpg 

Step 3: PHP Code to Create Image

Use the following PHP code to generate images from the website URL. This script required to be enabled the shell_exec function for Apache. Most shared hosting providers don’t allow this function.

Create a file name getImage.php and put below code:

Use the following syntax or demo URL to generate an image of the website.

Example:

https://example.net/getImage.php?url=http://google.com&img=image1.jpg

Step 4: PHP Script for SEO Friendly URL

If you want to create SEO friendly URL for this setup, you can use the below steps.

Create a few required directories in your web server document root

mkdir -p /var/www/html/getImage/images/ 

Create index.php file with the following content. Make sure all directory paths are correct,

Now create a .htaccess file in this directory to move all requests to the index.php file. make sure your server is enabled to use the .htaccess file.

vi /var/www/html/getImage/.htaccess 

Add the following content to the file:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} /images/.+(gif|png|jpg)$
RewriteRule .* – [L]
RewriteRule ^.*$ index.php

Save the file and close it.

Now you can create images by accessing the URL below:

Syntax:

 http://youdomain.com/getImage/[imagename]/[site url without http prefix]

Example:

 https://tecadmin.net/getImage/123.jpg/yahoo.com

Share.

7 Comments

  1. Very nice post. I just stumbled upon your weblog and wished to say
    that I have truly enjoyed browsing your blog posts.

    After all I will be subscribing to your feed and I hope you
    write again soon!

  2. Hello again!

    Thank you for fast answers!

    This updated steps helped me yes but there is one thing.

    If you want to run this the link from another website so I would do this:

    in my case:
    <img src="/getimages/.jpg/” />
    I get error on the main site and it’s look like I am loading image that dosen’t exists.

    So is there any change to make the url http://thedomain.com/getimages/somedomain.com.jpg/somedomain.com be an image, not php file.

    Like this:
    s.wordpress.com/mshots/v1/http://google.com <– You cannot veiw the source cause it's image 🙂

    Regards

  3. Hello.

    Thx for this advise.

    is there any possible way to let the script work that like this one:
    s.wordpress.com/mshots/v1/http://google.com

    I could use it like this in your way with mydomain.com/getImage.php?url=http://$getDomain&img=$getDomain

    but if I do this way then the image will now show on that url, I would like to make the url (the image) like the s.wordpress.com does.

    Thanks

    • Hi Omar,

      We have updated the article, and added Step #4, I hope that will fulfill your requirement.

      Also there are little changes in Step 1, to download latest binaries.

Leave A Reply

Exit mobile version