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

php生成缩略图代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-20 09:57:42 浏览: 评论:0 
  1. <?php
  2. /** 
  3. * 生成缩略图 
  4. * $srcName----为原图片路径 
  5. * $newWidth,$newHeight----分别缩略图的最大宽,高 
  6. * $newName----为缩略图文件名(含路径),默认为加入thumbnail 
  7. * @param string $srcName 
  8. * @param int $newWidth 
  9. * @param int $newHeight 
  10. * @param string $newName 
  11. * return viod 
  12. */ 
  13. function resizeImg($srcName,$newWidth,$newHeight,$newName=""
  14.         if($newName==""
  15.         { 
  16.                 $nameArr=explode('.',$srcName); 
  17.                 $expName=array_pop($nameArr); 
  18.                 $expName='thumbnail.'.$expName
  19.                 array_push($nameArr,$expName); 
  20.                 $newName = implode('.',$nameArr); 
  21.         } 
  22.         $info = ""
  23.         $data = getimagesize($srcName,$info); 
  24.         switch ($data[2]) 
  25.         { 
  26.                 case 1: 
  27.                         if(!function_exists("imagecreatefromgif")){ 
  28.                                 echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回"
  29.                                 exit(); 
  30.                         } 
  31.                         $im = ImageCreateFromGIF($srcName); 
  32.                         break
  33.                 case 2: 
  34.                         if(!function_exists("imagecreatefromjpeg")){ 
  35.                                 echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回"
  36.                                 exit(); 
  37.                         } 
  38.                         $im = ImageCreateFromJpeg($srcName); 
  39.                         break
  40.                 case 3: 
  41.                         $im = ImageCreateFromPNG($srcName); 
  42.                         break
  43.         } 
  44.         $srcW=ImageSX($im); 
  45.         $srcH=ImageSY($im); 
  46.         $newWidthH=$newWidth/$newHeight
  47.         $srcWH=$srcW/$srcH
  48.         if($newWidthH<=$srcWH){ 
  49.                 $ftoW=$newWidth
  50.                 $ftoH=$ftoW*($srcH/$srcW); 
  51.         } 
  52.         else
  53.                 $ftoH=$newHeight
  54.                 $ftoW=$ftoH*($srcW/$srcH); 
  55.         } 
  56.         if($srcW>$newWidth||$srcH>$newHeight
  57.         { 
  58.                 if(function_exists("imagecreatetruecolor")) 
  59.                 { 
  60.                         @$ni = ImageCreateTrueColor($ftoW,$ftoH); 
  61.                         if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 
  62.                         else 
  63.                         { 
  64.                                 $ni=ImageCreate($ftoW,$ftoH); 
  65.                                 ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 
  66.                         } 
  67.                 } 
  68.                 else 
  69.                 { 
  70.                         $ni=ImageCreate($ftoW,$ftoH); 
  71.                         ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 
  72.                 } 
  73.                 if(function_exists('imagejpeg')) ImageJpeg($ni,$newName); 
  74.                 else ImagePNG($ni,$newName); 
  75.                 ImageDestroy($ni); 
  76.         } 
  77.         ImageDestroy($im); 
  78. //开源代码phpfensi.com 
  79. resizeImg('123.JPG',150,150); 
  80. ?> 

Tags: PHP生成缩略图 实现程序

分享到: