search
Homephp教程php手册智能截取图片(缩略图)

按图片比例智能截取图片,主要还是利用tp自带的image函数实现。
/**<br>  * 居中剪裁图片<br>  * @param $img_url 图片绝对地址<br>  * @param $cut_width 裁剪宽度<br>  * @param $cut_height 采集高度<br>  * @param string $save_img_url 保存的图片绝对地址,如果为'',则覆盖掉原图片<br>  * @param bool $is_delete 当save_img_url不为空时,是否删掉原始img<br>  */<br> function imageCenterCut($img_url, $cut_width, $cut_height, $save_img_url='', $is_delete=false){<br>     $cut_width = intval($cut_width);<br>     $cut_height = intval($cut_height);<br>     $image = new \Think\Image();<br>     $image->open($img_url);<br>     $image_width = $image->width();<br>     $image_height = $image->height();<br>     //居中裁剪图片<br>     if($image_width / $image_height > $cut_width / $cut_height){<br>         $image->crop(round($image_height / $cut_height * $cut_width), $image_height, round(($image_width - $image_height / $cut_height * $cut_width) / 2), 0);<br>     }else if($image_width / $image_height          $image->crop($image_width, round($image_width / $cut_width * $cut_height), 0, round(($image_height - $image_width / $cut_width * $cut_height) / 2));<br>     }else{<br>         //长宽比相同<br>         if($image_width == $cut_width){<br>             //如果长宽相等,则直接copy一份<br>             if(!empty($save_img_url)){<br>                 copy($img_url, $save_img_url);<br>                 //删掉原图<br>                 if($is_delete){<br>                     unlink($img_url);<br>                 }<br>             }<br>         }else{<br>             if(empty($save_img_url)){<br>                 $image->thumb($cut_width, $cut_height)->save($img_url);<br>             }else{<br>                 $image->thumb($cut_width, $cut_height)->save($save_img_url);<br>                 //删掉原图<br>                 if($is_delete){<br>                     unlink($img_url);<br>                 }<br>             }<br>         }<br>         return;<br>     }<br>     if(empty($save_img_url)){<br>         $image->thumb($cut_width, $cut_height)->save($img_url);<br>     }else{<br>         $image->thumb($cut_width, $cut_height)->save($save_img_url);<br>         //删掉原图<br>         if($is_delete){<br>             unlink($img_url);<br>         }<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment