簡體   English   中英

PHP PNG 24位清潔透明度

[英]PHP PNG 24 bit clean Transparency

我一直在嘗試使用干凈的24位alpha透明度上傳PNG。 經過大量的研究,我已經設法讓它有點工作,但透明度似乎是低質量的8位,你可以在這個截圖中看到:

http://cozomo.com/apple.png

任何有助於實現干凈的PNG上傳和24位平滑透明度調整的幫助將非常感激。 我目前的代碼如下。

if($extension=="png")
    {
        $uploadedfile = $_FILES['photo']['tmp_name'];
        $src = imagecreatefrompng($uploadedfile);
    }

        $dest_x = 1400; 
        $dest_y = 1200;

        if ($width > $dest_x or $height > $dest_y) { 

                if ($width >= $height) { 
                    $fullSize_x = $dest_x; 
                    $fullSize_y = $height*($fullSize_x/$width); 
                } else { 
                    $fullSize_x = $width*($fullSize_y/$height); 
                    $fullSize_y = $dest_y; 
                } 
        }

        $fullSize=imagecreatetruecolor($fullSize_x,$fullSize_y);

    //TEST
    $black = imagecolorallocate($fullSize, 0, 0, 0);
    imagecolortransparent($fullSize, $black);
    //TEST END

    // OUTPUT NEW IMAGES
    imagecopyresampled($fullSize,$src,0,0,0,0,$fullSize_x,$fullSize_y,$width,$height);

    imagepng($fullSize, "/user/photos/".$filename);

    imagedestroy($fullSize);


  [1]: http://i.stack.imgur.com/w8VBI.png

要保存完整的Alpha通道,您必須使用imagesavealpha ,在保存png之前放置它

imagealphablending($fullSize, false);
imagesavealpha($fullSize, true);

以下是修改后的代碼,感謝Musa對於有同樣問題的人

function processPNG($pngImage) {
        $black = imagecolorallocate($pngImage, 0, 0, 0);
        imagecolortransparent($pngImage, $black);
        imagealphablending($pngImage, false);
        imagesavealpha($pngImage, true);
    }    


   if($extension=="png")
{
    $uploadedfile = $_FILES['photo']['tmp_name'];
    $src = imagecreatefrompng($uploadedfile);
}

    $dest_x = 1400; 
    $dest_y = 1200;

    if ($width > $dest_x or $height > $dest_y) { 

            if ($width >= $height) { 
                $fullSize_x = $dest_x; 
                $fullSize_y = $height*($fullSize_x/$width); 
            } else { 
                $fullSize_x = $width*($fullSize_y/$height); 
                $fullSize_y = $dest_y; 
            } 
    }

    $fullSize=imagecreatetruecolor($fullSize_x,$fullSize_y);
    if ($extension == "png") processPNG($fullSize);


// OUTPUT NEW IMAGES
imagecopyresampled($fullSize,$src,0,0,0,0,$fullSize_x,$fullSize_y,$width,$height);

imagepng($fullSize, "/user/photos/".$filename);

imagedestroy($fullSize);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM