The example of this article describes the PHP implementation of the specified size and so on to release the thumbnail image upload method. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
/**
* *
* Equal-ratio scaling
* @param unknown_type $srcImage Source picture path
* @param unknown_type $toFile target picture path
* @param unknown_type $maxWidth Maximum width
* @param unknown_type $maxHeight Maximum height
* @param unknown_type $imgQuality picture quality
* @return Unknown
*/
function Resize ($srcImage, $toFile, $maxWidth = $maxHeight =, $imgQuality =100)
{
List ($width, $height, $type, $attr) = getimagesize ($srcImage);
if ($width < $maxWidth | | | $height < $maxHeight) return;
Switch ($type) {
Case 1: $img = Imagecreatefromgif ($srcImage); Break
Case 2: $img = Imagecreatefromjpeg ($srcImage); Break
Case 3: $img = Imagecreatefrompng ($srcImage); Break
}
$scale = min ($maxWidth/$width, $maxHeight/$height); Find the bloom ratio
if ($scale < 1) {
$newWidth = Floor ($scale * $width);
$newHeight = Floor ($scale * $height);
$NEWIMG = Imagecreatetruecolor ($newWidth, $newHeight);
Imagecopyresampled ($NEWIMG, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
$toFile = Preg_replace ("/(. gif|). Jpg|. Jpeg|. PNG)/I "," ", $toFile);
Switch ($type) {
Case 1:if (Imagegif ($newImg, "$toFile $newname.gif", $imgQuality))
Return "$newName. gif"; Break
Case 2:if (imagejpeg ($newImg, "$toFile $newname.jpg", $imgQuality))
Return "$newName. jpg"; Break
Case 3:if (Imagepng ($newImg, "$toFile $newname.png", $imgQuality))
Return "$newName. png"; Break
Default:if (imagejpeg ($newImg, "$toFile $newname.jpg", $imgQuality))
Return "$newName. jpg"; Break
}
Imagedestroy ($NEWIMG);
}
Imagedestroy ($IMG);
return false;
}
I hope this article will help you with your PHP program design.