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.