Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to compress a picture with phpcms

Shulou Source: shulou.com Published: 2022-06-02 12:14:57 10月01日 Update

This article shows you how to use phpcms to compress pictures, the code is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Phpcms compression method: first find and open the "extention.func.php" file; then write an automatic compression method in the public class; finally, through the statement "{compress ($rs ['thumb'])}" directly in the html page can be called.

Phpcms image automatic compression function

Write an automatic compression method in the public class:

Phpcms\ libs\ functions\ extention.func.php**@param $imgName@return mixed | string$imgpath original image storage address $thumbpath compressed image storage path * / function compress ($imgName) {imgPath=PHPCMSPATH.trim (imgPath=PHPCMS _ PATH. Trim (imgPath=PHPCMS P ATH.trim (imgName,'/'); $Patharray = explode ('/', $imgName); fileName=end (Patharray); / / create a thumb folder to save the compressed image 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 (fileexists (thumbPath)) {return APP_PATH. 'uploadfile/thumb/'. $fileName;} else {src=imagecreatefromjpeg (imgPath); srcw=imagesx (src_w = imagesx (srcw=imagesx (src); srch=imagesy (src_h = imagesy (srch=imagesy (src)); / / set thumbnail size $dst_w = $src_w1;$dst_h = $src_h2;dst=imagecreatetruecolor (dst_w,dsth); / / Image sampling copy imagecopyresampled (dst_h); / / Image sampling copy imagecopyresampled (dsth)) / / copy 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); / / Save thumbnail imagejpeg (src_h); / / Save thumbnail imagejpeg (srch); / / Save thumbnail imagejpeg (dst,thumbPath); / / destroy resource imagedestroy (src); imagedestroy ($dst) ThumbPath=strreplace (PHPCMSPATH, "/", thumbPath=str _ replace (PHPCMS_PATH, "/", thumbPath=strreplace (PHPCMSPATH, "/", thumbPath); return $thumbPath;}}

Call directly from the html page

{compress ($rs ['thumb'])} the above is how to compress pictures with phpcms. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Pictures methods resources skills files knowledge pages concise concise code content function original picture address size that is folders articles more articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia MariaDB Huawei Shulou Technology