兼容性比较好的PHP生成缩略图的代码
代码如下:
function ImageResize($srcFile,$toW,$toH,$toFile="") { if($toFile==""){ $toFile = $srcFile; } $info = ""; $data = GetImageSize($srcFile,$info); switch ($data[2]) { case 1: if(!function_exists("imagecreatefromgif")){ echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; exit(); } $im = ImageCreateFromGIF($srcFile); break; case 2: if(!function_exists("imagecreatefromjpeg")){ echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; exit(); } $im = ImageCreateFromJpeg($srcFile); break; case 3: $im = ImageCreateFromPNG($srcFile); break; } $srcW=ImageSX($im); $srcH=ImageSY($im); $toWH=$toW/$toH; $srcWH=$srcW/$srcH; if($toWH<=$srcWH){ $ftoW=$toW; $ftoH=$ftoW*($srcH/$srcW); } else{ $ftoH=$toH; $ftoW=$ftoH*($srcW/$srcH); } if($srcW>$toW||$srcH>$toH) { if(function_exists("imagecreatetruecolor")){ @$ni = ImageCreateTrueColor($ftoW,$ftoH); if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); else{ $ni=ImageCreate($ftoW,$ftoH); ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); } }else{ $ni=ImageCreate($ftoW,$ftoH); ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); } if(function_exists('imagejpeg')) ImageJpeg($ni,$toFile); else ImagePNG($ni,$toFile); ImageDestroy($ni); } ImageDestroy($im); }
相关推荐
88236637 2020-08-19
长亭外一棵花菜 2020-06-14
ChaITSimpleLove 2020-06-09
dazhifu 2020-06-05
tianzyc 2020-06-05
加油奋斗吧 2020-05-31
大新哥Michael 2020-04-26
chunianyo 2020-04-11
lucialee 2020-03-04
RocketJ 2020-03-03
Web分享 2020-03-01
e度空间 2020-01-26
tangjianft 2020-01-05
微软FixIt 2015-07-05
Safari浏览器 2019-12-14