Website screenshots

前端 未结 26 3534
长发绾君心
长发绾君心 2020-11-21 06:07

Is there any way of taking a screenshot of a website in PHP, then saving it to a file?

相关标签:
26条回答
  • 2020-11-21 06:46

    There are many open source projects that can generate screenshots. For example PhantomJS, webkit2png etc

    The big problem with these projects is that they are based on older browser technology and have problems rendering many sites, especially sites that use webfonts, flexbox, svg and various other additions to the HTML5 and CSS spec over the last couple of months/years.

    I've tried a few of the third party services, and most are based on PhantomJS, meaning they also produce poor quality screenshots. The best third party service for generating website screenshots is urlbox.io. It is a paid service, although there is a free 7-day trial to test it out without committing to any paid plan.

    Here is a link to the documentation, and below are simple steps to get it working in PHP with composer.

    // 1 . Get the urlbox/screenshots composer package (on command line):
    composer require urlbox/screenshots
    
    // 2. Set up the composer package with Urlbox API credentials:
    $urlbox = UrlboxRenderer::fromCredentials('API_KEY', 'API_SECRET');
    
    // 3. Set your options (all options such as full page/full height screenshots, retina resolution, viewport dimensions, thumbnail width etc can be set here. See the docs for more.)
    $options['url'] = 'example.com';
    
    // 4. Generate the Urlbox url
    $urlboxUrl = $urlbox->generateUrl($options);
    // $urlboxUrl is now 'https://api.urlbox.io/v1/API_KEY/TOKEN/png?url=example.com'
    
    // 5. Now stick it in an img tag, when the image is loaded in browser, the API call to urlbox will be triggered and a nice PNG screenshot will be generated!
    <img src="$urlboxUrl" />
    

    For e.g. here's a full height screenshot of this very page:

    https://api.urlbox.io/v1/ca482d7e-9417-4569-90fe-80f7c5e1c781/8f1666d1f4195b1cb84ffa5f992ee18992a2b35e/png?url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F757675%2Fwebsite-screenshots-using-php%2F43652083%2343652083&full_page=true

    0 讨论(0)
  • 2020-11-21 06:48

    I've found this to be the best and easiest tool around: ScreenShotMachine. It's a paid service, but you get 100 free screenshots and you can buy another 2,000 for (about) $20, so it's a pretty good deal. It has a very simple usage, you just use a URL, so I wrote this little script to save a file based on it:

    <?php
      $url = file_get_contents("http://api.screenshotmachine.com/?key={mykey}&url=https://stackoverflow.com&size=X");
    
      $file = fopen("snapshots/stack.jpg", "w+");
      fwrite($file, $url);
      fclose($file);
      die("saved file!");
    ?>
    

    They have a very good documentation here, so you should definitely take a look.

    0 讨论(0)
  • 2020-11-21 06:49

    LAST EDIT: after 7 years I'm still getting upvotes for this answer, but I guess this one is now much more accurate.


    Sure you can, but you'll need to render the page with something. If you really want to only use php, I suggest you HTMLTOPS, which renders the page and outputs it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and don't support all the CSS).

    Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whatever.. Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine. You have to install it on your server, as well..

    UPDATE Now, with new HTML5 and JS feature, is also possible to render the page into a canvas object using JavaScript. Here a nice library to do that: Html2Canvas and here is an implementation by the same author to get a feedback like G+. Once you have rendered the dom into the canvas, you can then send to the server via ajax and save it as a jpg.

    EDIT: You can use the imagemagick tool for transforming pdf to png. My version of wkhtmltopdf does not support images. E.g. convert html.pdf -append html.png.

    EDIT: This small shell script gives a simple / but working usage example on linux with php5-cli and the tools mentioned above.

    EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly

    0 讨论(0)
  • 2020-11-21 06:49

    I'm on Windows so I was able to use the imagegrabwindow function after reading the tip on here from stephan. I added in cropping (to get rid of the Browser header, scroll bars, etc.) and resizing to get a final image. Here's my code. Hope that helps someone.

    0 讨论(0)
  • 2020-11-21 06:50

    If you don't want to use any third party tools, I have come across to simple solution that is using Google Page Insight api.

    Just need to call it's api with params screenshot=true.

    https://www.googleapis.com/pagespeedonline/v1/runPagespeed?
    url=https://stackoverflow.com/&key={your_api_key}&screenshot=true
    

    For mobile site view pass &strategy=mobile in params,

    https://www.googleapis.com/pagespeedonline/v1/runPagespeed?
    url=http://stackoverflow.com/&key={your_api_key}&screenshot=true&strategy=mobile
    

    DEMO.

    0 讨论(0)
  • 2020-11-21 06:51

    Yes. You will need some things tho:

    See khtmld(aemon) on *nx. See Url2Jpg for Windows but since it is dotNet app you should also chek Url2Bmp

    Both are console tools that u can utilise from your web app to get the screenshot.

    There are also web services that offer it. Check this out for example.

    Edit:

    This link is useful to.

    0 讨论(0)
提交回复
热议问题