css缩略图 原创dedecms缩略图系列教程:解决dedecms缩略图缩放变形问题

软件教程 2024-02-13 07:02:28

拷贝001if(! function_exists(ImageResize))002{003functionImageResize($srcFile,$toW,$toH,$toFile="")004{005global $cfg_photo_type;006if($toFile=="")007{008 $toFile=$srcFile;009}010 $info="";011 $srcInfo=GetImageSize($srcFile,$info);012switch($srcInfo[2])013{014case1:015if(!$cfg_photo_type[gif])016{017returnfalse;018}019 $im=imagecreatefromgif($srcFile);020break;021case2:022if(!$cfg_photo_type[jpeg])023{024returnfalse;025}026 $im=imagecreatefromjpeg($srcFile);027break;028case3:029if(!$cfg_photo_type[png])030{031returnfalse;032}033 $im=imagecreatefrompng($srcFile);034break;035case6:036if(!$cfg_photo_type[bmp])037{038returnfalse;039}040 $im=imagecreatefromwbmp($srcFile);041break;042}043 $srcW=ImageSX($im);044 $srcH=ImageSY($im);045if($srcW<=$toW && $srcH<=$toH )046{047returntrue;048}049//缩略生成并裁剪050 $newW=$toH *$srcW/ $srcH;051 $newH=$toW * $srcH / $srcW;052if($newH >=$toH)053{054 $ftoW=$toW;055 $ftoH=$newH;056}057else058{059 $ftoW=$newW;060 $ftoH=$toH;061}062if($srcW>$toW||$srcH>$toH)063{064if(function_exists("imagecreatetruecolor"))065{066@$ni=imagecreatetruecolor($ftoW,$ftoH);067if($ni)068{069 imagecopyresampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);070}071else072{073 $ni=imagecreate($ftoW,$ftoH);074imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);075}076}077else078{079 $ni=imagecreate($ftoW,$ftoH);080 imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);081}082//裁剪图片成标准纯文字083 $new_imgx=imagecreatetruecolor($toW,$toH);084if($newH >=$toH)085{086 imagecopyresampled($new_imgx,$ni,0,0,0,($newH - $toH)/2,$toW,$toH,$toW,$toH);087}088else089{090 imagecopyresampled($new_imgx,$ni,0,0,($newW - $toW)/2,0,$toW,$toH,$toW,$toH);091}092switch($srcInfo[2])093{094case1:095 imagegif($new_imgx,$toFile);096break;097case2:098 imagejpeg($new_imgx,$toFile,85);099break;100case3:101 imagepng($new_imgx,$toFile);102break;103case6:104 imagebmp($new_imgx,$toFile);105break;106default:107returnfalse;108}109 imagedestroy($new_imgx);110 imagedestroy($ni);111}112 imagedestroy($im);113returntrue;114}115}

发表回复