使用 PHP 从 URL 创建缩略图
我想生成网站的缩略图。我发现一些网站可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 无法自行完成此操作,因为它不包含 HTML 渲染库。
不过,您可以找到捕获屏幕截图的外部方法,并使用 PHP 与该方法进行通信。
首先,您需要设置一个用于截取屏幕截图的系统。查看 IECapt (http://iecapt.sourceforge.net/)、CutyCapt (http://cutycapt.sourceforge.net/) 或 khtml2png (http://khtml2png.sourceforge.net/) 并在系统。
然后设置一个 PHP 脚本,该脚本将 exec() 屏幕截图应用程序并将数据返回到浏览器。
例如:
然后您可以按以下方式调用脚本:
构建屏幕截图将占用 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:
You can then call the script in the following way:
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.
答案取决于您使用的平台。无论如何,这是之前提出的问题。
如果你想创建无头(无屏幕) )基于命令行的屏幕截图,大多数方法都以某种方式涉及 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
mac:
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
恐怕 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
蒂姆可能是对的,你不能在共享主机上做类似的事情。
我完成的一个实现是在专用的 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