国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Windows平臺PHP+IECapt實現(xiàn)網(wǎng)頁批量截圖并創(chuàng)建縮略圖功能詳解

2024-05-05 00:10:41
字體:
供稿:網(wǎng)友

本文實例講述了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)十分滿意的程序,干脆自己弄吧。

Windows,PHP,IECapt,網(wǎng)頁批量截圖

(圖一)

下面是在windows環(huán)境下用php結(jié)合iecapt實現(xiàn)的網(wǎng)頁截圖并創(chuàng)建縮略圖的步驟和代碼:

一、準(zhǔn)備

下載最新版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.ay360.cn/ --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é)果:

Windows,PHP,IECapt,網(wǎng)頁批量截圖

(圖二)

四、執(zhí)行批處理文件

可以通過php程序循環(huán)執(zhí)行 批處理文件,但在運行當(dāng)中會出現(xiàn)很多問題,這里手動直接批量打開上面剛創(chuàng)建好的批處理文件,考慮到帶寬和cpu,最多不要超過20個,截圖的速度大約3-5秒/張效果如圖三:

Windows,PHP,IECapt,網(wǎng)頁批量截圖

(圖三)

五、創(chuàng)建縮略圖

  生成縮略圖的文件是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;                         //創(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']." ".$row['url'];        }else{         echo "<span style='color:#0000CE;'>未能創(chuàng)建縮略圖:</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;'>準(zhǔn)備生成縮略圖:</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;  } }}?>

六、總結(jié)

至此整個實現(xiàn)網(wǎng)頁截圖并創(chuàng)建縮略圖的的步驟結(jié)束,其中執(zhí)行批處理文件部分為了提高截圖效率采用手動的方式,批量打開批處理文件,另外,鏈接數(shù)據(jù)庫部分還可以用封裝的數(shù)據(jù)庫操作類來實現(xiàn),代碼會更加簡潔。

希望本文所述對大家PHP程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 洮南市| 巴林右旗| 木里| 信阳市| 哈尔滨市| 海晏县| 礼泉县| 昭苏县| 榆社县| 漯河市| 东丽区| 柳河县| 墨脱县| 宁波市| 浪卡子县| 定兴县| 宁海县| 周口市| 宜章县| 渭南市| 安顺市| 广宁县| 车致| 涿鹿县| 鄂托克前旗| 屏山县| 福建省| 黑龙江省| 黎川县| 郧西县| 达拉特旗| 武山县| 安龙县| 横峰县| 商洛市| 廊坊市| 区。| 久治县| 同心县| 阳西县| 黑水县|