91视频蝌蚪视频|欧美久久资源网址|亚洲中文字幕Aⅴ|黄网毛片免费观看|国产激情在线主播|超碰人人愛人人操|黄色的成人BBw|亚洲伦理在线观看|香蕉AV在线国产|久久激情偷拍视频

打造一個外貿(mào)網(wǎng)站,不僅需要有吸引人的設(shè)計,更需要考慮用戶的體驗。我們的目標是從文案、設(shè)計、技術(shù)等多個維度出發(fā),構(gòu)建一個以用戶體驗為中心、以詢盤為導向的營銷型外貿(mào)網(wǎng)站。讓您的客戶在短時間內(nèi)了解您的優(yōu)勢,快速詢盤!
外貿(mào)網(wǎng)站建設(shè) 外貿(mào)獨立站推廣 跨境電商

PB上傳圖片轉(zhuǎn)換成webp功能開發(fā)

時間:2025-06-15   來源:外貿(mào)網(wǎng)站建設(shè)

webp能將原圖壓縮30%左右還能保持清楚。所以正在被越來越多的人喜歡。

pb要增這個功能,第一步:找到ueditor中的Uploader.class.php,任意處添加函數(shù)。

   /**
 * 將圖片轉(zhuǎn)換為WebP格式
 */
private function convertToWebP()
{
    $originalFilePath = $this->filePath; // 保存原始文件路徑
    $webpFilePath = preg_replace('/\\\\\\\\.(jpg|jpeg|png)$/i', '.webp', $this->filePath);
    $webpFullName = preg_replace('/\\\\\\\\.(jpg|jpeg|png)$/i', '.webp', $this->fullName);
    $webpFileName = preg_replace('/\\\\\\\\.(jpg|jpeg|png)$/i', '.webp', $this->fileName);

    if ($this->fileType === '.jpg' || $this->fileType === '.jpeg') {
        $image = imagecreatefromjpeg($this->filePath);
    } elseif ($this->fileType === '.png') {
        $image = imagecreatefrompng($this->filePath);
    } else {
        return false;
    }

    if ($image) 
        if (imagewebp($image, $webpFilePath)) {
            $this->filePath = $webpFilePath;
            $this->fullName = $webpFullName;
            $this->fileName = $webpFileName;
            $this->fileType = '.webp';
            $this->fileSize = filesize($webpFilePath);
            imagedestroy($image);
            unlink($originalFilePath); // 刪除原始文件而不是WebP文件
            return true;
        }
        imagedestroy($image);
    }
    return false;
}

/**
 * 轉(zhuǎn)換上傳的圖片為 WebP 格式(不影響原始文件)
 */


第二步:請找到如下代碼(大概率出現(xiàn)在函數(shù) handleFile()saveFile() 中):

找到:

$this->stateInfo = $this->stateMap[0];

后添加:

$this->convertToWebP(); // 上傳成功后生成 .webp 副本

效果如:

$this->stateInfo = $this->stateMap[0];
$this->convertToWebP(); // 上傳成功后生成 .webp 副本


第三步:funtion中的file.php中添加一個函數(shù)

// 添加一個新的函數(shù)用于轉(zhuǎn)換圖片為WebP格式
function convert_to_webp($src, $dest, $quality = 80)
{
    $info = getimagesize($src);
    $is_converted = false;
    switch ($info['mime']) {
        case 'image/jpeg':
            $image = imagecreatefromjpeg($src);
            $is_converted = imagewebp($image, $dest, $quality);
            imagedestroy($image);
            break;
        case 'image/gif':
            // 對于GIF文件,直接返回false,不進行WebP轉(zhuǎn)換
            return false;
            break;
        case 'image/png':
            $image = imagecreatefrompng($src);
            $is_converted = imagewebp($image, $dest, $quality);
            imagedestroy($image);
            break;
        default:
            return false;
    }
    return $is_converted;
}


第四步:在// 處理并移動上傳文件function handle_upload中的末尾找到并修改,

 // 如果是圖片
    if (is_image($file_path)) {
        // 進行等比例縮放
        if (($reset = resize_img($file_path, $file_path, $max_width, $max_height)) !== true) {
            return $reset;
        }
        // 圖片打水印
        if ($watermark) {
            watermark_img($file_path);
        }
    }
    return $save_file;


改成:


// 如果是圖片
    if (is_image($file_path)) {
        // 進行等比例縮放
        if (($reset = resize_img($file_path, $file_path, $max_width, $max_height)) !== true) {
            return $reset;
        }
        // 圖片打水印
        if ($watermark) {
            watermark_img($file_path);
        }

         // 轉(zhuǎn)換為WebP格式
        $webp_path = preg_replace('/\\.\\w+$/', '.webp', $file_path);
        if (!convert_to_webp($file_path, $webp_path)) {
            // 如果轉(zhuǎn)換失敗,保留原始文件格式
            $webp_path = $file_path;
        } else {
            // 刪除原始格式的圖片文件
            if (file_exists($file_path)) {
                unlink($file_path);
            }
            $file_path = $webp_path;
        }
    }
     // 返回WebP格式的文件路徑
    $save_file = str_replace(ROOT_PATH, '', $file_path); // 獲取文件站點路徑
    return $save_file;


Ok了,我們現(xiàn)在來測試一下上傳圖片。



TAG:
新聞推薦
pb后臺如何導入HTML文章
pb后臺如何導入HTML文章

項目需求:客戶要求后臺能導入HTML代碼。這個代碼是他用AI生成的新聞詳情,自帶了各種樣式。形如:<styl...

外貿(mào)網(wǎng)站建設(shè),PB二開調(diào)用標題來生成URL路徑
外貿(mào)網(wǎng)站建設(shè),PB二開調(diào)用標題來生成URL路徑

如題,客戶說希望URL地址就是發(fā)布文章的標題。這是一個奇怪的需求。為什么說奇怪了,標題變化帶來的URL變化...

外貿(mào)網(wǎng)站的需求,自動獲取網(wǎng)頁內(nèi)容中的h2標簽并添加到側(cè)邊欄作為定位
外貿(mào)網(wǎng)站的需求,自動獲取網(wǎng)頁內(nèi)容中的h2標簽并添加到側(cè)邊欄作為...

如題:客戶提出這樣一個奇怪的需求。自動獲取網(wǎng)頁內(nèi)容中的h2標簽并添加到側(cè)邊欄作為定位,然后點擊側(cè)邊欄的...

pbootcms占用讓服務器卡頓的問題。
pbootcms占用讓服務器卡頓的問題。

PBOOTCMS在運行時間久了之后,有可能會造成服務器卡頓。通過慢日志發(fā)現(xiàn),是file.php中的path_delete() 函數(shù)...

開啟外貿(mào)時代,歡迎留言!
留言您的聯(lián)系方式,我們會第一時間聯(lián)系您!

Top