Home > php教程 > php手册 > thinkphp图片压缩代码整理

thinkphp图片压缩代码整理

WBOY
Release: 2016-06-07 11:38:34
Original
1694 people have browsed it

thinkphp使用中感觉图片压缩扩展使用比较麻烦,所以自己整理了一个方法,可直接调用来使用图片压缩扩展功能
/*
* $img_path 被压缩的图片的路径
* $thumb_w 压缩的宽
* $save_path 压缩后图片的存储路径
* $is_del 是否删除原文件,默认删除
*/
public function thumb_img($img_path, $thumb_w, $save_path, $is_del = true){
$image = new \Think\Image();
$image->open($img_path);
$width = $image->width(); // 返回图片的宽度
if($width > $thumb_w){
$width = $width/$thumb_w; //取得图片的长宽比
$height = $image->height();
$thumb_h = ceil($height/$width);
}

//如果文件路径不存在则创建
$save_path_info = pathinfo($save_path);
if(!is_dir($save_path_info['dirname'])) mkdir ($save_path_info['dirname'], 0777);

$image->thumb($thumb_w, $thumb_h)->save($save_path);

if($is_del) @unlink($img_path); //删除源文件
}

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