当前位置:首页 > PHP教程 > php图像处理 > 列表

php生成缩略图类

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:45:47 浏览: 评论:0 

,生成缩略图是当我们要上传图片时如果图片较大我们需要生成一张指定大小的小图以在列表页面显示,这样可节省资源也是现在常用的做法,下面我来分享一个php生成缩略图类,支持自定义高和宽,还支持按高和宽截图,php生成缩略图类的代码如下:

  1. class resizeimage   
  2. {   
  3.     //图片类型   
  4.     var $type;   
  5.     //实际宽度   
  6.     var $width;   
  7.     //实际高度   
  8.     var $height;   
  9.     //改变后的宽度   
  10.     var $resize_width;   
  11.     //改变后的高度   
  12.     var $resize_height;   
  13.     //是否裁图   
  14.     var $cut;   
  15.     //源图象   
  16.     var $srcimg;   
  17.     //目标图象地址   
  18.     var $dstimg;   
  19.     //临时创建的图象   
  20.     var $im;   
  21.     function resizeimage($img, $wid, $hei,$c,$dstpath)   
  22.     {   
  23.         $this->srcimg = $img;   
  24.         $this->resize_width = $wid;   
  25.         $this->resize_height = $hei;   
  26.         $this->cut = $c;   
  27.         //图片的类型  
  28.  
  29. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));   
  30.         //初始化图象   
  31.         $this->initi_img();   
  32.         //目标图象地址   
  33.         $this -> dst_img($dstpath);   
  34.         //--   
  35.         $this->width = imagesx($this->im);   
  36.         $this->height = imagesy($this->im);   
  37.         //生成图象   
  38.         $this->newimg();   
  39.         ImageDestroy ($this->im);   
  40.     }   
  41.     function newimg()   
  42.     {   
  43.         //改变后的图象的比例   
  44.         $resize_ratio = ($this->resize_width)/($this->resize_height);   
  45.         //实际图象的比例   
  46.         $ratio = ($this->width)/($this->height);   
  47.         if(($this->cut)=="1")   
  48.         //裁图   
  49.         {   
  50.             if($ratio>=$resize_ratio)   
  51.             //高度优先   
  52.             {   
  53.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);   
  54.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);   
  55.                 ImageJpeg ($newimg,$this->dstimg);   
  56.             }   
  57.             if($ratio<$resize_ratio)   
  58.             //宽度优先   
  59.             {   
  60.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);   
  61.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));   
  62.                 ImageJpeg ($newimg,$this->dstimg);   
  63.             }   
  64.         }   
  65.         else  
  66.         //不裁图   
  67.         {   
  68.             if($ratio>=$resize_ratio)   
  69.             {   
  70.                 $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);   
  71.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);   
  72.                 ImageJpeg ($newimg,$this->dstimg);   
  73.             }   
  74.             if($ratio<$resize_ratio)   
  75.             {   
  76.                 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);   
  77.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);   
  78.                 ImageJpeg ($newimg,$this->dstimg);   
  79.             }   
  80.         }   
  81.     }   
  82.     //初始化图象   
  83.     function initi_img()   
  84.     {   
  85.         if($this->type=="jpg")   
  86.         {   
  87.             $this->im = imagecreatefromjpeg($this->srcimg);   
  88.         }   
  89.         if($this->type=="gif")   
  90.         {   
  91.             $this->im = imagecreatefromgif($this->srcimg);   
  92.         }   
  93.         if($this->type=="png")   
  94.         {   
  95.             $this->im = imagecreatefrompng($this->srcimg);   
  96.         }   
  97.     }   
  98.     //图象目标地址   
  99.     function dst_img($dstpath)   
  100.     {   
  101.         $full_length  = strlen($this->srcimg);   
  102.         $type_length  = strlen($this->type);   
  103.         $name_length  = $full_length-$type_length;  
  104.  
  105.         $name         = substr($this->srcimg,0,$name_length-1);   
  106.         $this->dstimg = $dstpath;  
  107.  
  108. //echo $this->dstimg;   
  109.     }   
  110. }  
  111.  
  112. $resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg");   
  113. ?> 
  114.  
  115. //实例二,代码如下: 
  116.  
  117. <?php 
  118. /** 
  119.  * 生成缩略图 
  120.  * @author yangzhiguo0903@163.com 
  121.  * @param string     源图绝对完整地址{带文件名及后缀名} 
  122.  * @param string     目标图绝对完整地址{带文件名及后缀名} 
  123.  * @param int        缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)} 
  124.  * @param int        缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)} 
  125.  * @param int        是否裁切{宽,高必须非0} 
  126.  * @param int/float  缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)} 
  127.  * @return boolean 
  128.  */ 
  129. function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0) 
  130. { 
  131.     if(!is_file($src_img)) 
  132.     { 
  133.         return false; 
  134.     } 
  135.     $ot = fileext($dst_img); 
  136.     $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot); 
  137.     $srcinfo = getimagesize($src_img); 
  138.     $src_w = $srcinfo[0]; 
  139.     $src_h = $srcinfo[1]; 
  140.     $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1)); 
  141.     $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type); 
  142.  
  143.     $dst_h = $height; 
  144.     $dst_w = $width; 
  145.     $x = $y = 0; 
  146.  
  147.     /** 
  148.      * 缩略图不超过源图尺寸(前提是宽或高只有一个) 
  149.      */ 
  150.     if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0)) 
  151.     { 
  152.         $proportion = 1; 
  153.     } 
  154.     if($width> $src_w) 
  155.     { 
  156.         $dst_w = $width = $src_w; 
  157.     } 
  158.     if($height> $src_h) 
  159.     { 
  160.         $dst_h = $height = $src_h; 
  161.     } 
  162.  
  163.     if(!$width && !$height && !$proportion) 
  164.     { 
  165.         return false; 
  166.     } 
  167.     if(!$proportion) 
  168.     { 
  169.         if($cut == 0) 
  170.         { 
  171.             if($dst_w && $dst_h) 
  172.             { 
  173.                 if($dst_w/$src_w> $dst_h/$src_h) 
  174.                 { 
  175.                     $dst_w = $src_w * ($dst_h / $src_h); 
  176.                     $x = 0 - ($dst_w - $width) / 2; 
  177.                 } 
  178.                 else 
  179.                 { 
  180.                     $dst_h = $src_h * ($dst_w / $src_w); 
  181.                     $y = 0 - ($dst_h - $height) / 2; 
  182.                 } 
  183.             } 
  184.             else if($dst_w xor $dst_h) 
  185.             { 
  186.                 if($dst_w && !$dst_h)  //有宽无高 
  187.                 { 
  188.                     $propor = $dst_w / $src_w; 
  189.                     $height = $dst_h  = $src_h * $propor; 
  190.                 } 
  191.                 else if(!$dst_w && $dst_h)  //有高无宽 
  192.                 { 
  193.                     $propor = $dst_h / $src_h; 
  194.                     $width  = $dst_w = $src_w * $propor; 
  195.                 } 
  196.             } 
  197.         } 
  198.         else 
  199.         { 
  200.             if(!$dst_h)  //裁剪时无高 
  201.             { 
  202.                 $height = $dst_h = $dst_w; 
  203.             } 
  204.             if(!$dst_w)  //裁剪时无宽 
  205.             { 
  206.                 $width = $dst_w = $dst_h; 
  207.             } 
  208.             $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1); 
  209.             $dst_w = (int)round($src_w * $propor); 
  210.             $dst_h = (int)round($src_h * $propor); 
  211.             $x = ($width - $dst_w) / 2; 
  212.             $y = ($height - $dst_h) / 2; 
  213.         } 
  214.     } 
  215.     else 
  216.     { 
  217.         $proportion = min($proportion, 1); 
  218.         $height = $dst_h = $src_h * $proportion; 
  219.         $width  = $dst_w = $src_w * $proportion; 
  220.     } 
  221.  
  222.     $src = $createfun($src_img); 
  223.     $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h); 
  224.     $white = imagecolorallocate($dst, 255, 255, 255); 
  225.     imagefill($dst, 0, 0, $white); 
  226.  
  227.     if(function_exists('imagecopyresampled')) 
  228.     { 
  229.         imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 
  230.     } 
  231.     else 
  232.     { 
  233.         imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 
  234.     } 
  235.     $otfunc($dst, $dst_img); 
  236.     imagedestroy($dst); 
  237.     imagedestroy($src); 
  238.     return true; 
  239. } 
  240.   
  241. //使用方法,代码如下: 
  242. //开源代码phpfensi.com 
  243.  
  244. $src_img = "./ROSI_050_002.JPG"; 
  245. $dst_img = "./ROSI_050_002_thumb.jpg"; 
  246. $stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0); 
  247. if($stat){ 
  248.  echo 'Resize Image Success!<br />'; 
  249.  echo '<img src="'.$dst_img.'" />';  
  250. }else{ 
  251.  echo 'Resize Image Fail!';  
  252. } 

Tags: php生成缩略图类

分享到: