search
HomeBackend DevelopmentPHP TutorialWin+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代码:  

 1 <?php 2  3 //------------------------------------------------------------ 4 //从表t_url中提取url链接,存放到数组$data中 5 //-------------------------------------------------------------- 6   7 mysql_connect("localhost","root","123"); 8 mysql_select_db("test"); 9 $sql = "select * from t_url";10 //选用sql语句$sql2 = "select * from t_url where pictype = 1 and flag = 1";11 12 $query = mysql_query($sql); 13 14 //------------------------------------------15 //生成批处理文件16 //------------------------------------------17 18 $expire_time = 10;   //代表10天,文件过期时间,86400秒/天19 $i = 0;20 foreach($row = mysql_fetch_array($query)){21   $url_md5 = md5($row['url']);22   $file_folder = 'img/';23   $filename = $file_folder.$url_md5.'.'.'jpg';24   $newname = $url_md5.'.'.'jpg'; 25 26   if (!file_exists($filename) || (filemtime ($filename) + $expire_time * 86400 < time()) ) {   27 28        $str .= "if not exist ".$newname." (iecapt.exe --url=".$value['url']." --out=".$newname.")\r\n";             29 30        if(($i % 30) == 0 && $i > 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."生成成功<br>";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 <?php  2 mysql_connect("localhost","root","123456"); 3 mysql_select_db("test"); 4   5  6 if(!isset($_GET['ID'])){ 7   $_GET['ID'] = 1; 8 }  9 10 if($_GET['ID']){   11   $sql = "select * from t_url id =".$_GET['ID'];12   $query = mysql_query($sql);13   $row = mysql_fetch_array($query); 14 15   echo "<span style='color:#CE0000;'>正在生成缩略图:</span>".$row['id']." ".$row['url']."<br><br>"; 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 "<span style='color:#CE0000;'>成功创建缩略图:</span>".$row['id']." ".$row['url'];   46                 }else{       47                   echo "<span style='color:#0000CE;'>未能创建缩略图:</span>".$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 "<br><span style='color:#0000CE;'>准备生成缩略图:</span>".$row['id']." ".$row['url']."<br><br>"; 57 58   if($row['id']){    59     echo "<script>window.location.href='create_image_img.php?ID=".$row['id']."';</script>";60   }else{61     $_GET['ID'] = "";62   }    63 } 64 65 ?>

image.class.php代码:

 1 <?php 2  3 class Zubrag_image {  4  5   var $iscapt = true; 6   var $image_type = -1; 7   var $quality = 100; 8   var $max_w = 100; 9   var $max_h = 100; 10 11   function SaveImage($im, $filename) { 12     $res = null;    13     if(($this->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 ?>

六、总结

  至此整个实现网页截图并创建缩略图的的步骤结束,其中执行批处理文件部分为了提高截图效率采用手动的方式,批量打开批处理文件,另外,链接数据库部分还可以用封装的数据库操作类来实现,代码会更加简洁。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),