Home  >  Article  >  Backend Development  >  Code for generating thumbnails in PHP with good compatibility_PHP tutorial

Code for generating thumbnails in PHP with good compatibility_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:08955browse

Copy code The code is as follows:

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 cannot use GIF format images, please use Jpeg or PNG format! Return";
exit();
}
$im = ImageCreateFromGIF($srcFile) ;
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "Your GD library cannot use images in jpeg format, please use images in other formats!< ;a href='javascript:go(-1);'>return";
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);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322863.htmlTechArticleCopy the code The code is as follows: function ImageResize($srcFile,$toW,$toH,$toFile="") { if($toFile==""){ $toFile = $srcFile; } $info = ""; $data = GetImageSize($srcFile,$info); switch ($d...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn