Heim  >  Artikel  >  php教程  >  thinkphp图片压缩代码整理

thinkphp图片压缩代码整理

WBOY
WBOYOriginal
2016-06-07 11:38:341638Durchsuche

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元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn