|
The image cropping and scaling function supports Square and custom coordinate cropping.
/**
* The Image cropping function supports the specified fixed-point cropping and orientation cropping modes.
* @ Param <string> $ src_file original image path
* @ Param <int> $ new_width: width of the cropped image (when the width of the cropped image exceeds the width of the original image, remove the width of the original image)
* @ Param <int> $ new_height: specifies the height of the cropped image. When the width of the cropped image exceeds the width of the original image, remove the original image height)
* @ Param <int> $ type: cropping in 1-azimuth mode and cropping in 0-fixed point mode.
* @ Param <int> $ starting orientation of pos orientation cropping (this parameter does not work when the fixed-point cropping mode is selected)
* 1 is the top left, 2 is the top center, and 3 is the top right;
* 4: center left, 5: center, and 6: center right;
* 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
* @ Param <int> $ start_x start position X (this parameter does not work when the orientation mode is selected for cropping)
* @ Param <int> $ start_y start position Y (this parameter does not work when the orientation mode is selected for cropping)
* @ Return <string> crop the image storage path
*/
Function thumb ($ src_file, $ new_width, $ new_height, $ type = 1, $ pos = 5, $ start_x = 0, $ start_y = 0 ){
$ Pathinfo = pathinfo ($ src_file );
$ Dst_file = $ pathinfo ['dirname']. '/'. $ pathinfo ['filename']. '_'. $ new_width. 'X '. $ new_height. '. '. $ pathinfo ['extension'];
If (! File_exists ($ dst_file )){
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
$ Img_type = exif_imagetype ($ src_file );
$ Support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF );
If (! In_array ($ img_type, $ support_type, true )){
Echo "only jpg, png, and gif image cropping is supported ";
Exit ();
}
/* Load the image */
Switch ($ img_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 "image loading error! ";
Exit ();
}
/* Obtain the width and height of the source image */
$ Src_width = imagesx ($ src_img );
$ Src_height = imagesy ($ src_img );
/* Calculate the width and height of the cut Image */
$ Mid_width = ($ src_width <$ new_width )? $ Src_width: $ new_width;
$ Mid_height = ($ src_height <$ new_height )? $ Src_height: $ new_height;
/* Initialize the coordinates of the starting position of the source image cropping */
Switch ($ pos * $ type ){
Case 1: // 1 is the top left
$ Start_x = 0;
$ Start_y = 0;
Break;
Case 2: // 2 center the top
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = 0;
Break;
Case 3: // 3: top right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = 0;
Break;
Case 4: // 4 is left in the middle
$ Start_x = 0;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 5: // 5 center in the middle
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 6: // 6 is the center and right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 7: // 7 is left at the bottom
$ Start_x = 0;
$ Start_y = $ src_height-$ mid_height;
Break;
Case 8: // 8 is centered at the bottom
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = $ src_height-$ mid_height;
Break;
Case 9: // 9: right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = $ src_height-$ mid_height;
Break;
Default: // random
Break;
}
// Create a background canvas for the cut Image
$ Mid_img = imagecreatetruecolor ($ mid_width, $ mid_height );
// Copy the cut image data to the canvas to generate the cut Image
Imagecopy ($ mid_img, $ src_img, 0, 0, $ start_x, $ start_y, $ mid_width, $ mid_height );
// Create a background canvas for the cropped image
$ New_img = imagecreatetruecolor ($ new_width, $ new_height );
// Copy and cut the image to the background canvas and crop it proportionally.
Imagecopyresampled ($ new_img, $ mid_img, 0, 0, 0, 0, $ new_width, $ new_height, $ mid_width, $ mid_height );
/* Save as image by format */
Switch ($ img_type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100 );
Break;
Case IMAGETYPE_PNG:
Imagepng ($ new_img, $ dst_file, 9 );
Break;
Case IMAGETYPE_GIF:
Imagegif ($ new_img, $ dst_file, 100 );
Break;
Default:
Break;
}
}
Return ltrim ($ dst_file ,'.');
} |