This article mainly introduces the PHP image cropping function while keeping the image not deformed. For more information, see
To automatically crop the uploaded image, the cropped image is displayed on the foreground.
The requirement is as follows. The source code is as follows:
The Code is as follows:
* Exif_imagetype -- determines the type of an image.
* Description: The function is to crop an image of any size without deformation.
* 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 an image of any size. The image is stretched out in a few places without deformation or blank space.
Function my_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 = imagecreatefromgif ($ 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 and reduce the image by a large proportion (the scaled 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; // Case 1: The width ratio is smaller than the height direction. Crop or zoom in according to the height ratio standard.
} Else {
$ Ratio = $ ratio_w;
}
// Define an intermediate temporary image. The Aspect Ratio of the image 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, $ 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, $ new_width, $ new_height, $ inter_w, $ inter_h );
Switch ($ type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100); // store images
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. If one side of the target image is greater than the source image and the other side is smaller than the source image, the image is enlarged and then cropped.
// = If ($ ratio_w <1 & $ ratio_h> 1) | ($ ratio_w> 1 & $ ratio_h <1 ))
Else {
$ Ratio = $ ratio_h> $ ratio_w? $ Ratio_h: $ ratio_w; // The value with a large proportion.
// Define a large middle image with the same height or width as the target image, and then enlarge the source Image
$ Inter_w = (int) ($ w * $ ratio );
$ Inter_h = (int) ($ h * $ ratio );
$ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
// Scale and crop the source Image
Imagecopyresampled ($ inter_img, $ src_img, 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, $ new_width, $ new_height );
Switch ($ type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100); // store images
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
My_image_resize('test.gif ', '11111.gif', '100px ', '100px ');
?>