Php code sharing for generating thumbnails by percentage _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
Php generates code sharing for thumbnails by percentage. After reading the manual, I learned a few functions and wrote a simple php code to generate thumbnails. No class is used. I think a function can be done. for beginners, I read the manual and understand a few functions. Then I Wrote a Simple php thumbnails program. No class is used. I think a function can be done, and it is easier for beginners to understand and help more people.

You can scale down by score. you can scale down by a specified length or width. the default value is by percentage. The comments in the program have been very detailed. if you have any questions, please leave a message below. please contact me.

The source code is as follows:

The code is as follows:



/*
* Param ori_img original image name and path
* Param new_img: name of the image generated
* Param percent: scale down based on the percentage of the source image. If this parameter is left blank, the default value is 50%.
* Param width specifies the scaled width.
* Param height specifies the scaled height.
*
* Note: When both the percent width and height values are input and the percent value is greater than 0, the values are scaled down by percentage first.
* By: http://www.jb51.net more source code to share with you
* Tip: to use this function, you must enable gd2 in php. ini.
*
**/

Function makeThumb ($ ori_img, $ new_img, $ percent = 50, $ width = 0, $ height = 0 ){

$ Original = getimagesize ($ ori_img); // Obtain the image information. print_r ($ original) can find that it is an array.
// $ Original [2] indicates the image type. 1 indicates gif, 2 indicates jpg, and 3 indicates 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 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", 5%, 0); // Generate the original thumbnail
MakeThumb ("bhsj.jpg", "suolue2.jpg", 0,200,120); // Generate a thumbnail with a width of PX and a height of 60 px
MakeThumb ("bhsj.jpg", "suolue3.jpg", 15,200,120); // Generate the original 5% thumbnail (the percentage has a higher priority when all parameters are set)

?>

Bytes. No use of classes, I think a function can be done, and more...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.