phpcms图片自动压缩功能

phpcms图片自动压缩功能
在公共类中写一个自动压缩的方法:

phpcms\libs\functions\extention.func.php
**

  • @param $imgName
  • @return mixed|string
  • $imgpath 原图存放地址
  • $thumbpath 压缩后图片存放路径
    */

function compress ($imgName){

imgPath=PHPCMSPATH.trim(imgPath = PHPCMS_PATH . trim(imgName, ‘/’);

$Patharray = explode(’/’, $imgName);
fileName=end(fileName = end(Patharray);
//创建thumb文件夹,保存压缩后的图片
header(“Content-type:text/html;charset=utf-8”);
$path="/uploadfile/thumb/";
dirPath=PHPCMSPATH.dirPath = PHPCMS_PATH.path;
if(!is_dir(KaTeX parse error: Expected '}', got 'EOF' at end of input: …h)){ mkdir(dirPath,0777,true);
}
$thumbPath = PHPCMS_PATH . ‘uploadfile/thumb/’ . fileName;if(fileexists(fileName; if (file_exists(thumbPath)) {
return APP_PATH . ‘uploadfile/thumb/’ . $fileName;
}else{
src=imagecreatefromjpeg(src = imagecreatefromjpeg(imgPath);
srcw=imagesx(src_w = imagesx(src);
srch=imagesy(src_h = imagesy(src);
//设置缩略图大小
$dst_w = $src_w1;
$dst_h = $src_h
1;
dst=imagecreatetruecolor(dst = imagecreatetruecolor(dst_w,dsth);//imagecopyresampled(dst_h); //图片采样复制 imagecopyresampled(dst,src,0,0,0,0,src,0,0,0,0,dst_w,dsth,dst_h,src_w,srch);//imagejpeg(src_h); //保存缩略图 imagejpeg(dst,thumbPath);//imagedestroy(thumbPath); //销毁资源 imagedestroy(src);
imagedestroy($dst);
thumbPath=strreplace(PHPCMSPATH,"/",thumbPath = str_replace(PHPCMS_PATH,"/",thumbPath);
return $thumbPath;
}
}

phpcms图片自动压缩功能
phpcms图片自动压缩功能
在html页面中直接调用

{compress ($rs[‘thumb’])}