PHP is more compatible to generate thumbnail code

  • 2020-03-31 21:26:59
  • OfStack

 
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 " your GD Library not available GIF Format the picture, please use Jpeg or PNG Format! <a href='javascript:go(-1);'> return </a>"; 
exit(); 
} 
$im = ImageCreateFromGIF($srcFile); 
break; 
case 2: 
if(!function_exists("imagecreatefromjpeg")){ 
echo " your GD Library not available jpeg Format of the picture, please use other format of the picture! <a href='javascript:go(-1);'> return </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); 
} 

Related articles: