Windows平台PHP+IECapt实现网页批量截图并创建缩略图(3)

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程序设计有所帮助。