This article mainly introduces PHP image cropping and scaling examples (lossless image cropping). I think it is quite good. I will share it with you and give you a reference. Let's take a look at the small Editor. This article mainly introduces the PHP image cropping and scaling examples (lossless cropping images). The first PHP community editor thinks it is quite good. I will share it with you now, I would also like to give you a reference. Let's take a look at it with Xiaobian.
This article introduces the PHP image cropping and scaling example. the code is as follows:
/** Exif_imagetype -- determines the type of an image * function description: This function crops an image of any size and keeps the image unchanged. * parameter description: enter the file name of the image to be processed, generate the save file name of the new image, generate the width of the new image, and generate the height of the new image * // Obtain the image of any size, stretch at least, function image_resize ($ src_file, $ dst_file, $ new_width, $ new_height) {$ new_width = intval ($ new_width ); $ new_height = intval ($ new_width); if ($ new_width <1 | $ new_height <1) {echo "params width or height error! "; Exit () ;}if (! File_exists ($ src_file) {echo $ src_file. "is not exists! "; Exit () ;}// Image type $ type = exif_imagetype ($ src_file); $ support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF); if (! In_array ($ type, $ support_type, true) {echo "this type of image does not support! Only support jpg, gif or png "; exit () ;}// Load image switch ($ type) {case IMAGETYPE_JPEG: $ src_img = imagecreatefromjpeg ($ src_file); break; case IMAGETYPE_PNG: $ src_img = imagecreatefrompng ($ src_file); break; case IMAGETYPE_GIF: $ src_img = Example ($ src_file); break; default: echo "Load image error! "; Exit () ;}$ w = imagesx ($ src_img); $ h = imagesy ($ src_img); $ ratio_w = 1.0 * $ new_width/$ w; $ ratio_h = 1.0 * $ new_height/$ h; $ ratio = 1.0; // The aspect ratio of the generated image is small or big. The principle is to enlarge the image by a large proportion, scale down a large proportion (the scale-down ratio is relatively small) if ($ ratio_w <1 & $ ratio_h <1) | ($ ratio_w> 1 & $ ratio_h> 1) {if ($ ratio_w <$ ratio_h) {$ ratio = $ ratio_h; // Scenario 1, the ratio of the width is smaller than that of the height. the width is cropped or enlarged according to the height ratio.} else {$ ratio = $ ratio_w;} // defines an intermediate temporary image, width of the image The aspect ratio exactly meets the target requirements $ inter_w = (int) ($ new_width/$ ratio); $ inter_h = (int) ($ new_height/$ ratio ); $ inter_img = imagecreatetruecolor ($ inter_w, $ inter_h); // var_dump ($ inter_img); 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 = imagecreatetruecolor ($ new_width, $ new_height); // var_dump ($ new_img); exit (); imagecopyresampled ($ new_img, $ Inter_img, 0, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h); switch ($ type) {case IMAGETYPE_JPEG: // store image imagejpeg ($ new_img, $ dst_file, 100); break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break; }}// end if 1/2 a target image has an edge greater than the source image and an edge smaller than the source image, first enlarge the plain image, and then crop // = if ($ ratio_w <1 & $ ratio_h> 1) | | ($ Ratio_w> 1 & $ ratio_h <1) else {$ ratio = $ ratio_h> $ ratio_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: // storage Image imagejpeg ($ new_img, $ dst_file, 100); break; case IMAGETYPE_PNG: imagepng ($ new_img, $ dst_file, 100); break; case IMAGETYPE_GIF: imagegif ($ new_img, $ dst_file, 100); break; default: break ;}} // if3} // end function // output the new image image_resize('test.jpg ', 'demo.jpg', 'shadowp', 'shadowp ');
The above is all the content of the image cropping and scaling example for PHP web development (lossless cropping of images). I hope it will be helpful for everyone's learning, and I hope you will enjoy the first PHP community!