### [phpcms怎么压缩图片](https://www.prosaas.cn/article/1793) **Published:** 2020-07-21T01:46:14 **Author:** 5201314 **Excerpt:** phpcms压缩图片的方法:首先找到并打开“extention.func.php”文件;然后在公共类中写一个自… > phpcms压缩图片的方法:首先找到并打开“extention.func.php”文件;然后在公共类中写一个自动压缩的方法;最后通过语句“{compress ($rs\[‘thumb’\])}”在html页面中直接调用即可。 ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212120245358.jpg) **phpcms图片自动压缩功能** 在公共类中写一个自动压缩的方法: phpcms\\libs\\functions\\extention.func.php \*\* @param $imgName @return mixed|string $imgpath 原图存放地址 $thumbpath 压缩后图片存放路径 \*/ function compress ($imgName){ imgPath=PHPCMSPATH.trim(imgPath = PHPCMS\_PATH . trim(imgPath=PHPCMS P ATH.trim(imgName, ‘/’); $Patharray = explode(’/’, $imgName); fileName=end(fileName = end(fileName=end(Patharray); //创建thumb文件夹,保存压缩后的图片 header(“Content-type:text/html;charset=utf-8”); $path="/uploadfile/thumb/"; dirPath=PHPCMSPATH.dirPath = PHPCMS\_PATH.dirPath=PHPCMS P ATH.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(fileName;if(file e xists(thumbPath)) { return APP\_PATH . ‘uploadfile/thumb/’ . $fileName; }else{ src=imagecreatefromjpeg(src = imagecreatefromjpeg(src=imagecreatefromjpeg(imgPath); srcw=imagesx(src\_w = imagesx(src w =imagesx(src); srch=imagesy(src\_h = imagesy(src h =imagesy(src); //设置缩略图大小 $dst\_w = $src\_w1; $dst\_h = $src\_h1; dst=imagecreatetruecolor(dst = imagecreatetruecolor(dst=imagecreatetruecolor(dst\_w,dsth);//图片采样复制imagecopyresampled(dst\_h);//图片采样复制imagecopyresampled(dst h );//图片采样复制imagecopyresampled(dst,src,0,0,0,0,src,0,0,0,0,src,0,0,0,0,dst\_w,dsth,dst\_h,dst h ,src\_w,srch);//保存缩略图imagejpeg(src\_h);//保存缩略图imagejpeg(src h );//保存缩略图imagejpeg(dst,thumbPath);//销毁资源imagedestroy(thumbPath);//销毁资源imagedestroy(thumbPath);//销毁资源imagedestroy(src); imagedestroy($dst); thumbPath=strreplace(PHPCMSPATH,"/",thumbPath = str\_replace(PHPCMS\_PATH,"/",thumbPath=str r eplace(PHPCMS P ATH,"/",thumbPath); return $thumbPath; } } ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212120245797.png) ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212120245267.png) 推荐:《phpcms教程》 在html页面中直接调用 {compress ($rs\[‘thumb’\])} 以上就是phpcms怎么压缩图片的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** html, php, PHPCMS **Categories:** PHPCMS ---