QQ咨询 官方微信
微信二维码

添加微信好友

官方小程序
百度小程序

百度小程序

微信小程序

微信小程序

头条小程序

头条小程序

关于我们

PHPCMS

phpcms图片上传自动转 WebP改造流程

 shitouadmin  2026-11-09 19:01:30
1. cms/libs/classes/attachment.class.php
① 第 8 行 — 添加webp格式
  1. var $imageexts = array('gif', 'jpg', 'jpeg', 'png', 'bmp');   // 原 
  2. var $imageexts = array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp');   // 改为 
② 第 112–128 行 —`upload()` 内的上传处理块,处理转换webp
  1. if($watermark_enable) { 
  2.     $image->watermark($savefile, $savefile); 
  3. } 
upload方法里边找到上面的代码,在后边添加代码:
  1. // 自动转 WebP(后台开关控制) 
  2. $webp_setting = $this->_get_site_setting($this->siteid); 
  3. $src_ext = $fileext; 
  4. $webp_file = $this->to_webp($savefile, $fileext, $webp_setting); 
  5. if($webp_file) { 
  6.     @unlink($savefile); 
  7.     $fileext = 'webp'; 
  8.     $savefile = $webp_file; 
  9.     $filepath = preg_replace(new_addslashes("|^".$this->upload_root."|"), "", $webp_file); 
  10.     $file['name'] = preg_replace('/\.(jpg|jpeg|png|gif)$/i', '.webp', $file['name']); 
  11.     $file['size'] = @filesize($webp_file); 
  12.     // 缩略图同步转 webp,保持 get_thumb 命名一致 
  13.     if($thumb_enable && !empty($thumb_src) && file_exists($thumb_src)) { 
  14.         $thumb_webp = $this->to_webp($thumb_src, $src_ext, $webp_setting); 
  15.         if($thumb_webp) @unlink($thumb_src); 
  16.     } 
  17. } 
  18. $uploadedfile = array('filename'=>$file['name'], 'filepath'=>$filepath, 'filesize'=>$file['size'], 'fileext'=>$fileext, 'fn'=>$file['fn']); 
③ 第 250 行(`getname()` 方法前) — 新增`to_webp()` 方法:
  1. /** 
  2.  * 图片转 WebP(需 GD 支持 webp,且后台开关开启) 
  3.  */ 
  4. function to_webp($file, $fileext, $setting = array()) { 
  5.     if(empty($setting['webp_enable']) || !function_exists('imagewebp')) return false; 
  6.     $fileext = strtolower($fileext); 
  7.     switch($fileext) { 
  8.         case 'jpg': 
  9.         case 'jpeg': 
  10.             $src = @imagecreatefromjpeg($file); 
  11.             break; 
  12.         case 'png': 
  13.             $src = @imagecreatefrompng($file); 
  14.             break; 
  15.         case 'gif': 
  16.             $src = @imagecreatefromgif($file); 
  17.             break; 
  18.         default: 
  19.             return false; 
  20.     } 
  21.     if(!$src) return false; 
  22.     if($fileext == 'png' || $fileext == 'gif') { 
  23.         imagepalettetotruecolor($src); 
  24.         imagealphablending($src, true); 
  25.         imagesavealpha($src, true); 
  26.     } 
  27.     $quality = intval($setting['webp_quality']) ? intval($setting['webp_quality']) : 85; 
  28.     $newfile = preg_replace('/\.('.$fileext.')$/i', '.webp', $file); 
  29.     if(imagewebp($src, $newfile, $quality)) { 
  30.         imagedestroy($src); 
  31.         return $newfile; 
  32.     } 
  33.     imagedestroy($src); 
  34.     return false; 
  35. } 
2. cms/modules/admin/templates/site_edit.tpl.php
第 176 行(水印开关`</tr>` 之后) 插入:
  1. <tr> 
  2.   <th>上传图片转 WebP:</th> 
  3.   <td class="y-bg"> 
  4.     <input class="radio_style" name="setting[webp_enable]" value="1" <?php echo $setting['webp_enable']==1 ? 'checked="checked"' : ''?> type="radio"> 开启&nbsp;&nbsp;&nbsp;&nbsp; 
  5.     <input class="radio_style" name="setting[webp_enable]" value="0" <?php echo $setting['webp_enable']!=1 ? 'checked="checked"' : ''?> type="radio"> 关闭&nbsp;&nbsp;&nbsp;&nbsp; 
  6.     质量:<input type="text" class="input-text" name="setting[webp_quality]" id="webp_quality" size="4" value="<?php echo $setting['webp_quality'] ? $setting['webp_quality'] : '85' ?>" />(1-100,需 GD 支持 WebP) 
  7.   </td> 
  8. </tr> 
3. cms/modules/admin/templates/site_add.tpl.php
第 166 行(水印开关`</tr>` 之后) 插入:
  1. <tr> 
  2.   <th>上传图片转 WebP:</th> 
  3.   <td class="y-bg"> 
  4.     <input class="radio_style" name="setting[webp_enable]" value="1" type="radio"> 开启&nbsp;&nbsp;&nbsp;&nbsp; 
  5.     <input class="radio_style" name="setting[webp_enable]" value="0" checked="checked" type="radio"> 关闭&nbsp;&nbsp;&nbsp;&nbsp; 
  6.     质量:<input type="text" class="input-text" name="setting[webp_quality]" id="webp_quality" size="4" value="85" />(1-100,需 GD 支持 WebP) 
  7.   </td> 
  8. </tr> 
4. statics/js/swfupload/swf2ckeditor.js(这是老版本的上传)
第 73 行 :添加webp格式
  1. var opt="jpg|gif|png|bmp|jpeg";   // 原 
  2. var opt="jpg|gif|png|bmp|jpeg|webp";   // 改为 
5. statics/js/webuploader/h5upload.js
第 54 行 :
  1. var opt="jpg|gif|png|bmp|jpeg";   // 原 
  2. var opt="jpg|gif|png|bmp|jpeg|webp";   // 改为 
 6. cms/modules/attachment/attachments.php
第 16 行 :添加webp格式
  1. $this->imgext = array('jpg','gif','png','bmp','jpeg');   // 原 
  2. $this->imgext = array('jpg','gif','png','bmp','jpeg','webp');   // 改为 
7. cms/modules/attachment/functions/global.func.php
第 128 行 (`is_image()` 内):判断是否为图片
  1. $ext_arr = array('jpg','gif','png','bmp','jpeg','tiff');   // 原 
  2. $ext_arr = array('jpg','gif','png','bmp','jpeg','tiff','webp');   // 改为 
8. cms/modules/attachment/templates/attachment_list.tpl.php
第 125 行 (内联`IsImg()` 内):
  1. var opt="jpg|gif|png|bmp|jpeg";   // 原 
  2. var opt="jpg|gif|png|bmp|jpeg|webp";   // 改为 
9. cms/modules/attachment/templates/attachment_dir.tpl.php
第 81 行 (内联`IsImg()` 内):
  1. var opt="jpg|gif|png|bmp|jpeg";   // 原 
  2. var opt="jpg|gif|png|bmp|jpeg|webp";   // 改为 
10.(可选)cms/modules/attachment/functions/global.func.php
第 8 行 (`file_icon()` 内,让 webp 图标不显示成空白):
  1. $ext_arr = array('doc','docx','ppt','xls','txt','pdf','mdb','jpg','gif','png','bmp','jpeg','rar','zip','swf','flv');   // 原 
  2. $ext_arr = array('doc','docx','ppt','xls','txt','pdf','mdb','jpg','gif','png','bmp','jpeg','webp','rar','zip','swf','flv');   // 改为 
11、statics\images\ext下复制一份jpg.gif改成webp.gif,这样附件列表可以正常显示图标

12、处理缩略图生成问题(默认的是不支持webp裁剪缩略图的)
找到:cms/modules/attachment/attachment.php搜索代码:
  1. if (!in_array($fileext, array('jpg', 'gif', 'jpeg', 'png', 'bmp'))) exit(); 
里边加上‘webp’,同理搜索:
  1. if (in_array($uploadedfile['fileext'], array('jpg', 'gif', 'jpeg', 'png', 'bmp'))) { 
也是加上webp,然后处理缩略图的裁剪,libs/image.class.php大约268行:
  1. function check($image) { 
  2.     return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1])); 
  3. } 
也是补上webp改成:
  1. function check($image) { 
  2.     return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png|webp)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1])); 
  3. } 

改完保存,并更新缓存,就可以了

另外:存量图片如何转成webp?
目前是采用打开文章编辑,然后直接保存的方式,此时会触发自动转换,修改代码:
1、新增方法 convert_local_webp()
在 attachment.class.php 里,紧接在之前加的`to_webp()` 方法 之后 新增:
  1. /** 
  2.  * 将内容中的本站本地图片转为 WebP(编辑保存时触发,幂等:已转 webp 的不会再处理) 
  3.  */ 
  4. function convert_local_webp($value) { 
  5.     if(!function_exists('imagewebp')) return $value; 
  6.     $site_setting = $this->_get_site_setting($this->siteid); 
  7.     if(empty($site_setting['webp_enable'])) return $value; 
  8.     $string = new_stripslashes($value); 
  9.     // 匹配本站 uploadfile 目录下的图片(相对或绝对 URL 均可用子串替换覆盖) 
  10.     if(!preg_match_all('#(uploadfile/[\w\-\./]+\.(?:gif|jpg|jpeg|png))#i', $string, $matches)) return $value; 
  11.     $list = array_unique($matches[0]); 
  12.     $this->att_db = pc_base::load_model('attachment_model'); 
  13.     $search = $replace = array(); 
  14.     foreach($list as $rel) { 
  15.         $filepath = preg_replace('#^uploadfile/#', '', $rel); 
  16.         $file = $this->upload_root.$filepath; 
  17.         if(!is_file($file)) continue; 
  18.         $fileext = fileext($file); 
  19.         $newfile = $this->to_webp($file, $fileext, $site_setting); 
  20.         if(!$newfile) continue; 
  21.         // 若存在缩略图,同步转换 
  22.         $thumb_src = str_replace('.', '_thumb.', $file); 
  23.         if(is_file($thumb_src)) { 
  24.             $thumb_new = $this->to_webp($thumb_src, $fileext, $site_setting); 
  25.             if($thumb_new) @unlink($thumb_src); 
  26.         } 
  27.         @unlink($file); 
  28.         // 更新附件表 
  29.         $new_filepath = preg_replace("|^".$this->upload_root."|", "", $newfile); 
  30.         $this->att_db->update(array('filepath'=>$new_filepath, 'fileext'=>'webp', 'isimage'=>1), array('filepath'=>$filepath)); 
  31.         // 替换内容中的地址(只改扩展名,保留原 URL 形式) 
  32.         $new_rel = preg_replace('/\.('.$fileext.')$/i', '.webp', $rel); 
  33.         $search[] = $rel; 
  34.         $replace[] = $new_rel; 
  35.     } 
  36.     if(!empty($search)) $string = str_replace($search, $replace, $string); 
  37.     return $string; 
  38. } 
2、找到module/content/fields/editor/input.inc.php在return之前加入代码
  1. // 本地图片转 webp 
  2. $value = $this->attachment->convert_local_webp($value); 
3、同理在image的input.inc.php里的return之前也加入这行代码
  1. // 本地图片转 webp 
  2. $value = $this->attachment->convert_local_webp($value); 
4、images字段,这个比较特殊找到代码:
  1. $temp['url'] = $pic; 
在这之前加入一行:
  1. $pic = $this->attachment->convert_local_webp($pic); 

保存,然后记得更新全站缓存

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《phpcms图片上传自动转 WebP改造流程》发布于石头博客文章,作者:shitouadmin,如若转载,请注明出处:https://www.pweb123.com/cms/phpcms/1244.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友