Home > php教程 > php手册 > 智能截取图片(缩略图)

智能截取图片(缩略图)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 11:40:10
Original
1031 people have browsed it

按图片比例智能截取图片,主要还是利用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元

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template