⬆️ ⬇️

Himself service screenshots

It all started with ...



A few years ago, I just started to get acquainted with web-programming, and one of my first “projects” was site catalogs. The development was carried out for themselves, in order to improve the experience. But since similar sites darkness, wanted to make something special. I decided that each site in the directory will be presented with a screenshot. I did not know how to automate all this, because at first I took all the screenshots with my hands and filled them in like ready files.



As time went on, the project was “covered with dust”, there was no time to deal with it, but about a year ago a new “wave of creativity” rolled over and I wanted to solve the “problem of the autoscript”.



The first thing that came to mind was ready-made screenshot services that provide an API. But, having gone through some (I don’t remember the name now), I realized that this was not for me: the opportunities were rather curtailed, sometimes it took a long time to wait for the “queue”, sometimes the sites on the screens looked rather poor. But the main problem was that all this had to work asynchronously and I could not understand from the script - whether the service gave me a temporary picture-stub, or a ready-made screenshot of the site.

')

So I decided to make my own “bicycle”.



Tool selection



To begin with, it was necessary to find a utility that can take screenshots, working in linux from the command line (the site is spinning on my ubuntu server, because there are no X). A colleague helped me by giving a link to CutyCapt .



CutyCapt is a small cross-platform utility (running from the command line) to create a screenshot of a web page and save it in various vector and raster formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, BM P (translation of the original description from official site).



In order not to engage in copy-paste from the official site, I’ll just say that everything is already described there: installation, dependencies, startup parameters, example, and method of use in the absence of an X server (via xvfb-run).



Coding



So, with the tool decided. Let's go back to the site. I saw it this way: the administrator on the description page has a “make a screenshot” button, which, through an AJAX request, invokes a script that creates a picture, performs the necessary processing with it (resize / crop / sketch), etc.

No sooner said than done. I will give a part of the code of my PHP script (leaving the most basic):



<?PHP //  -    URL ,     //      (  id),   - $url = 'http://habrahabr.ru'; // ""   //       $tmpfname = tempnam('/tmp', 'catalog') . '.jpg'; //       $url = escapeshellarg($url); //    //    CutyCapt    .  , url   ,    -    //  "xvfb-run"    $cmd = sprintf('xvfb-run --server-args="-screen 0, 1024x768x24" //C//CutyCapt --min-width=1280 --url=%s --out=\'%s\'', $url, $tmpfname); //  exec($cmd); // ,    if (file_exists($tmpfname)) { // ,    0  (    ) if (filesize($tmpfname) > 1) { //         $tmpfname ,    //      ,  ,    ,   ..... // , ,  phpthumb (phpthumb.gxdlabs.com)   $f = date("ymd_his", time()).".jpg"; //    if (copy($tmpfname, IMAGES_DIR.$f)) { if (file_exists(IMAGES_DIR.$f)) { try{ $thumb = PhpThumbFactory::create(IMAGES_DIR.$f); //  jpg $thumb->setOptions( array('jpegQuality' => 90) ); //   $thumb->resize(800); //    800x800px $thumb->crop(0, 0, 800, 800); $thumb->save(IMAGES_DIR.$f); //  $thumb->resize(200)->crop(0,0,200,150); $thumb->save(IMAGES_DIR.'t_'.$f); } catch(exception $e) { //  ... } //       ,  . } } } //    unlink($tmpfname); } //       json -   /   ?> 


Oh yeah, I would like to share information about the "pitfalls":

  1. Sometimes CutyCapt took a long time to screen, but never completed its work;
  2. Sometimes did 0-byte screenshots
  3. Sometimes it crashed with an error on some flash-sites. I had to sacrifice them and just remove the flash-plugin from the system. Now, just on the screenshots in place of the flush is empty, but there are no more departures because of the flush.




Literature:

CutyCapt official website

Php.net



PS If it seems to someone that I have written little about CutyCapt, say - I will add an article (it just seemed to me that everything was clearly described on the site)

Source: https://habr.com/ru/post/111188/



All Articles