Generate a thumbnail in PHP (2) -- proportional thumbnail, thumbnail proportional

Source: Internet
Author: User

Generate a thumbnail in PHP (2) -- proportional thumbnail, thumbnail proportional

Analysis:

When the source image is a horizontal or vertical screen, you want the thumbnail to maintain the original proportional scaling without changing the integrity of the source image, that is, proportional scaling!

In this case, you only need to determine the width and height of the source image and the maximum width and height of the target image. Compare the width and height of the target image with that of the source image, in order to determine whether the source image is a horizontal screen or a vertical screen to determine the high or wide as the standard!

The Code is as follows:

// Evaluate the width and height of the target image if ($ max_width/$ max_height <$ src_width/$ src_height) {$ dst_w = $ max_width; // use the width as the standard $ dst_h = $ max_width * $ src_height/$ src_width;} else {$ dst_h = $ max_height; // use the height standard $ dst_w = $ max_height * $ src_width/$ src_height ;}

The complete code is as follows:

<? Php // maximum width and height of the Target Image $ max_width = 300; $ max_height = 300; // create a true color image with a large number of colors supported $ dst = imagecreatetruecolor ($ max_width, $ max_height); // target image width and height $ src = imagecreatefromjpeg ('. /01.jpg '); // source image $ src_width = imagesx ($ src); // source Image Width $ src_height = imagesy ($ src ); // height of the source image // evaluate the width and height of the target image if ($ max_width/$ max_height <$ src_width/$ src_height) {// The horizontal screen image is based on the width standard $ dst_w = $ max_width; $ dst_h = $ max_width * $ src_height/$ src_width;} else {// portrait Image Standard with height $ dst_h = $ max_height; $ dst_w = $ max_height * $ src_width/$ src_height;} // Coordinate Position displayed on the target graph $ dst_x = (int) ($ max_width-$ dst_w)/2); $ dst_y = (int) ($ max_height-$ dst_h)/2); // generate the thumbnail imagecopyresampled ($ dst, $ src, $ dst_x, $ dst_y, $ dst_w, $ dst_h, $ src_width, $ src_height); // output to the browser header ('content-type: image/png '); imagepng ($ dst); // destroy image resources imagedestroy ($ dst); imagedestroy ($ src);?>

 

The result is as follows:

Portrait Image

 

Landscape Image:

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.