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

php生成图片缩略图类程序

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

缩略图用得最多的是我们上传图片时生成一张小图,这样就可以很好的解决图片大小或影响网站整体美观的问题了,下面介绍的生成图片缩略图类都不支持图片上传,我们要先上传再调用此类来操作.

使用如下类就可以生成图片缩略图,代码如下:

  1. <?php 
  2. class resizeimage 
  3. { 
  4.     //图片类型 
  5.     var $type; 
  6.     //实际宽度 
  7.     var $width; 
  8.     //实际高度 
  9.     var $height; 
  10.     //改变后的宽度 
  11.     var $resize_width; 
  12.     //改变后的高度 
  13.     var $resize_height; 
  14.     //是否裁图 
  15.     var $cut; 
  16.     //源图象 
  17.     var $srcimg; 
  18.     //目标图象地址 
  19.     var $dstimg; 
  20.     //临时创建的图象 
  21.     var $im; 
  22.  
  23.     function resizeimage($img, $wid, $hei,$c,$dstpath) 
  24.     { 
  25.         $this->srcimg = $img; 
  26.         $this->resize_width = $wid; 
  27.         $this->resize_height = $hei; 
  28.         $this->cut = $c; 
  29.         //图片的类型 
  30.     
  31. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
  32.  
  33.         //初始化图象 
  34.         $this->initi_img(); 
  35.         //目标图象地址 
  36.         $this -> dst_img($dstpath); 
  37.         //-- 
  38.         $this->width = imagesx($this->im); 
  39.         $this->height = imagesy($this->im); 
  40.         //生成图象 
  41.         $this->newimg(); 
  42.         ImageDestroy ($this->im); 
  43.     } 
  44.     function newimg() 
  45.     { 
  46.         //改变后的图象的比例 
  47.         $resize_ratio = ($this->resize_width)/($this->resize_height); 
  48.         //实际图象的比例 
  49.         $ratio = ($this->width)/($this->height); 
  50.         if(($this->cut)=="1") 
  51.         //裁图 
  52.         { 
  53.             if($ratio>=$resize_ratio) 
  54.             //高度优先 
  55.             { 
  56.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  57.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this- 
  58.  
  59. >resize_height, (($this->height)*$resize_ratio), $this->height); 
  60.                 ImageJpeg ($newimg,$this->dstimg); 
  61.             } 
  62.             if($ratio<$resize_ratio) 
  63.             //宽度优先 
  64.             { 
  65.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  66.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this- 
  67.  
  68. >resize_height, $this->width, (($this->width)/$resize_ratio)); 
  69.                 ImageJpeg ($newimg,$this->dstimg); 
  70.             } 
  71.         } 
  72.         else 
  73.         //不裁图 
  74.         { 
  75.             if($ratio>=$resize_ratio) 
  76.             { 
  77.                 $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
  78.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this- 
  79.  
  80. >resize_width)/$ratio, $this->width, $this->height); 
  81.                 ImageJpeg ($newimg,$this->dstimg); 
  82.             } 
  83.             if($ratio<$resize_ratio) 
  84.             { 
  85.                 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
  86.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, 
  87. //开源代码phpfensi.com 
  88. $this->resize_height, $this->width, $this->height); 
  89.                 ImageJpeg ($newimg,$this->dstimg); 
  90.             } 
  91.         } 
  92.     } 
  93.     //初始化图象 
  94.     function initi_img() 
  95.     { 
  96.         if($this->type=="jpg") 
  97.         { 
  98.             $this->im = imagecreatefromjpeg($this->srcimg); 
  99.         } 
  100.         if($this->type=="gif") 
  101.         { 
  102.             $this->im = imagecreatefromgif($this->srcimg); 
  103.         } 
  104.         if($this->type=="png") 
  105.         { 
  106.             $this->im = imagecreatefrompng($this->srcimg); 
  107.         } 
  108.     } 
  109.     //图象目标地址 
  110.     function dst_img($dstpath) 
  111.     { 
  112.         $full_length  = strlen($this->srcimg); 
  113.  
  114.         $type_length  = strlen($this->type); 
  115.         $name_length  = $full_length-$type_length; 
  116.  
  117.  
  118.         $name         = substr($this->srcimg,0,$name_length-1); 
  119.         $this->dstimg = $dstpath; 
  120.  
  121.  
  122. //echo $this->dstimg; 
  123.     } 
  124. } 
  125. ?> 

类的调用方法:

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");

就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高.

实例2:PHP缩略图 等比例无损压缩,可填充空白区域补充色,代码如下:

  1. <?php 
  2. error_reporting( E_ALL ); 
  3.  
  4. // 测试 
  5. imagezoom('1.jpg', '2.jpg', 400, 300, '#FFFFFF'); 
  6.  
  7. /* 
  8.     php缩略图函数: 
  9.         等比例无损压缩,可填充补充色 author: 华仔 
  10.     主持格式: 
  11.         bmp 、jpg 、gif、png 
  12.     param: 
  13.         @srcimage : 要缩小的图片 
  14.         @dstimage : 要保存的图片 
  15.         @dst_width: 缩小宽 
  16.         @dst_height: 缩小高 
  17.         @backgroundcolor: 补充色  如:#FFFFFF  支持 6位  不支持3位 
  18. */ 
  19. function imagezoom( $srcimage, $dstimage,  $dst_width, $dst_height, $backgroundcolor ) { 
  20.          
  21.         // 中文件名乱码 
  22.         if ( PHP_OS == 'WINNT' ) { 
  23.                 $srcimage = iconv('UTF-8', 'GBK', $srcimage); 
  24.                 $dstimage = iconv('UTF-8', 'GBK', $dstimage); 
  25.         } 
  26.      
  27.     $dstimg = imagecreatetruecolor( $dst_width, $dst_height ); 
  28.     $color = imagecolorallocate($dstimg 
  29.         , hexdec(substr($backgroundcolor, 1, 2)) 
  30.         , hexdec(substr($backgroundcolor, 3, 2)) 
  31.         , hexdec(substr($backgroundcolor, 5, 2)) 
  32.     ); 
  33.     imagefill($dstimg, 0, 0, $color); 
  34.      
  35.     if ( !$arr=getimagesize($srcimage) ) { 
  36.                 echo "要生成缩略图的文件不存在"; 
  37.                 exit; 
  38.         } 
  39.          
  40.     $src_width = $arr[0]; 
  41.     $src_height = $arr[1]; 
  42.     $srcimg = null; 
  43.     $method = getcreatemethod( $srcimage ); 
  44.     if ( $method ) { 
  45.         eval( '$srcimg = ' . $method . ';' ); 
  46.     } 
  47.      
  48.     $dst_x = 0; 
  49.     $dst_y = 0; 
  50.     $dst_w = $dst_width; 
  51.     $dst_h = $dst_height; 
  52.     if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) { 
  53.         $dst_w = $src_width * ( $dst_height / $src_height ); 
  54.         $dst_x = ( $dst_width - $dst_w ) / 2; 
  55.     } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) { 
  56.         $dst_h = $src_height * ( $dst_width / $src_width ); 
  57.         $dst_y = ( $dst_height - $dst_h ) / 2; 
  58.     } 
  59.  
  60.     imagecopyresampled($dstimg, $srcimg, $dst_x 
  61.         , $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height); 
  62.      
  63.     // 保存格式 
  64.     $arr = array( 
  65.         'jpg' => 'imagejpeg' 
  66.         , 'jpeg' => 'imagejpeg' 
  67.         , 'png' => 'imagepng' 
  68.         , 'gif' => 'imagegif' 
  69.         , 'bmp' => 'imagebmp' 
  70.     ); 
  71.     $suffix = strtolower( array_pop(explode('.', $dstimage ) ) ); 
  72.     if (!in_array($suffix, array_keys($arr)) ) { 
  73.         echo "保存的文件名错误"; 
  74.         exit; 
  75.     } else { 
  76.         eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' ); 
  77.     } 
  78.      
  79.     imagejpeg($dstimg, $dstimage); 
  80.      
  81.     imagedestroy($dstimg); 
  82.     imagedestroy($srcimg); 
  83.      
  84. } 
  85.  
  86.  
  87. function getcreatemethod( $file ) { 
  88.         $arr = array( 
  89.                 '474946' => "imagecreatefromgif('$file')" 
  90.                 , 'FFD8FF' => "imagecreatefromjpeg('$file')" 
  91.                 , '424D' => "imagecreatefrombmp('$file')" 
  92.                 , '89504E' => "imagecreatefrompng('$file')" 
  93.         ); 
  94.         $fd = fopen( $file, "rb" ); 
  95.         $data = fread( $fd, 3 ); 
  96.          
  97.         $data = str2hex( $data ); 
  98.          
  99.         if ( array_key_exists( $data, $arr ) ) { 
  100.                 return $arr[$data]; 
  101.         } elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) { 
  102.                 return $arr[substr($data, 0, 4)]; 
  103.         } else { 
  104.                 return false; 
  105.         } 
  106. } 
  107.  
  108. function str2hex( $str ) { 
  109.         $ret = ""; 
  110.          
  111.         for( $i = 0; $i < strlen( $str ) ; $i++ ) { 
  112.                 $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) ) 
  113.                         : '0'. strval( dechex( ord($str[$i]) ) ); 
  114.         } 
  115.          
  116.         return strtoupper( $ret ); 
  117. } 
  118.  
  119. // BMP 创建函数  php本身无 
  120. function imagecreatefrombmp($filename) 
  121. { 
  122.    if (! $f1 = fopen($filename,"rb")) return FALSE; 
  123.  
  124.    $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14)); 
  125.    if ($FILE['file_type'] != 19778) return FALSE; 
  126.  
  127.    $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. 
  128.                  '/Vcompression/Vsize_bitmap/Vhoriz_resolution'. 
  129.                  '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40)); 
  130.    $BMP['colors'] = pow(2,$BMP['bits_per_pixel']); 
  131.    if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; 
  132.    $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; 
  133.    $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']); 
  134.    $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4); 
  135.    $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4); 
  136.    $BMP['decal'] = 4-(4*$BMP['decal']); 
  137.    if ($BMP['decal'] == 4) $BMP['decal'] = 0; 
  138.     
  139.    $PALETTE = array(); 
  140.    if ($BMP['colors'] < 16777216) 
  141.    { 
  142.     $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4)); 
  143.    } 
  144.     
  145.    $IMG = fread($f1,$BMP['size_bitmap']); 
  146.    $VIDE = chr(0); 
  147.  
  148.    $res = imagecreatetruecolor($BMP['width'],$BMP['height']); 
  149.    $P = 0; 
  150.    $Y = $BMP['height']-1; 
  151.    while ($Y >= 0) 
  152.    { 
  153.         $X=0; 
  154.         while ($X < $BMP['width']) 
  155.         { 
  156.          if ($BMP['bits_per_pixel'] == 24) 
  157.             $COLOR = unpack("V",substr($IMG,$P,3).$VIDE); 
  158.          elseif ($BMP['bits_per_pixel'] == 16) 
  159.          {   
  160.             $COLOR = unpack("n",substr($IMG,$P,2)); 
  161.             $COLOR[1] = $PALETTE[$COLOR[1]+1]; 
  162.          } 
  163.          elseif ($BMP['bits_per_pixel'] == 8) 
  164.          {   
  165.             $COLOR = unpack("n",$VIDE.substr($IMG,$P,1)); 
  166.             $COLOR[1] = $PALETTE[$COLOR[1]+1]; 
  167.          } 
  168.          elseif ($BMP['bits_per_pixel'] == 4) 
  169.          { 
  170.             $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); 
  171.             if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F); 
  172.             $COLOR[1] = $PALETTE[$COLOR[1]+1]; 
  173.          } 
  174.          elseif ($BMP['bits_per_pixel'] == 1) 
  175.          { 
  176.             $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); 
  177.             if     (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7; 
  178.             elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6; 
  179.             elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5; 
  180.             elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4; 
  181.             elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3; 
  182.             elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2; 
  183.             elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1; 
  184.             elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1); 
  185.             $COLOR[1] = $PALETTE[$COLOR[1]+1]; 
  186.          } 
  187.          else 
  188.             return FALSE; 
  189.          imagesetpixel($res,$X,$Y,$COLOR[1]); 
  190.          $X++; 
  191.          $P += $BMP['bytes_per_pixel']; 
  192.         } 
  193.         $Y--; 
  194.         $P+=$BMP['decal']; 
  195.    } 
  196.    fclose($f1); 
  197.  
  198. return $res; 
  199. } 
  200. // BMP 保存函数,php本身无 
  201. function imagebmp ($im, $fn = false) 
  202. { 
  203.     if (!$im) return false; 
  204.              
  205.     if ($fn === false) $fn = 'php://output'; 
  206.     $f = fopen ($fn, "w"); 
  207.     if (!$f) return false; 
  208.              
  209.     $biWidth = imagesx ($im); 
  210.     $biHeight = imagesy ($im); 
  211.     $biBPLine = $biWidth * 3; 
  212.     $biStride = ($biBPLine + 3) & ~3; 
  213.     $biSizeImage = $biStride * $biHeight; 
  214.     $bfOffBits = 54; 
  215.     $bfSize = $bfOffBits + $biSizeImage; 
  216.              
  217.     fwrite ($f, 'BM', 2); 
  218.     fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits)); 
  219.              
  220.     fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0)); 
  221.              
  222.     $numpad = $biStride - $biBPLine; 
  223.     for ($y = $biHeight - 1; $y >= 0; --$y) 
  224.     { 
  225.         for ($x = 0; $x < $biWidth; ++$x) 
  226.         { 
  227.             $col = imagecolorat ($im, $x, $y); 
  228.             fwrite ($f, pack ('V', $col), 3); 
  229.         } 
  230.         for ($i = 0; $i < $numpad; ++$i) 
  231.             fwrite ($f, pack ('C', 0)); 
  232.     } 
  233.     fclose ($f); 
  234.     return true; 
  235. } 
  236. ?> 

总结,第一个类文件不如第二个好,因为第一个生成图之后图片会有点变模糊,后面这个生成类是高质量的.

Tags: php生成图片 缩略图类

分享到: