婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能詳解

Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能詳解

熱門標簽:哪里辦理400電話 廣東地市地圖標注 江西手機自動外呼防封系統是什么 仁和怎么申請400開頭的電話 怎么向銷售公司推銷外呼系統 長春人工外呼系統服務商 外呼系統撥打暫時無法接通 高德地圖標注家 廣州防封卡外呼系統多少錢一個月

本文實例講述了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代碼:

?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;                         //創建 $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']."nbsp;".$row['url'];
        }else{
         echo "span style='color:#0000CE;'>未能創建縮略圖:/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;
  }
 }
}
?>

六、總結

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

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數學運算技巧總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》

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

您可能感興趣的文章:
  • php實現的支付寶網頁支付功能示例【基于TP5框架】
  • php實現網頁上一頁下一頁翻頁過程詳解
  • PHP 爬取網頁的主要方法
  • 實例分析基于PHP微信網頁獲取用戶信息
  • php實現網頁常見文件上傳功能
  • php中抓取網頁內容的實例詳解
  • php編程實現簡單的網頁版計算器功能示例
  • PHP網頁緩存技術優點及代碼實例

標簽:文山 廈門 濮陽 海北 黔東 梅河口 惠州 湘西

巨人網絡通訊聲明:本文標題《Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能詳解》,本文關鍵詞  Windows,平臺,PHP+IECapt,實現,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能詳解》相關的同類信息!
  • 本頁收集關于Windows平臺PHP+IECapt實現網頁批量截圖并創建縮略圖功能詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 太和县| 澄城县| 正阳县| 乡城县| 库尔勒市| 桂东县| 扶余县| 桓仁| 中西区| 花莲市| 衡南县| 崇礼县| 武清区| 辉县市| 乐东| 仪陇县| 保德县| 白城市| 新巴尔虎左旗| 大理市| 柳州市| 龙游县| 图木舒克市| 皋兰县| 沁水县| 比如县| 秦皇岛市| 鞍山市| 哈密市| 静宁县| 开阳县| 石台县| 拉萨市| 平泉县| 仁布县| 肇庆市| 尼木县| 贵南县| 隆德县| 息烽县| 闽侯县|