本文介绍了我需要Opencart 3上的webp图像.可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在opencart 3中使用webp图像?我的意思是自动生成.

Is it possible to use webp images in opencart 3? I mean automatic generation.

推荐答案

并非所有浏览器都接受WEBP图像格式但是您可以键入代码来检查浏览器是否接受WEBP格式,那么您将不得不进行新的图像缓存并将现有图像转换为webp

Not all the browsers accept WEBP image formatbut you can type a code to check if the browser accept WEBP format then you will have to make a new images cache and convert the existing images to webp

在fir dir目录/模型/工具/image.php中添加

on the fir dir catalog/model/tool/image.phpadd

$image_new_webp = 'cachewebp/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.webp';

在此之后

 $image_new = 'cache/'

检查浏览器是否接受image.webp并创建新的图像缓存在同一文件中:catalog/model/tool/image.php添加此代码:

to check if the browser accept image.webp and to create your new images cachein the same file: catalog/model/tool/image.phpadd this code:

$gd = gd_info();
        if ($gd['WebP Support']) {
            if (!is_file(DIR_IMAGE . $image_new_webp) || (filectime(DIR_IMAGE . $image_new) > filectime(DIR_IMAGE . $image_new_webp))) {
                                    
                $path = '';

                $directories = explode('/', dirname($image_new_webp));

                foreach ($directories as $directory) {
                    $path = $path . '/' . $directory;

                    if (!is_dir(DIR_IMAGE . $path)) {
                        @mkdir(DIR_IMAGE . $path, 0777);
                    }
                }
                
                $image_webp = new Image(DIR_IMAGE . $image_old);
                $image_webp->resize($width, $height);
                $image_webp->save_webp(DIR_IMAGE . $image_new_webp);
            }
        }

在此行之前: $ image_new = str_replace(

现在您需要一个功能以新格式保存图像在文件目录上:system/library/image.php添加此功能:

now you need a function to save the images in new formaton file dir: system/library/image.phpadd this function:

public function save_webp($file, $quality = 90) {
            if (is_resource($this->image)) {
                imagewebp($this->image, $file, $quality);
                imagedestroy($this->image);
            }
        }

在此行之前:公共函数save($ file,$ quality = 90){

输出webp图像格式在文件系统/library/response.php上添加此功能

output the webp images formatadd this function on file system/library/response.php

public function webpRebuild($output) {
            $gd = gd_info();
            if ($gd['WebP Support']) {
                $uri = '';

                if (isset($_SERVER['REQUEST_URI'])) {
                    $uri = $_SERVER['REQUEST_URI'];
                }
                
                if (stripos($uri, 'admin') === false) { // admin is your dashboard url if you have different name jst change it  
                    if (isset($_SERVER['HTTP_ACCEPT']) && isset($_SERVER['HTTP_USER_AGENT'])) {
                        if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false ) {   
                            $re = '/(cache)(.*)(\.jpg|\.png|.jpeg)/U';
                            $subst = '$1webp$2.webp';
                            $this->output = preg_replace($re, $subst, $this->output);
                        }
                    }
                }
            }
        }

在此行之前:私有函数compress($ data,$ level = 0)在同一文件上,您必须输出webpRebuild函数,因此添加以下代码:

before this line: private function compress($data, $level = 0)on the same file you have to output the webpRebuild function so add this code:

$this->webpRebuild($this->output);

在此行之后: $ output = $ this

这篇关于我需要Opencart 3上的webp图像.可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:47