使用 PHP 从 URL 创建缩略图

发布于 2024-10-14 18:23:51 字数 162 浏览 1 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌酱 2024-10-21 18:23:51

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 资源,因此我强烈建议构建某种文件缓存(即保存输出结果并检查是否你已经在某处有一个屏幕截图),甚至可能是一个排队系统,这样你的屏幕截图服务器就不会被淹没。

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.

心是晴朗的。 2024-10-21 18:23:51

答案取决于您使用的平台。无论如何,这是之前提出的问题。

如果你想创建无头(无屏幕) )基于命令行的屏幕截图,大多数方法都以某种方式涉及 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。

干杯

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

怪我闹别瞎闹 2024-10-21 18:23:51

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

可能的相关答案

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

痴者 2024-10-21 18:23:51

蒂姆可能是对的,你不能在共享主机上做类似的事情。

我完成的一个实现是在专用的 Linux 服务器上使用的,它具有 Xvfb< /a>、firefox 和导入命令。

您可能还想检查此问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文