yy日韩无码,富婆的诱惑,国产菊爆视频在线观看,国产精品无码AV高清波波AV,国产成人啪精品视频站午夜,已满十八岁免费观看电视剧十八岁,中文字幕av久久人妻蜜桃臀

LOGO
外貿(mào)網(wǎng)站建設(shè),讓業(yè)務(wù)全球可達(dá)
0%
新聞中心 網(wǎng)絡(luò)推廣 網(wǎng)站建設(shè) 服務(wù)器相關(guān) 優(yōu)化推廣 首頁>新聞>網(wǎng)站建設(shè)

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

時(shí)間:2025-06-15   訪問量:0

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

pb要增這個(gè)功能,第一步:找到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 格式(不影響原始文件)
 */


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

找到:

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

后添加:

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

效果如:

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


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

// 添加一個(gè)新的函數(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':
            // 對(duì)于GIF文件,直接返回false,不進(jìn)行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;
}


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

  // 如果是圖片
    if (is_image($file_path)) {
        // 進(jìn)行等比例縮放
        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)) {
        // 進(jìn)行等比例縮放
        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); // 獲取文件站點(diǎn)路徑
    return $save_file;

Ok了,我們現(xiàn)在來測(cè)試一下上傳圖片。下面的圖片是我修改后上傳的,點(diǎn)右鍵,查看源代碼,可以看到圖片已經(jīng)轉(zhuǎn)換成webp格式了。原圖為1.07M,壓縮后為:629KB


parade-3245208_1920.jpg


服務(wù)咨詢
1對(duì)1咨詢,專業(yè)客服為您解疑答惑
聯(lián)系銷售
15899750475
在線咨詢
聯(lián)系在線客服,為您解答所有的疑問
ARE YOU INTERESTED IN ?
感興趣嗎?

有關(guān)我們服務(wù)的更多信息,請(qǐng)聯(lián)系項(xiàng)目經(jīng)理

15899750475 楊先生