您好,欢迎访问一九零五行业门户网

Win+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程序循环执行 批处理文件,但在运行当中会出现很多问题,这里手动直接批量打开上面刚创建好的批处理文件,考虑到带宽和cpu,最多不要超过20个,截图的速度大约3-5秒/张效果如图三:
(图三)
五、创建缩略图
生成缩略图的文件是create_image_img.php,其中包含生成缩略图的主要的一个类文件是image.class.php,两个文件的代码如下:
ceate_image_img.php代码:
1 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 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 ?>
六、总结
  至此整个实现网页截图并创建缩略图的的步骤结束,其中执行批处理文件部分为了提高截图效率采用手动的方式,批量打开批处理文件,另外,链接数据库部分还可以用封装的数据库操作类来实现,代码会更加简洁。
其它类似信息

推荐信息