So I read the manual, understood a few functions, and wrote a simple PHP program to generate thumbnails. Without classes, I think a function can do it, and it is easier for novices to understand, which can help more people.
Supports abbreviation according to score ratio, supports abbreviation according to specified length and width, and defaults to percentage. The comments in the program are very detailed. If you have any questions, please leave a message below. Welcome to communicate with me.
The source code is as follows:
Copy the code The code is as follows:
/*
* param ori_img The name and path of the original image
* param new_img The name of the generated image
* param percent means to abbreviate according to the percentage of the original image. When this item is empty, it defaults to 50%
* param width specifies the abbreviated width
* param height specifies the abbreviated height
*
* Note: When percent width height is passed in, and percent>0, priority is given to Abbreviate by percentage
* by: http://www.jb51.net More source code to share with you
* Warm reminder: To use this function, you need to enable gd2 in php.ini
*
**/
function makeThumb($ori_img, $new_img, $percent=50, $width=0, $height=0){
$original = getimagesize($ori_img); / /Get the picture information, you can print_r($original) and find that it is an array
//$original[2] is the picture type, where 1 means gif, 2 means jpg, and 3 means png
switch($original [2]){
case 1 : $s_original = imagecreatefromgif($ori_img);
break;
case 2 : $s_original = imagecreatefromjpeg($ori_img);
break;
case 3 : $s_original = imagecreatefrompng($ori_img);
break;
}
if($percent > 0){
$width = $original[0] * $percent / 100 ;
$width = ($width > 0) ? $width : 1;
$height = $original[1] * $percent / 100;
$height = ($height > 0) ? $height : 1;
}
//Create a true color canvas
$canvas = imagecreatetruecolor($width,$height);
imagecopyresized($canvas, $s_original, 0, 0, 0, 0, $width, $height, $original[0], $original[1]);
//header("Content-type:image/jpeg");
// imagejpeg($canvas); //Output the image to the browser
$loop = imagejpeg($canvas, $new_img); //Generate a new image
if($loop){
echo "OK!
";
}
}
makeThumb("bhsj.jpg","suolue1.jpg",15,0,0); //Generate 15% of the original image Thumbnail
makeThumb("bhsj.jpg", "suolue2.jpg",0,200,120); // Generate a thumbnail with a width of 100px and a height of 60px
makeThumb("bhsj.jpg", "suolue3. jpg",15,200,120); //Generate a thumbnail of 15% of the original image (when all parameters are filled in, the percentage priority is higher)
?>
http://www.bkjia.com/PHPjc/768143.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768143.htmlTechArticleSo I looked through the manual, understood a few functions and wrote a simple PHP program to generate thumbnails. . There is no need to use classes, I think a function can do it, and it is more suitable for novices...