: This article mainly introduces the PHP practical image compression method. if you are interested in the PHP Tutorial, please refer to it.
1 & $ ratio_h> 1) {if ($ ratio_w <$ ratio_h) {$ ratio = $ ratio_h;} else {$ ratio = $ ratio_w ;} // define an intermediate temporary Image. the image's aspect ratio exactly meets the target requirement $ inter_w = (int) ($ new_width/$ ratio); $ inter_h = (int) ($ new_height/$ ratio); $ inter_img = imagecreatetruecolor ($ inter_w, $ inter_h); imagecopy ($ inter_img, $ src_img, 0, 0, 0, 0, $ inter_w, $ inter_h); // generate a temporary image with the maximum edge length as the size of the target image $ ratio // define a new image $ new_img = imagecreatet Ruecolor ($ new_width, $ new_height); imagecopyresampled ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h ); switch ($ type) {case IMAGETYPE_JPEG: imagejpeg ($ new_img, $ dst_file, 100); // store image break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100 ); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break ;}} else {$ ratio = $ ratio_h> $ ra Tio_w? $ Ratio_h: $ ratio_w; // take the value with a large proportion. // define a large middle image with the same height or width as the target image, then enlarge the source image $ inter_w = (int) ($ w * $ ratio); $ inter_h = (int) ($ h * $ ratio); $ inter_img = imagecreatetruecolor ($ inter_w, $ inter_h); // crop imagecopyresampled ($ inter_img, $ src_img, 0, 0, 0, 0, $ inter_w, $ inter_h, $ w, $ h); // define a new image $ new_img = imagecreatetruecolor ($ new_width, $ new_height); imagecopy ($ new_img, $ inter_img, 0, 0, 0, 0, 0, $ new_width, $ new_height); switch ($ type) {case IMAGETYPE_JPEG: imagejpeg ($ new_img, $ dst_file, 100); // store the image break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break;} comment ', 200,200 );
The above introduces the PHP practical image compression method, including the content, hope to be helpful to friends interested in PHP tutorials.