佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Windows平臺(tái)PHP+IECapt實(shí)現(xiàn)網(wǎng)頁(yè)批量截圖并創(chuàng)建縮略圖功能詳解

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

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 服務(wù)器配置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器

本文實(shí)例講述了Windows平臺(tái)PHP+IECapt實(shí)現(xiàn)網(wǎng)頁(yè)批量截圖并創(chuàng)建縮略圖功能。分享給大家供大家參考,具體如下:

最近在開(kāi)發(fā)一個(gè)本地互聯(lián)網(wǎng)應(yīng)用的項(xiàng)目,為了增加用戶體驗(yàn),需要在搜索結(jié)果左側(cè)顯示如圖一所示的某個(gè)網(wǎng)站的縮略圖效果,在網(wǎng)上不停地百度谷歌了一上午后,發(fā)現(xiàn)大多數(shù)實(shí)現(xiàn)少量截圖還是可以的,如果大批量的截圖總會(huì)在中途出現(xiàn)很多問(wèn)題,最終也沒(méi)有發(fā)現(xiàn)十分滿意的程序,干脆自己弄吧。

(圖一)

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

一、準(zhǔn)備

下載最新版IECapt

官方地址:http://iecapt.sourceforge.net/

在linux環(huán)境下,可以考慮用HTML2Image來(lái)實(shí)現(xiàn)

下載地址:http://www.guangmingsoft.net/htmlsnapshot/html2image.i386.tar.gz

其它的實(shí)現(xiàn)方式還有CutyCapt,另外,只要是windows環(huán)境,有IE瀏覽器(推薦使用IE7)即可,這個(gè)大部分機(jī)器都應(yīng)該不是問(wèn)題。

二、創(chuàng)建數(shù)據(jù)表(這一步非必須,根據(jù)實(shí)際情況選用)

因?yàn)橐拷貓D,數(shù)據(jù)十分的多,建立一個(gè)數(shù)據(jù)表來(lái)存放要截圖的網(wǎng)站的url地址還是有必要的,如下所示(mysql數(shù)據(jù)庫(kù)表):

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)建一個(gè)test.bat文件,鼠標(biāo)右擊編輯,寫入一句話if not exist ay360cn.jpg (iecapt.exe --url=http://www.ay#/ --out=ay360cn.jpg)保存,雙擊運(yùn)行test.bat看看是否會(huì)在本目錄下多出一個(gè)名叫ay360cn.jpg的文件,如果看到說(shuō)明截圖成功,這句話是截圖的核心語(yǔ)句。

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語(yǔ)句$sql2 = "select * from t_url where pictype = 1 and flag = 1";
$query = mysql_query($sql);
//------------------------------------------
//生成批處理文件
//------------------------------------------
$expire_time = 10;  //代表10天,文件過(guò)期時(shí)間,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條為一個(gè)批處理文件
       $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;
 }
}
?>

運(yùn)行結(jié)果:

(圖二)

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

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

(圖三)

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

  生成縮略圖的文件是create_image_img.php,其中包含生成縮略圖的主要的一個(gè)類文件是image.class.php,兩個(gè)文件的代碼如下:

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/';             //保存臨時(shí)大圖的目錄,必須以/結(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ù)庫(kù)不可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;'>準(zhǔn)備生成縮略圖:/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))) { //是截圖,且高度過(guò)高
     $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é)

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

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

您可能感興趣的文章:
  • php實(shí)現(xiàn)的支付寶網(wǎng)頁(yè)支付功能示例【基于TP5框架】
  • php實(shí)現(xiàn)網(wǎng)頁(yè)上一頁(yè)下一頁(yè)翻頁(yè)過(guò)程詳解
  • PHP 爬取網(wǎng)頁(yè)的主要方法
  • 實(shí)例分析基于PHP微信網(wǎng)頁(yè)獲取用戶信息
  • php實(shí)現(xiàn)網(wǎng)頁(yè)常見(jiàn)文件上傳功能
  • php中抓取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解
  • php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)版計(jì)算器功能示例
  • PHP網(wǎng)頁(yè)緩存技術(shù)優(yōu)點(diǎn)及代碼實(shí)例

標(biāo)簽:江蘇 廣元 棗莊 衡水 衢州 萍鄉(xiāng) 大理 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Windows平臺(tái)PHP+IECapt實(shí)現(xiàn)網(wǎng)頁(yè)批量截圖并創(chuàng)建縮略圖功能詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    独山县| 贵港市| 施甸县| 阿拉善盟| 周口市| 盐边县| 腾冲县| 农安县| 佛学| 台江县| 宁化县| 内丘县| 新晃| 永福县| 屯留县| 阿坝县| 云霄县| 昌吉市| 鲜城| 鄂托克前旗| 黄陵县| 台湾省| 云霄县| 名山县| 东方市| 江安县| 美姑县| 德兴市| 邹平县| 赣州市| 北安市| 四子王旗| 呼和浩特市| 砀山县| 浮山县| 吴旗县| 长乐市| 扎囊县| 阿拉善左旗| 衡东县| 黄大仙区|