本文實例講述了Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能。分享給大家供大家參考,具體如下:
最近在開發一個本地互聯網應用的項目,為了增加用戶體驗,需要在搜索結果左側顯示如圖一所示的某個網站的縮略圖效果,在網上不停地百度谷歌了一上午后,發現大多數實現少量截圖還是可以的,如果大批量的截圖總會在中途出現很多問題,最終也沒有發現十分滿意的程序,干脆自己弄吧。
(圖一)
下面是在windows環境下用php結合iecapt實現的網頁截圖并創建縮略圖的步驟和代碼:
一、準備
下載最新版IECapt
官方地址:http://iecapt.sourceforge.net/
在linux環境下,可以考慮用HTML2Image來實現
下載地址:http://www.guangmingsoft.net/htmlsnapshot/html2image.i386.tar.gz
其它的實現方式還有CutyCapt,另外,只要是windows環境,有IE瀏覽器(推薦使用IE7)即可,這個大部分機器都應該不是問題。
二、創建數據表(這一步非必須,根據實際情況選用)
因為要批量截圖,數據十分的多,建立一個數據表來存放要截圖的網站的url地址還是有必要的,如下所示(mysql數據庫表):
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 ;
三、創建批處理文件
1.首先把下載的iecapt壓縮包解壓,然后把iecapt.exe放到要生成截圖的文件夾下(如:img_tmp)。
為了便于理解,在看下面代碼前,先創建一個test.bat文件,鼠標右擊編輯,寫入一句話if not exist ay360cn.jpg (iecapt.exe --url=http://www.ay360.cn/ --out=ay360cn.jpg)保存,雙擊運行test.bat看看是否會在本目錄下多出一個名叫ay360cn.jpg的文件,如果看到說明截圖成功,這句話是截圖的核心語句。
2.將需要截圖的url鏈接導入url鏈接表t_url,然后執行如下php代碼:
<?php//------------------------------------------------------------//從表t_url中提取url鏈接,存放到數組$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; }}?>
運行結果:
(圖二)
四、執行批處理文件
可以通過php程序循環執行 批處理文件,但在運行當中會出現很多問題,這里手動直接批量打開上面剛創建好的批處理文件,考慮到帶寬和cpu,最多不要超過20個,截圖的速度大約3-5秒/張效果如圖三:
(圖三)
五、創建縮略圖
生成縮略圖的文件是create_image_img.php,其中包含生成縮略圖的主要的一個類文件是image.class.php,兩個文件的代碼如下:
ceate_image_img.php代碼:
<?phpmysql_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']." ".$row['url']."<br><br>"; $url = $row['url']; $url_md5 = md5($url); $pictype = $row['pictype']; $limit_time = 1; //創建 $limit_time日內創建的大圖,天 $thumbnails_folder = 'img_tmp/'; //保存臨時大圖的目錄,必須以/結束 $thumbnails_folder2 = 'img/'; //保存小圖的目錄,必須以/結束 $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){ //字節,不能是空白圖片 //創建縮略圖 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; //此處用布爾型即可,數據庫不可1.非比例縮略圖2.按比例縮略 if($img->GenerateThumbFile($cached_filename, $to_filename)){ echo "<span style='color:#CE0000;'>成功創建縮略圖:</span>".$row['id']." ".$row['url']; }else{ echo "<span style='color:#0000CE;'>未能創建縮略圖:</span>".$row['id']." ".$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']." ".$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代碼:
<?phpclass 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; } }}?>
六、總結
至此整個實現網頁截圖并創建縮略圖的的步驟結束,其中執行批處理文件部分為了提高截圖效率采用手動的方式,批量打開批處理文件,另外,鏈接數據庫部分還可以用封裝的數據庫操作類來實現,代碼會更加簡潔。
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選