Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Create Screenshots of Websites (html) with PHP Script and Linux Command LIne

    How to Create Screenshots of Websites (html) with PHP Script and Linux Command LIne

    RahulBy RahulFebruary 19, 20142 Mins Read

    Wkhtmltoimage is a very useful application to create screenshot of a website or webpage. Some times we required to create an screenshot of webpages and store to 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.

    Step 1: Install wkhtmltoimage in Linux

    Download wkhtmltoimage from google code and install on your linux system using following steps.

    # cd /opt
    # wget http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.0/wkhtmltox-linux-i386_0.12.0-03c001d.tar.xz
    # tar xJf wkhtmltox-linux-i386_0.12.0-03c001d.tar.xz
    # ln -s /opt/wkhtmltox/bin/wkhtmltoimage /usr/bin/wkhtmltoimage
    # chown apache:apache /usr/bin/wkhtmltoimage
    # chmod +x /usr/bin/wkhtmltoimage
    

    Step 2: Create Image Using Command Line

    First check wkhtmltoimage script using linux command line. Following command will take screen shot of http://google.com and create and image with name google.jpg.

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

    Step 3: PHP Code to Create Image

    Use following php code to generate image from html ( website url ). This script required to be enabled shell_exec function for Apache. Most of shared hosting providers doesn’t allow’s this function.

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

    PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?php
     
    $url = $_GET['url'];    // Website URL to Create Image
    $name = $_GET['img'];   // Output Image Name
    $command = "/usr/bin/wkhtmltoimage --no-images --load-error-handling ignore";
    $dir_img = "/var/www/html/images/";     // Image files will be saved here
    $ex_cmd = "$command $url " . $dir_img . $name;
    $output = shell_exec($ex_cmd);
    ?>

    Use following syntex or demo url to generate image of website ( html ).

    Syntax:

    http://youdomain.com/getImage.php?url=<website url>&img=<image name>

    Example:

    https://tecadmin.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 below steps.

    Create few required directories in your web server document root

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

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

    PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    &lt;?php
     
    $url = str_replace('index.php','', $_SERVER['PHP_SELF']);
    $url = str_replace($url,'',$_SERVER['REQUEST_URI']);
    $url = explode('/',$url);
    $name = array_shift($url);
    $weburl = array_shift($url);
     
    $command = &quot;/usr/bin/wkhtmltoimage --no-images --load-error-handling ignore&quot;;
    $dir_img = &quot;/var/www/html/getImage/images/&quot;;     // Image files will be saved here
    $ex_cmd = &quot;$command $weburl &quot; . $dir_img . $name;
    $output = shell_exec($ex_cmd);
    echo &quot;&lt;img src=&quot;/getImage/images/$name&quot; &gt;&quot;;  // comment this to disable image show on page
     
    ?&gt;

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

    # vi /var/www/html/getImage/.htaccess
    
    RewriteEngine On
    RewriteCond %{REQUEST_URI} /images/.+(gif|png|jpg)$
    RewriteRule .* - [L]
    RewriteRule ^.*$ index.php
    
    

    Now you can create images by accessing urls like below

    Syntax:

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

    Example:

     https://tecadmin.net/getImage/123.jpg/yahoo.com
    html to image wkhtmltoimage
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleInstall Opera 12.16 Web Browser in CentOS/RHEL and Fedora
    Next Article 3 Sample PHP Scripts for Sending Emails

    Related Posts

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:May 9, 20223 Mins Read

    (Resolved) userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms

    Updated:May 10, 20221 Min Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How To Enable SSH Server on Ubuntu 22.04

    Updated:April 22, 20222 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    7 Comments

    1. Cric on September 20, 2019 2:46 am

      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!

      Reply
    2. fol.to on June 25, 2019 8:07 pm

      You are a very bright individual!

      Reply
    3. Ft Farhad on February 24, 2016 7:16 am

      Really php is a allrounder language.

      Reply
    4. Domus on May 12, 2014 11:47 am

      Hi, great article although if you don’t want to worry about scalability and managing your own service you could use a free third-party service. Like GrabzIt’s PHP API: http://grabz.it/api/php/

      Reply
    5. omar on April 24, 2014 3:00 pm

      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

      Reply
    6. omar on April 24, 2014 10:55 am

      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

      Reply
      • Rahul on April 24, 2014 12:30 pm

        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.

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.