本文實例講述了Windows平臺PHP+IECapt實現(xiàn)網(wǎng)頁批量截圖并創(chuàng)建縮略圖功能。分享給大家供大家參考,具體如下:
最近在開發(fā)一個本地互聯(lián)網(wǎng)應(yīng)用的項目,為了增加用戶體驗,需要在搜索結(jié)果左側(cè)顯示如圖一所示的某個網(wǎng)站的縮略圖效果,在網(wǎng)上不停地百度谷歌了一上午后,發(fā)現(xiàn)大多數(shù)實現(xiàn)少量截圖還是可以的,如果大批量的截圖總會在中途出現(xiàn)很多問題,最終也沒有發(fā)現(xiàn)十分滿意的程序,干脆自己弄吧。
![](/d/20211017/4a02232ac5309a44b32fa3ca47e57500.gif)
(圖一)
下面是在windows環(huán)境下用php結(jié)合iecapt實現(xiàn)的網(wǎng)頁截圖并創(chuàng)建縮略圖的步驟和代碼:
一、準備
下載最新版IECapt
官方地址:http://iecapt.sourceforge.net/
在linux環(huán)境下,可以考慮用HTML2Image來實現(xiàn)
下載地址:http://www.guangmingsoft.net/htmlsnapshot/html2image.i386.tar.gz
其它的實現(xiàn)方式還有CutyCapt,另外,只要是windows環(huán)境,有IE瀏覽器(推薦使用IE7)即可,這個大部分機器都應(yīng)該不是問題。
二、創(chuàng)建數(shù)據(jù)表(這一步非必須,根據(jù)實際情況選用)
因為要批量截圖,數(shù)據(jù)十分的多,建立一個數(shù)據(jù)表來存放要截圖的網(wǎng)站的url地址還是有必要的,如下所示(mysql數(shù)據(jù)庫表):
CREATE TABLE IF NOT EXISTS `t_url` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(100) NOT NULL,
`pictype` tinyint(1) unsigned NOT NULL COMMENT '1.非比例縮略圖2比例縮略圖
`flag` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0.禁用1.可用
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk COMMENT='url鏈接表' AUTO_INCREMENT=1 ;
三、創(chuàng)建批處理文件
1.首先把下載的iecapt壓縮包解壓,然后把iecapt.exe放到要生成截圖的文件夾下(如:img_tmp)。
為了便于理解,在看下面代碼前,先創(chuàng)建一個test.bat文件,鼠標(biāo)右擊編輯,寫入一句話if not exist ay360cn.jpg (iecapt.exe --url=http://www.ay#/ --out=ay360cn.jpg)保存,雙擊運行test.bat看看是否會在本目錄下多出一個名叫ay360cn.jpg的文件,如果看到說明截圖成功,這句話是截圖的核心語句。
2.將需要截圖的url鏈接導(dǎo)入url鏈接表t_url,然后執(zhí)行如下php代碼:
?php
//------------------------------------------------------------
//從表t_url中提取url鏈接,存放到數(shù)組$data中
//--------------------------------------------------------------
mysql_connect("localhost","root","123");
mysql_select_db("test");
$sql = "select * from t_url";
//選用sql語句$sql2 = "select * from t_url where pictype = 1 and flag = 1";
$query = mysql_query($sql);
//------------------------------------------
//生成批處理文件
//------------------------------------------
$expire_time = 10; //代表10天,文件過期時間,86400秒/天
$i = 0;
foreach($row = mysql_fetch_array($query)){
$url_md5 = md5($row['url']);
$file_folder = 'img/';
$filename = $file_folder.$url_md5.'.'.'jpg';
$newname = $url_md5.'.'.'jpg';
if (!file_exists($filename) || (filemtime ($filename) + $expire_time * 86400 time()) ) {
$str .= "if not exist ".$newname." (iecapt.exe --url=".$value['url']." --out=".$newname.")\r\n";
if(($i % 30) == 0 $i > 0){ //每30條為一個批處理文件
$title = "title capt".$i.".bat\r\n";
$str = $title.$str;
$file_bat = fopen("img_tmp/capt".$i.".bat","w");
if(fwrite($file_bat,$str)){
echo "批處理文件capt".$i."生成成功br>";
$str = "";
}
}
$i = $i+1;
}
}
?>
運行結(jié)果:
![](/d/20211017/6bb26495b7550f88789b58326d9e261c.gif)
(圖二)
四、執(zhí)行批處理文件
可以通過php程序循環(huán)執(zhí)行 批處理文件,但在運行當(dāng)中會出現(xiàn)很多問題,這里手動直接批量打開上面剛創(chuàng)建好的批處理文件,考慮到帶寬和cpu,最多不要超過20個,截圖的速度大約3-5秒/張效果如圖三:
![](/d/20211017/22d038dc4ed7a9d09dca9585d4f4be07.gif)
(圖三)
五、創(chuàng)建縮略圖
生成縮略圖的文件是create_image_img.php,其中包含生成縮略圖的主要的一個類文件是image.class.php,兩個文件的代碼如下:
ceate_image_img.php代碼:
?php
mysql_connect("localhost","root","123456");
mysql_select_db("test");
if(!isset($_GET['ID'])){
$_GET['ID'] = 1;
}
if($_GET['ID']){
$sql = "select * from t_url id =".$_GET['ID'];
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
echo "span style='color:#CE0000;'>正在生成縮略圖:/span>".$row['id']."nbsp;".$row['url']."br>br>";
$url = $row['url'];
$url_md5 = md5($url);
$pictype = $row['pictype'];
$limit_time = 1; //創(chuàng)建 $limit_time日內(nèi)創(chuàng)建的大圖,天
$thumbnails_folder = 'img_tmp/'; //保存臨時大圖的目錄,必須以/結(jié)束
$thumbnails_folder2 = 'img/'; //保存小圖的目錄,必須以/結(jié)束
$output_format = 'jpg';
$cached_filename = $thumbnails_folder.$url_md5.".".$output_format;
$to_filename = $thumbnails_folder2 .$url_md5.'.'.$output_format;
if((file_exists($cached_filename) || filemtime ($filename) + $limit_time*86400 > time())
!file_exists($to_filename)){
if (filesize($cached_filename) > 1024){ //字節(jié),不能是空白圖片
//創(chuàng)建縮略圖
include("image.class.php");
$img = new Zubrag_image;
// get parameters
$img->image_type = 2; // 1 = GIF, 2 = JPG, 3 = PNG
$img->quality = 80;
$img->max_w = 90;
$img->max_h = 67;
$img->iscapt = ($pictype == 1) ? true : false; //此處用布爾型即可,數(shù)據(jù)庫不可1.非比例縮略圖2.按比例縮略
if($img->GenerateThumbFile($cached_filename, $to_filename)){
echo "span style='color:#CE0000;'>成功創(chuàng)建縮略圖:/span>".$row['id']."nbsp;".$row['url'];
}else{
echo "span style='color:#0000CE;'>未能創(chuàng)建縮略圖:/span>".$row['id']."nbsp;".$row['url'];
}
}
}
$sql = "select * from t_url id >".$_GET['ID']." and flag = 1 order by id asc limit 1";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
echo "br>span style='color:#0000CE;'>準備生成縮略圖:/span>".$row['id']."nbsp;".$row['url']."br>br>";
if($row['id']){
echo "script>window.location.href='create_image_img.php?ID=".$row['id']."';/script>";
}else{
$_GET['ID'] = "";
}
}
?>
image.class.php代碼:
?php
class Zubrag_image {
var $iscapt = true;
var $image_type = -1;
var $quality = 100;
var $max_w = 100;
var $max_h = 100;
function SaveImage($im, $filename) {
$res = null;
if(($this->image_type == 1) !function_exists('imagegif')) $this->image_type = 3;
switch ($this->image_type) {
case 1:
//if ($this->save_to_file) {
$res = ImageGIF($im,$filename);
//}
//else {
// header("Content-type: image/gif");
// $res = ImageGIF($im);
//}
break;
case 2:
$res = ImageJPEG($im,$filename,$this->quality);
break;
case 3:
$res = ImagePNG($im,$filename);
break;
}
return $res;
}
function ImageCreateFromType($type,$filename) {
$im = NULL;
switch ($type) {
case 1:
$im = ImageCreateFromGif($filename);
break;
case 2:
$im = ImageCreateFromJpeg($filename);
break;
case 3:
$im = ImageCreateFromPNG($filename);
break;
}
return $im;
}
function GenerateThumbFile($from_name, $to_name) {
list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($from_name);
/*if ($this->cut_x > 0) $orig_x = min($this->cut_x, $orig_x);
if ($this->cut_y > 0) $orig_y = min($this->cut_y, $orig_y);*/
if ($this->iscapt (($orig_y/$orig_x) > (90/67))) { //是截圖,且高度過高
$orig_y = $orig_x*(67/90);
}
$this->image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type);
if ($orig_img_type 1 or $orig_img_type > 3) die("Image type not supported");
if ($this->image_type == 1) {
$ni = imagecreate($this->max_w, $this->max_h);
}
else {
$ni = imagecreatetruecolor($this->max_w,$this->max_h);
}
$white = imagecolorallocate($ni, 255, 255, 255);
imagefilledrectangle( $ni, 0, 0, $this->max_w, $this->max_h, $white);
$im = $this->ImageCreateFromType($orig_img_type,$from_name);
imagepalettecopy($ni,$im);
imagecopyresampled(
$ni, $im,
0, 0, 0, 0,
$this->max_w, $this->max_h,
$orig_x, $orig_y);
if($this->SaveImage($ni, $to_name)){
return true;
}else{
return false;
}
}
}
?>
六、總結(jié)
至此整個實現(xiàn)網(wǎng)頁截圖并創(chuàng)建縮略圖的的步驟結(jié)束,其中執(zhí)行批處理文件部分為了提高截圖效率采用手動的方式,批量打開批處理文件,另外,鏈接數(shù)據(jù)庫部分還可以用封裝的數(shù)據(jù)庫操作類來實現(xiàn),代碼會更加簡潔。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- php實現(xiàn)的支付寶網(wǎng)頁支付功能示例【基于TP5框架】
- php實現(xiàn)網(wǎng)頁上一頁下一頁翻頁過程詳解
- PHP 爬取網(wǎng)頁的主要方法
- 實例分析基于PHP微信網(wǎng)頁獲取用戶信息
- php實現(xiàn)網(wǎng)頁常見文件上傳功能
- php中抓取網(wǎng)頁內(nèi)容的實例詳解
- php編程實現(xiàn)簡單的網(wǎng)頁版計算器功能示例
- PHP網(wǎng)頁緩存技術(shù)優(yōu)點及代碼實例