The most compatible PHP generated thumbnail function code (modified version)

Source: Internet
Author: User
Tags format exit imagejpeg
Copy CodeThe 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 pictures, 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 can not use JPEG format pictures, please use other format pictures!" <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);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.