Provides a variety of official and user-released code examples. For code reference, you are welcome to exchange and learn how to intelligently intercept pictures based on the proportion of images, mainly using the image function provided by tp.
/**
* Center crop image
* @ Param $ img_url: Absolute image address
* @ Param $ cut_width crop width
* @ Param $ cut_height acquisition height
* @ Param string $ save_img_url: the absolute address of the image saved. If it is '', the original image will be overwritten.
* @ Param bool $ is_delete whether to delete the original img when the save_img_url is not empty
*/
Function imageCenterCut ($ img_url, $ cut_width, $ cut_height, $ save_img_url = '', $ is_delete = false ){
$ Cut_width = intval ($ cut_width );
$ Cut_height = intval ($ cut_height );
$ Image = new \ Think \ Image ();
$ Image-> open ($ img_url );
$ Image_width = $ image-> width ();
$ Image_height = $ image-> height ();
// Center the cropped Image
If ($ image_width/$ image_height> $ cut_width/$ cut_height ){
$ Image-> crop (round ($ image_height/$ cut_height * $ cut_width), $ image_height, round ($ image_width-$ image_height/$ cut_height * $ cut_width)/2 ), 0 );
} Else if ($ image_width/$ image_height <$ cut_width/$ cut_height ){
$ Image-> crop ($ image_width, round ($ image_width/$ cut_width * $ cut_height), 0, round ($ image_height-$ image_width/$ cut_width * $ cut_height) /2 ));
} Else {
// Same Aspect Ratio
If ($ image_width = $ cut_width ){
// If the length and width are equal, copy one copy directly.
If (! Empty ($ save_img_url )){
Copy ($ img_url, $ save_img_url );
// Delete the source Image
If ($ is_delete ){
Unlink ($ img_url );
}
}
} Else {
If (empty ($ save_img_url )){
$ Image-> thumb ($ cut_width, $ cut_height)-> save ($ img_url );
} Else {
$ Image-> thumb ($ cut_width, $ cut_height)-> save ($ save_img_url );
// Delete the source Image
If ($ is_delete ){
Unlink ($ img_url );
}
}
}
Return;
}
If (empty ($ save_img_url )){
$ Image-> thumb ($ cut_width, $ cut_height)-> save ($ img_url );
} Else {
$ Image-> thumb ($ cut_width, $ cut_height)-> save ($ save_img_url );
// Delete the source Image
If ($ is_delete ){
Unlink ($ img_url );
}
}
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB