本文介绍了使用PHP创建来自URL的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成网站的缩略图。我发现有几个网站使用API​​处理它,例如



这怎么用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 [链接]; // !! INSECURE !!在生产中,请务必对此输入进行消毒!
$ filename ='/ var / cutycapt / images /'。 $ _REQUEST ['url']。 .PNG; //可能需要标准化文件名,这只是一个例子

//首先检查文件是否存在,如果存在,则跳过生成并重新使用文件
// This是一个超级简单的缓存系统,如果(!file_exists($ filename)){
exec('/ usr / local / bin / CutyCapt --url ='')将有助于减少资源需求
。 $ _REQUEST ['url']。'--out ='。$ filename。'');
}

//第二次检查文件是否存在,无论是从前一次运行还是从上一代例程中
if(file_exists($ filename)){
标题('Content-type:image / png');
打印file_get_contents($文件名);
} else {
header('Status:500 Internal Server Error');
}
?>

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

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

构建屏幕截图将是CPU密集型的,所以我强烈建议构建某种文件缓存(即保存输出结果并检查是否已经有截图某处),甚至可能是一个排队系统,所以你的截图服务器不会被淹没。


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?

解决方案

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.

这篇关于使用PHP创建来自URL的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:39