Smart screenshot (thumbnail)

Source: Internet
Author: User
Tags crop image
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.