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: