Win+PHP+IECapt完整實現網頁批量截圖並建立縮圖

來源:互聯網
上載者:User
最近在開發一個本地互連網應用的項目,為了增加使用者體驗,需要在搜尋結果左側顯示一所示的某個網站的縮圖效果,在網上不停地百度Google了一上午後,發現大多數實現少量還是可以的,如果大批量的總會在中途出現很多問題,最終也沒有發現十分滿意的程式,乾脆自己弄吧。



(圖一)

下面是在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代碼:

 1  0){     //每30條為一個批次檔31               $title = "title capt".$i.".bat\r\n";32               $str = $title.$str;      33               $file_bat = fopen("img_tmp/capt".$i.".bat","w");              34 35               if(fwrite($file_bat,$str)){36                 echo "批次檔capt".$i."產生成功
";37 $str = "";38 } 39 } 40 $i = $i+1;41 } 42 } 43 ?>

運行結果:

(圖二)

四、執行批次檔

可以通過php程式迴圈執行 批次檔,但在運行當中會出現很多問題,這裏手動直接批量開啟上面剛建立好的批次檔,考慮到頻寬和cpu,最多不要超過20個,的速度大約3-5秒/張效果三:


(圖三)

五、建立縮圖

產生縮圖的檔案是create_image_img.php,其中包含產生縮圖的主要的一個類檔案是image.class.php,兩個檔案的代碼如下:

ceate_image_img.php代碼:

 1 正在產生縮圖:".$row['id']." ".$row['url']."

"; 16 17 $url = $row['url']; 18 $url_md5 = md5($url);19 $pictype = $row['pictype']; 20 21 $limit_time = 1; //建立 $limit_time日內建立的大圖,天22 $thumbnails_folder = 'img_tmp/'; //儲存臨時大圖的目錄,必須以/結束23 $thumbnails_folder2 = 'img/'; //儲存小圖的目錄,必須以/結束 24 $output_format = 'jpg'; 25 $cached_filename = $thumbnails_folder.$url_md5.".".$output_format;26 27 $to_filename = $thumbnails_folder2 .$url_md5.'.'.$output_format; 28 29 if((file_exists($cached_filename) || filemtime ($filename) + $limit_time*86400 > time())30 && !file_exists($to_filename)){ 31 32 if (filesize($cached_filename) > 1024){ //位元組,不能是空白圖片33 //建立縮圖34 include("image.class.php");35 $img = new Zubrag_image; 36 37 // get parameters38 $img->image_type = 2; // 1 = GIF, 2 = JPG, 3 = PNG39 $img->quality = 80;40 $img->max_w = 90;41 $img->max_h = 67;42 $img->iscapt = ($pictype == 1) ? true : false; //此處用布爾型即可,資料庫不可1.非比例縮圖2.按比例縮減 43 44 if($img->GenerateThumbFile($cached_filename, $to_filename)){ 45 echo "成功建立縮圖:".$row['id']." ".$row['url']; 46 }else{ 47 echo "未能建立縮圖:".$row['id']." ".$row['url'];48 } 49 } 50 } 51 52 $sql = "select * from t_url id >".$_GET['ID']." and flag = 1 order by id asc limit 1";53 $query = mysql_query($sql);54 $row = mysql_fetch_array($query); 55 56 echo "
準備產生縮圖:".$row['id']." ".$row['url']."

"; 57 58 if($row['id']){ 59 echo "";60 }else{61 $_GET['ID'] = "";62 } 63 } 64 65 ?>

image.class.php代碼:

 1 image_type == 1)  && !function_exists('imagegif')) $this->image_type = 3; 14     switch ($this->image_type) {15       case 1:16         //if ($this->save_to_file) {17           $res = ImageGIF($im,$filename);18         //}19         //else {20         //  header("Content-type: image/gif");21         //  $res = ImageGIF($im);22         //}23         break;24       case 2:       25           $res = ImageJPEG($im,$filename,$this->quality);      26         break;27       case 3:       28           $res = ImagePNG($im,$filename);        29         break;30     }  31     return $res;32   } 33 34   function ImageCreateFromType($type,$filename) {35      $im = NULL;36      switch ($type) {37        case 1:38          $im = ImageCreateFromGif($filename);39          break;40        case 2:41          $im = ImageCreateFromJpeg($filename);42          break;43        case 3:44          $im = ImageCreateFromPNG($filename);45          break;46     }47     return $im;48   }49 50 51   function GenerateThumbFile($from_name, $to_name) {    52     list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($from_name);  53     /*if ($this->cut_x > 0) $orig_x = min($this->cut_x, $orig_x);54     if ($this->cut_y > 0) $orig_y = min($this->cut_y, $orig_y);*/55        if ($this->iscapt && (($orig_y/$orig_x) > (90/67))) {  //是,且高度過高   56           $orig_y = $orig_x*(67/90);          57        }58 59     $this->image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type);   60 61     if ($orig_img_type < 1 or $orig_img_type > 3) die("Image type not supported");      62 63     if ($this->image_type == 1) {     64       $ni = imagecreate($this->max_w, $this->max_h);65     }66     else {     67       $ni = imagecreatetruecolor($this->max_w,$this->max_h);68     } 69 70     $white = imagecolorallocate($ni, 255, 255, 255);71     imagefilledrectangle( $ni, 0, 0, $this->max_w, $this->max_h, $white);        72 73     $im = $this->ImageCreateFromType($orig_img_type,$from_name);   74     imagepalettecopy($ni,$im);   75     imagecopyresampled(76       $ni, $im,             77       0, 0, 0, 0,          78       $this->max_w, $this->max_h,   79       $orig_x, $orig_y);       80     if($this->SaveImage($ni, $to_name)){81          return true;82     }else{83          return false;84     }85   } 86 } 87 88 ?>

六、總結

至此整個實現網頁並建立縮圖的的步驟結束,其中執行批次檔部分為了提高效率採用手動的方式,批量開啟批次檔,另外,連結資料庫部分還可以用封裝的資料庫操作類來實現,代碼會更加簡潔。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.