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

thinkphp图片压缩代码整理

WBOY
WBOYOriginal
2016-06-07 11:38:341637browse

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元

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