Home >Backend Development >PHP Tutorial >PHP big picture generates small picture code (thumbnail program)_PHP tutorial

PHP big picture generates small picture code (thumbnail program)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:45:23951browse

PHP large image generation code (thumbnail program) This is a code that uses the function that comes with PHP to generate thumbnail images of the specified size from the specified large image. It is easy to use and you only need to set the following four parameters to generate thumbnails of the size you want.

php tutorial large image generation code for small images (thumbnail program)
This is a code that uses the function that comes with PHP to generate thumbnail images of the specified size from the specified large image
It is easy to use and you only need to set the following four parameters to generate thumbnails of the size you want.

*/

function bigtosmallimg($file,$path,$w=120,$h=90)
{
$img=$path.$file;
$imgarr=getimagesize($img);
$sw=$imgarr[0];//Original image width
$sh=$imgarr[1];//Height of original image
$stype=$imgarr[2];
//Scale proportionally
if($sw/$sh>$w/$h){
$mw=$w;
$mh=(int)$sh*($w/$sw);
}
else{
$mw=(int)$sw*($h/$sh);
$mh=$h;
}

switch($stype){//Create a new source file for generating thumbnails based on the uploaded graphic file type.
case 1:
$srcf = imagecreatefromgif($img);
Break;
case 2:
$srcf = imagecreatefromjpeg($img);
Break;
case 3:
$srcf = imagecreatefrompng($img);
Break;
default:
Showmsg('Program call error.');
Break;
}

$desf =imagecreatetruecolor($mw,$mh);

imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh);
$sm_name=$path."s_".$file;
switch($stype){
case 1:
Imagegif($desf,$sm_name);
break;
case 2:
Imagejpeg($desf,$sm_name);
break;
case 3:
Imagepng($desf,$sm_name);
break;
default:
Showmsg('Unable to generate thumbnail image of www.bKjia.c0m' . $stype . ');
break;
}
imagedestroy($desf);
imagedestroy($srcf);

}

//This thumbnail calling method


bigtosmallimg($file,$path,$w=120,$h=90);
/*

$file = path to the image
$path = path saved after generation
$w =image width
$h =image height
*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633034.htmlTechArticlephp large image to generate small image code (thumbnail program) This is a function that uses PHP's own functions to specify The large image generates the thumbnail code of the size we specify. It is easy and simple to use, just set the settings...
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