亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用PHP通過壓縮將PNG轉換為JPG?

使用PHP通過壓縮將PNG轉換為JPG?

PHP
慕絲7291255 2019-11-27 14:34:32
我有一堆高質量的PNG文件。我想使用PHP將它們轉換為JPG,因為它的文件較小,同時又保持了質量。我想在網上顯示JPG文件。PHP是否具有執行此操作的功能/庫?質量/壓縮度好嗎?
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

這樣做可以將PNG安全地轉換為白色透明的JPG。


$image = imagecreatefrompng($filePath);

$bg = imagecreatetruecolor(imagesx($image), imagesy($image));

imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));

imagealphablending($bg, TRUE);

imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));

imagedestroy($image);

$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file 

imagejpeg($bg, $filePath . ".jpg", $quality);

imagedestroy($bg);


查看完整回答
反對 回復 2019-11-27
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

請注意您要轉換的內容。JPG不支持Alpha透明度,而PNG則支持。您將丟失該信息。


要進行轉換,您可以使用以下功能:


// Quality is a number between 0 (best compression) and 100 (best quality)

function png2jpg($originalFile, $outputFile, $quality) {

    $image = imagecreatefrompng($originalFile);

    imagejpeg($image, $outputFile, $quality);

    imagedestroy($image);

}

此函數使用GD庫中的imagecreatefrompng()和imagejpeg()函數。


查看完整回答
反對 回復 2019-11-27
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

<?php

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth) {

    $explode = explode(".", $imageName);

    $filetype = $explode[1];


    if ($filetype == 'jpg') {

        $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");

    } else

    if ($filetype == 'jpeg') {

        $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");

    } else

    if ($filetype == 'png') {

        $srcImg = imagecreatefrompng("$imageDirectory/$imageName");

    } else

    if ($filetype == 'gif') {

        $srcImg = imagecreatefromgif("$imageDirectory/$imageName");

    }


    $origWidth = imagesx($srcImg);

    $origHeight = imagesy($srcImg);


    $ratio = $origWidth / $thumbWidth;

    $thumbHeight = $origHeight / $ratio;


    $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);

    imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);


    if ($filetype == 'jpg') {

        imagejpeg($thumbImg, "$thumbDirectory/$imageName");

    } else

    if ($filetype == 'jpeg') {

        imagejpeg($thumbImg, "$thumbDirectory/$imageName");

    } else

    if ($filetype == 'png') {

        imagepng($thumbImg, "$thumbDirectory/$imageName");

    } else

    if ($filetype == 'gif') {

        imagegif($thumbImg, "$thumbDirectory/$imageName");

    }

}

    ?>

這是一個非常好的縮略圖腳本=)這是一個示例:


$ path =原始圖片所在文件夾的路徑。$ name =要為其制作縮略圖的文件的文件名。$ thumbpath =要將縮略圖保存到的目錄的路徑。$ maxwidth = PX中縮略圖的最大寬度,例如 100(將為100px)。


createThumbnail($path, $name, $thumbpath, $maxwidth);


查看完整回答
反對 回復 2019-11-27
  • 3 回答
  • 0 關注
  • 730 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號