【问题标题】:Create thumbnails from URLs using PHP使用 PHP 从 URL 创建缩略图
【发布时间】:2011-06-18 05:28:58
【问题描述】:

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

如何使用 PHP 完成此操作,以便我可以处理服务器上的所有请求?

【问题讨论】:

标签: php html web


【解决方案1】:

PHP 无法单独执行此操作,因为它不包含 HTML 呈现库。

不过,您可以找到捕获屏幕截图的外部方法,并使用 PHP 与该方法进行通信。

首先,您需要设置一个系统来截取屏幕截图。查看 IECapt (http://iecapt.sourceforge.net/)、CutyCapt (http://cutycapt.sourceforge.net/) 或 khtml2png (http://khtml2png.sourceforge.net/) 并配置其中一个系统。

然后设置一个 PHP 脚本,该脚本将 exec() 截屏应用程序并将数据返回给浏览器。

例如:

<?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');
}
?>

然后您可以通过以下方式调用脚本:

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

构建屏幕截图会占用大量 CPU,因此我强烈建议构建某种文件缓存(即保存输出结果并检查您是否已经在某处有屏幕截图),甚至可能是排队系统,这样您的屏幕截图服务器就不会不堪重负。

【讨论】:

    【解决方案2】:

    答案取决于您使用的平台。不管怎样,这是question asked before.

    如果您想创建基于无头(无屏幕)命令行的屏幕截图,大多数方法都以某种方式涉及 Xvfb,和/或安装大量依赖项/库。

    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

    ma​​c:

    www.paulhammond.org/webkit2png/

    编辑:当然,它不仅可以在 rackspace 之类的东西上,也可以在任何允许您编译和安装自己的代码的共享主机上,例如 webfaction。

    干杯

    【讨论】:

      【解决方案3】:

      恐怕 php 无法自行处理此类任务。您必须在服务器上安装一些外部库。

      可能相关answer

      【讨论】:

        【解决方案4】:

        Tim 可能是对的,你不能在共享主机上做这样的事情。

        我在一个专用的 linux 服务器上使用了一个实现,它具有 Xvfb、firefox 和 import 命令。

        您可能还想查看this question

        【讨论】:

          猜你喜欢
          • 2015-05-04
          • 2015-09-16
          • 2023-03-25
          • 2023-03-20
          • 2011-10-27
          • 1970-01-01
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          相关资源
          最近更新 更多