/**
* 图像的裁剪
* 依赖 composer require topthink/think-image
1 等比例缩放; 2 缩放后填充; 3 居中裁剪; 4 左上角裁剪; 5右下角裁剪; 6 固定尺寸缩放;
路径必须/开头
*/
function thumb_img($path,$width = 300,$height = null,$type=3){
if(substr($path,0,1)!=="/"){
return $path;
}
$image = \think\Image::open(ROOT_PATH."public".$path);
$imgWidth = $image->width();
$imgHeight = $image->height();
$scale=$imgWidth/$imgHeight;
if(!$height){
$height=intval($width/$scale);
}
$arr=explode(".",$path);
$ext=end($arr);
$newpath=substr($path,0,-strlen(".".$ext))."_{$width}_{$height}.{$ext}";
if(!is_file(ROOT_PATH."public".$newpath)){
echo "sc";
$image->thumb($width,$height,$type)->save(ROOT_PATH."public".$newpath);
}
return $newpath;
}
快捷键:tpsuoluetu
源:
sublime_text格式
<snippet>
<content><![CDATA[
/**
* 图像的裁剪
* 依赖 composer require topthink/think-image
1 等比例缩放; 2 缩放后填充; 3 居中裁剪; 4 左上角裁剪; 5右下角裁剪; 6 固定尺寸缩放;
路径必须/开头
*/
function thumb_img(\$path,\$width = 300,\$height = null,\$type=3){
if(substr(\$path,0,1)!=="/"){
return \$path;
}
\$image = \think\Image::open(ROOT_PATH."public".\$path);
\$imgWidth = \$image->width();
\$imgHeight = \$image->height();
\$scale=\$imgWidth/\$imgHeight;
if(!\$height){
\$height=intval(\$width/\$scale);
}
\$arr=explode(".",\$path);
\$ext=end(\$arr);
\$newpath=substr(\$path,0,-strlen(".".\$ext))."_{\$width}_{\$height}.{\$ext}";
if(!is_file(ROOT_PATH."public".\$newpath)){
echo "sc";
\$image->thumb(\$width,\$height,\$type)->save(ROOT_PATH."public".\$newpath);
}
return \$newpath;
}
]]></content>
<tabTrigger>tpsuoluetu(thinkphp生成缩略图)</tabTrigger>
<scope></scope>
</snippet>
发表评论 取消回复