Home >Backend Development >PHP Tutorial >PHP implements a method to generate thumbnails of uploaded images by scaling them proportionally to a specified size, _PHP Tutorial

PHP implements a method to generate thumbnails of uploaded images by scaling them proportionally to a specified size, _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:31776browse

php implements the method of generating thumbnails of uploaded images by scaling them proportionally to the specified size,

The example in this article describes how PHP implements scaling to generate thumbnails of uploaded images according to a specified size. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
/**
* *
*Constantial scaling
* @param unknown_type $srcImage Source image path
* @param unknown_type $toFile Target image path
* @param unknown_type $maxWidth Maximum width
* @param unknown_type $maxHeight Maximum height
* @param unknown_type $imgQuality image quality
* @return unknown
​*/
function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928234.htmlTechArticlephp implements the method of generating uploaded image thumbnails according to the specified size by scaling in equal proportions. This article describes the example of php implementing the specified size A method to generate thumbnails of uploaded images by scaling them proportionally. Share with everyone...
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