2011-04-16 18 views
18

我正在使用允許用戶上傳圖片的腳本。腳本調整大小並將圖像轉換爲JPEG。如何在用透明背景調整/轉換PNG圖像爲JPEG格式時用白色替換黑色背景。

我遇到的問題是上傳帶有透明度的PNG時,得到的JPEG圖像在黑色透明處出現。

如何編輯下面的腳本來替換黑色與白色?它已經爲GIF做了這個,但不是PNG的。

// RESIZE IMAGE AND PUT IN USER DIRECTORY 
    switch($this->file_ext) 
{ 
    case "gif": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefromgif($this->file_tempname); 
     $kek=imagecolorallocate($file, 255, 255, 255); 
     imagefill($file,0,0,$kek); 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "bmp": 
     $file = imagecreatetruecolor($width, $height); 
     $new = $this->imagecreatefrombmp($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "jpeg": 
    case "jpg": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefromjpeg($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 
    } 

    chmod($photo_dest, 0777); 

    return true; 
} 

我試圖編輯的情況下「PNG」:部分以匹配的情況下「GIF」的:代碼,但所得到的JPEG完全是白色。

UPDATE:

我固定它自己。

謝謝大家,爲了貢獻!

我代替:

case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

有:

case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     $kek=imagecolorallocate($file, 255, 255, 255); 
     imagefill($file,0,0,$kek); 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 
+0

這imagecolorallocate做什麼?它爲png接收4個參數,不應該爲alpha通道接收5個參數嗎? – Andre 2011-04-16 19:27:45

+0

我沒有寫代碼,我是小白。我不知道你的問題的答案。抱歉,我無法幫助你幫助我。 – Jeff 2011-04-16 19:43:15

+0

實際上這些都在手冊中。 _imagecolorallocate - 返回表示由給定的RGB分量組成的顏色的顏色標識符._對於alpha透明度,您需要使用'imagecolorallocatealpha',它確實會添加第5個參數:http://php.net/manual/en/ function.imagecolorallocatealpha.php – kasimir 2012-12-06 09:57:07

回答

0

好吧,這個功能是從PHP。因爲我從來沒有在PHP上使用過圖片,所以我不知道它。

因此,圖像由三種顏色組成:RGB(紅色,綠色,藍色)。在PNG的情況下,它也由另一個過濾器,稱爲α,也就是透明度

SO,只爲PNG,您應該切換到imagecolorallocateimagecolorallocatealpha($file,$i,$i,$i,$i);

這應該使您的圖片透明的。這是解決你的問題還是你真的需要他們在白色背景?

編輯: 我還注意到,你在所有情況下都使用imagejpg功能。他們切換到相應的功能,即,imagepng,imagebmp,imagegif

+0

我需要爲所有功能保留imagejpeg,因爲所有新圖像都必須是jpg。我做了你提出的其他修改,但是結果jpg在透明的地方仍然是黑色的。我不需要保持透明度,我只需要將黑色更改爲白色。謝謝! – Jeff 2011-04-16 20:33:00

0

嘗試這樣的(未測試):

case "png": 
    $file = imagecreatetruecolor($width, $height); 
    $new = imagecreatefrompng($this->file_tempname); 
    imagefilledrectangle ($file, 0, 0, $width, $height, imagecolorallocate($file, 0,0,0)) 

(提前繪製在白色背景上的$文件圖像)

此外, for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }部分看起來很奇怪。

+0

謝謝,但它沒有奏效。 – Jeff 2011-04-16 20:49:20

0
switch($image_extension){ 
    case 'gif': 
    case 'GIF': 
    $image_orig_resource = imagecreatefromgif($image_orig_path); 
    break; 
    case 'png': 
    case 'PNG': 
    $image_orig_resource = imagecreatefrompng($image_orig_path); 
    break; 
    case 'jpg': 
    case 'jpeg': 
    case 'JPG': 
    case 'JPEG': 
    $image_orig_resource = imagecreatefromjpeg($image_orig_path); 
    break; 
    default: 
    throw new Exception('Extension not supported'); 
} 

$image_resource = imagecreatetruecolor($width, $height); 
imagefill($image_resource, 0, 0, imagecolorallocate($image_resource, 255, 255, 255)); // white background; 

imagecopyresampled($image_resource, $image_orig_resource, 0, 0, 0, 0, $width, $height, $image_orig_width, $image_orig_height); 

imagejpeg($image_resource, $image_path, 100); // quality: [0-100] 
0

圖片真實色彩後增添線:

$file = imagecreatetruecolor($width, $height); 
    $background = imagecolorallocate($file, 0, 0, 0); 
    imagecolortransparent($file, $background); 
    imagealphablending($file, false); 
    imagesavealpha($file, true); 

這將mailtaining阿爾法所有格式幫助。平如果你沒有得到答案。

相關問題