PHP generate thumbnail fill white edge (equal to Thumbnail scheme) _php tutorial

Source: Internet
Author: User
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 ...

  • Related Article

    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.