phpcms图片自动压缩功能
在公共类中写一个自动压缩的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
phpcmslibsunctionsextention.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;
}
}
立即学习“PHP免费学习笔记(深入)”;
点击下载“嗨格式压缩大师”;
推荐:《phpcms教程》
在html页面中直接调用
1
{compress ($rs[‘thumb’])}
以上就是phpcms怎么压缩图片的详细内容,更多请关注php中文网其它相关文章!