Crop and scale php images to generate thumbnails as needed

Source: Internet
Author: User

The image size is too large and its specifications are not uniform. The display control needs to be completed by JavaScript. When used on mobile devices, the display effect is poor and the traffic is huge. You need to process the image of the existing image library once, generate thumbnails that meet the requirements of mobile devices, and transfer the work done by the original client JS to the server using the php gd library for centralized processing.

Image Source and required size:
Copy codeThe Code is as follows:
$ Src_img = "wallpaper.jpg ";
$ Dst_w = 300;
$ Dst_h = 200;

Crop the image to maximize the display of the image area and scale proportionally to the specified size.

At first, the imagecopyresized method was used for image proportional reduction. After the actual operation, it was found that the dry points were very serious after the image was reduced. I used imagecopyresampled again (many of the articles are reposted online, but they all wrote imagecopyresampled as imagecopysampled, So I re-posted this, this method will re-sample the image, smooth the reduced image, and greatly improve the definition.
Copy codeThe Code is as follows:
<? Php
List ($ src_w, $ src_h) = getimagesize ($ src_img); // obtain the source image size
$ Dst_scale = $ dst_h/$ dst_w; // aspect ratio of the Target Image
$ Src_scale = $ src_h/$ src_w; // aspect ratio of the source Image
If ($ src_scale> = $ dst_scale)
{
// Too high
$ W = intval ($ src_w );
$ H = intval ($ dst_scale * $ w );
$ X = 0;
$ Y = ($ src_h-$ h)/3;
}
Else
{
// Too wide
$ H = intval ($ src_h );
$ W = intval ($ h/$ dst_scale );
$ X = ($ src_w-$ w)/2;
$ Y = 0;
}
// Crop
$ Source = imagecreatefromjpeg ($ src_img );
$ Croped = imagecreatetruecolor ($ w, $ h );
Imagecopy ($ croped, $ source, 0, 0, $ x, $ y, $ src_w, $ src_h );
// Zoom
$ Scale = $ dst_w/$ w;
$ Target = imagecreatetruecolor ($ dst_w, $ dst_h );
$ Final_w = intval ($ w * $ scale );
$ Final_h = intval ($ h * $ scale );
Imagecopyresampled ($ target, $ croped, 0, 0, 0, $ final_w, $ final_h, $ w, $ h );
// Save
$ Timestamp = time ();
Imagejpeg ($ target, "success ");
Imagedestroy ($ target );
?>

I hope you can use it for convenience.

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.