duan2891
2011-01-30 20:10
浏览 65
已采纳

使用PHP从URL创建缩略图

I want to generate thumbnails of websites. I found a few sites that handle it with an API, such as http://www.websnapr.com/

How can this be done with PHP so I can handle all of the requests on my server?

图片转代码服务由CSDN问答提供 功能建议

我想生成网站的缩略图。 我找到了一些使用API​​处理它的网站,例如 http://www.websnapr.com/

如何用PHP完成这项工作,以便处理我服务器上的所有请求?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doumao8803 2011-01-30 20:29
    已采纳

    PHP can't do this on it's own as it does not include an HTML rendering library.

    You can find an external method of capturing the screenshots and communicate with that method using PHP, though.

    First you'll need a system set up to take screenshots. Look into IECapt (http://iecapt.sourceforge.net/), CutyCapt (http://cutycapt.sourceforge.net/) or khtml2png (http://khtml2png.sourceforge.net/) and configure one of those on a system.

    Then set up a PHP script that will exec() the screenshot taking application and return the data to the browser.

    For example:

    <?php
    $in_url = 'http://' . $_REQUEST['url']; // !!INSECURE!! In production, make sure to sanitize this input!
    $filename = '/var/cutycapt/images/' . $_REQUEST['url'] . '.png'; // Will probably need to normalize filename too, this is just an illustration
    
    // First check the file does not exist, if it does exist skip generation and reuse the file
    // This is a super simple caching system that will help to reduce the resource requirements
    if(!file_exists($filename)) {
      exec('/usr/local/bin/CutyCapt --url="' . $_REQUEST['url'] . '" --out="' . $filename . '"');
    }
    
    // Second check if the file exists, either from a previous run or from the above generation routine
    if(file_exists($filename)) {
      header('Content-type: image/png');
      print file_get_contents($filename);
    } else {
      header('Status: 500 Internal Server Error');
    }
    ?>
    

    You can then call the script in the following way:

    http://localhost/screenshot.php?url=www.google.com
    

    Building the screenshots is going to be CPU intensive so I'd strongly recommend building in some kind of file caching (ie. save the results of the output and check to see if you already have a screenshot somewhere), perhaps even a queuing system so your screenshot server does not get overwhelmed.

    打赏 评论
  • douzhongjiu2263 2011-01-30 20:16

    I'm afraid php is not able to handle such tasks on its own. You have to install some external libraries on server.

    Possible related answer

    打赏 评论
  • dowaw80220 2011-01-30 20:26

    Tim is probably right, you can't do something like that on a shared host.

    An implementation I've done was used on a dedicated linux server and it featured Xvfb, firefox and the import command.

    You might also want to check this question

    打赏 评论
  • doukuangxun5382 2011-01-30 20:35

    Answer depends on which platform you are using. Anyways, here is this question asked before.

    If you want to create headless (no screen) commandline based screenshots, most aproaches involve Xvfb in some way, and/or installing a lot of dependencies/libraries.

    linux:

    khtml2png.sourceforge.net

    mysql-apache-php.com/website_screenshot.htm

    cutycapt.sourceforge.net

    www.unruhdesigns.com/news/2010/10/using-firefox-on-a-headless-server-to-make-screenshots-of-websites

    windows:

    iecapt.sourceforge.net

    mac:

    www.paulhammond.org/webkit2png/

    EDIT: It's more than posible on something like rackspace of course, and on any shared host that lets you compile and install your own code, like webfaction.

    cheers

    打赏 评论

相关推荐 更多相似问题