当前位置: 首页 > article >正文

osclass增加支持webp格式

1、basic_data.sql

数据表:t_preference中的('osclass', 'allowedExt', 'png,gif,jpg,jpeg', 'STRING'),添加:'png,gif,jpg,jpeg,webp'

2、includes/osclass/mimes.php

'webp' => 'image/webp',

3、includes/osclass/classes/ImageProcessing.php

修改private function __construct($imagePath) {}

switch (@$this->image_info['mime']) {
        case 'image/gif':
            $this->ext = 'gif';
            $this->mime = 'image/gif';
            break;
            
        case 'image/png':
            $this->ext = 'png';
            $this->mime = 'image/png';
            break;
            
        case 'image/webp': // 增加 WebP 处理
            $this->ext = 'webp';
            $this->mime = 'image/webp';
            break;
    
        default:
            $this->ext = 'jpg';
            $this->mime = 'image/jpeg';
    
            if (!$this->_use_imagick) {
                $bg = imagecreatetruecolor($this->_width, $this->_height);
                
                // 处理 PNG、WebP 透明背景
                if (in_array($this->ext, ['png', 'webp'])) {
                    imagefill($bg, 0, 0, imagecolorallocatealpha($bg, 0, 0, 0, 127));
                    imagesavealpha($bg, true);
                    imagealphablending($bg, false);
                } else {
                    imagefill($bg, 0, 0, $this->imageColor($bg));
                    imagesavealpha($bg, true);
                    imagealphablending($bg, true);
                }
    
                imagecopy($bg, $this->im, 0, 0, 0, 0, $this->_width, $this->_height);
                imagedestroy($this->im);
                $this->im = $bg;
            }
            break;
    }

4、savetofile改成

public function saveToFile($imagePath, $ext = null) {
        if (file_exists($imagePath) && !is_writable($imagePath)) {
            throw new RuntimeException("$imagePath is not writable!");
        }
    
        if ($ext == null) {
            $ext = $this->ext;
        }
    
        // 允许 webp 格式
        if (!in_array($ext, ['png', 'gif', 'webp'])) {
            $ext = 'jpeg'; // 默认 jpeg
        }
    //var_dump($ext);
        if ($this->_use_imagick) {
            if ($ext === 'jpeg' && ($this->ext !== 'jpeg' && $this->ext !== 'jpg')) {
                $bg = new Imagick();
                $bg->newImage($this->_width, $this->_height, osc_canvas_background());
                $this->im->thumbnailImage($this->_width, $this->_height, true);
                $bg->compositeImage($this->im, Imagick::COMPOSITE_OVER, 0, 0);
                $this->im = $bg;
                $this->ext = 'jpeg';
            }
    
            $this->im->setImageDepth(8);
            $this->im->setImageFilename($imagePath);
            $this->im->setImageFormat($ext);
            $this->im->writeImage($imagePath);
        } else {
            
            switch ($ext) {
                case 'gif':
                    imagegif($this->im, $imagePath);
                    break;
                case 'png':
                    imagepng($this->im, $imagePath, 0);
                    break;
                case 'webp': // 增加 WebP 处理
                    if (function_exists('imagewebp')) {
                        //var_dump($ext);var_dump('oka');die;
                        imagewebp($this->im, $imagePath);
                    } else {
                        //var_dump($ext);var_dump('okb');die;
                        throw new RuntimeException("WebP format is not supported by GD");
                    }
                    break;
                default:
                    if (($ext === 'jpeg' && ($this->ext !== 'jpeg' && $this->ext !== 'jpg')) || $this->_watermarked) {
                        $this->ext = 'jpeg';
                    }
                    imagejpeg($this->im, $imagePath);
                    break;
            }
        }
    }


http://www.kler.cn/a/536042.html

相关文章:

  • 51单片机 02 独立按键
  • PCA9685舵机控制板使用
  • B站自研的第二代视频连麦系统(上)
  • MongoDB 查询文档
  • Python爬虫-如何正确解决起点中文网的无限debugger
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_sprintf_num 函数
  • 【CPP】C++后端开发面试:深入理解编程中的锁机制
  • Linux进阶——web服务器
  • 【Spring Boot】自动配置源码解析
  • TcpClientTest
  • Python中 logging.basicConfig
  • 最新阿里高级Java面试题(首发,70道,带详细答案)
  • 支持向量机(一)
  • VERA: 基于视觉-语言模型的解释性视频异常检测框架
  • 大模型的微调方式
  • 【软件测试入门】Linux操作系统初级命令大全
  • 大模型蒸馏(Model Distillation)的原理及过程
  • 【Git】tortoisegit使用配置
  • 解锁高效 Web 开发新姿势:Open WebUI 安装指南
  • Java 的try-with-resources语句,不需要显式调用close()
  • autMan奥特曼机器人-对接deepseek教程
  • 【鸿蒙HarmonyOS Next实战开发】实现ArkTS/JS和C/C++的交互-Node-API
  • Qt —— 加载百度离线地图、及简单绘图(附源码)
  • 备战蓝桥杯:二维前缀和之激光炸弹
  • Java面试题-Java基础
  • 基础入门-算法解密散列对称非对称字典碰撞前后端逆向MD5AESDESRSA