In php programming, the picture size is too large and the specification is 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, the image of the existing image library needs to be processed once to generate thumbnails that meet the requirements of mobile devices, and transfer the work done by the original client JS to the server side for centralized processing using the GD library of PHP. Required. Image source and size:
- 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 );
- ?>
|