www.adminn.cn
站长正能量分享网!

php生成缩略图代码

AD:阿里云服务器企业会员更优惠 腾讯云香港,韩国免备案服务器1.8折优惠
  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. ?> 

模板优惠价: (点击购买)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《php生成缩略图代码》
文章链接:https://www.adminn.cn/news/8109.html
本站资源模板仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。2021.5月起,网站调整,暂不再分享免费模板。谢谢理解

Adminn.Cn 站长分享圈

帝国CMS精品模板腾讯云优惠券,代金券

2次开发,学习研究使用!扫码支付后联系QQ677123发货!

支付宝扫一扫打赏