Bootstrap

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;
            }
        }
    }

;