Site upload pictures After the generation of thumbnails should be very common features, usually for the site display beautiful, thumbnails will be the same size, such as the recent author of a site, thumbnail specifications are 160x120. However, if the uploaded image scale and thumbnail are inconsistent, direct scaling will cause the picture to deform, so the experience is definitely not good. So I think of a compromise approach, is to reduce the way to add white edge.
Source graph, size is 600x366:
Eventually generated:
The code is relatively long, the following simple ideas:
The source map is generated proportionally to the thumbnail, and the width is not greater than 160, and the height is not greater than 120. For example, Mr. Cheng 160x98 's thumbnail image.
Create a new white background image of 160x120, and place the thumbnail generated in the previous step in the center of the image to be OK.
The final code is as follows:
Copy the Code code as follows:
The path to the source map, either a local file or a remote picture
$src _path = ' 1.jpg ';
Finally save the width of the picture
$width = 160;
The final saving picture of the high
$height = 120;
Source Graph Object
$src _image = imagecreatefromstring (file_get_contents ($src _path));
$src _width = imagesx ($src _image);
$src _height = Imagesy ($src _image);
Generate thumbnails of equal proportions
$tmp _image_width = 0;
$tmp _image_height = 0;
if ($src _width/$src _height >= $width/$height) {
$tmp _image_width = $width;
$tmp _image_height = Round ($tmp _image_width * $src _height/$src _width);
} else {
$tmp _image_height = $height;
$tmp _image_width = Round ($tmp _image_height * $src _width/$src _height);
}
$tmpImage = Imagecreatetruecolor ($tmp _image_width, $tmp _image_height);
Imagecopyresampled ($tmpImage, $src _image, 0, 0, 0, 0, $tmp _image_width, $tmp _image_height, $src _width, $src _height);
Add White Edge
$final _image = Imagecreatetruecolor ($width, $height);
$color = Imagecolorallocate ($final _image, 255, 255, 255);
Imagefill ($final _image, 0, 0, $color);
$x = Round (($width-$tmp _image_width)/2);
$y = Round (($height-$tmp _image_height)/2);
Imagecopy ($final _image, $tmpImage, $x, $y, 0, 0, $tmp _image_width, $tmp _image_height);
Output picture
Header (' Content-type:image/jpeg ');
Imagejpeg ($final _image);
http://www.bkjia.com/PHPjc/621663.html www.bkjia.com true http://www.bkjia.com/PHPjc/621663.html techarticle site upload pictures After the generation of thumbnails should be very common features, usually for the site display beautiful, thumbnails will be the same size, such as the recent author of a site, shrinking ...