PHP _php instance of code sharing of thumbnails generated by percentages

Source: Internet
Author: User
Tags imagejpeg

Then turned over the manual, understand several functions after writing a simple PHP to generate thumbnails of the program. Without a class, I think a function can be done, and it's easier for beginners to understand, so that they can help more people.

Supports a score-specific thumbnail, supported by a specified long width abbreviation, default by percent. Procedures in the comments are very detailed, if you have any questions can be in the following message, welcome to communicate with me.

The source code is as follows:

Copy Code code 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 be abbreviated as a percentage of the original image, which defaults to 50% when this is empty
* param width Specifies the length of the thumbnail
* param height specified after the thumbnail
* Note: When percent width height is passed into the value, and percent>0, the priority is abbreviated by percent
* by: more source to share with you
* Warm tip: Use this feature to open gd2 in php.ini

function Makethumb ($ori _img, $new _img, $percent =50, $width =0, $height =0) {

$original = getimagesize ($ori _img); To get a picture of the information, you can Print_r ($original) that it is an array
$original [2] is a picture type, where 1 represents GIF, 2 for JPG, 3 for PNG
Switch ($original [2]) {
Case 1: $s _original = imagecreatefromgif ($ori _img);
Case 2: $s _original = imagecreatefromjpeg ($ori _img);
Case 3: $s _original = imagecreatefrompng ($ori _img);

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 a picture to a browser
$loop = imagejpeg ($canvas, $new _img); Generate a new picture
if ($loop) {
echo "Ok!<br/>";

Makethumb ("Bhsj.jpg", "suolue1.jpg", 15,0,0); To generate a thumbnail of image 15%
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 the parameter is filled, the percentage priority is large)


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: 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.