php - 使用 PHP 从 URL 创建缩略图

标签 php html web

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

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

最佳答案

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

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

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

然后设置一个 PHP 脚本,它将执行()截取应用程序的屏幕截图并将数据返回给浏览器。

例如:

<?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,因此我强烈建议构建某种文件缓存(即保存输出结果并检查您是否已经在某处创建了屏幕截图),甚至可能是排队系统,这样您的屏幕截图服务器就不会不堪重负。

关于php - 使用 PHP 从 URL 创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845202/

相关文章:

使用 innerHTML 时 JavaScript 不工作

html - base64 格式图像发出服务器请求

php - 如何选择一个特定的id值而不是最后一个mysql php

php - 使用 PHP & MySQL 的考勤系统

php - 无法使用 PHP 更改 xml 值

javascript - 合并两个 PHP - Kimonlabs

html - 图 slider "Stacks"

javascript - null 在 xhr 请求中发送了期望的文件内容

javascript - Fuse.js:在类数组对象中进行模糊搜索

java - pom.xml 中的依赖类型