PHP generates thumbnails of uploaded images according to the specified size and proportional scaling_PHP tutorial

WBOY
Release: 2016-07-20 11:07:44
Original
767 people have browsed it

php tutorial to generate thumbnails of uploaded images by scaling them proportionally to the specified size
/**
* *
*Constant ratio 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 bloom Scale

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"; ))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg" ; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}


http://www.bkjia.com/PHPjc/444947.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444947.htmlTechArticlephp tutorial to generate thumbnails of uploaded images according to the specified size and proportional scaling/** * * *Equal proportional scaling* @ param unknown_type $srcImage source image path* @param unknown_type $toFile target image path...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!