2010-08-21 6 views
7

투명 색상을 gif 및 png 이미지에서 흰색으로 대체하는 가장 좋은 방법은 무엇입니까?이미지에서 투명 색상을 삭제하는 방법은 무엇입니까?

// get transparent color indexes 
$trsp = ImageColorsForIndex($image, ImageColorTransparent($image)); 
// get transparent color set 
$delete = imagecolorallocate($image, $trsp['red'], $trsp['green'], $trsp['blue']); 
// replace 
imagecolorset($image, $delete, 255, 255, 255); 

이 작동하지 않습니다.

+0

좋은, 그리고 그들이 반 투명한 경우 색상을 교체하지 않기 때문에 내가는이 솔루션을 좋아 – Latze

답변

12

저는 GD를 사용하지 않습니다. 나는 ImageMagick을 선호합니다. 다음 방법은 작동하지만, 나는 그것이 가장 효율적입니다 있는지 확실하지 않습니다 :

// Get the original image. 
$src = imagecreatefrompng('trans.png'); 

// Get the width and height. 
$width = imagesx($src); 
$height = imagesy($src); 

// Create a white background, the same size as the original. 
$bg = imagecreatetruecolor($width, $height); 
$white = imagecolorallocate($bg, 255, 255, 255); 
imagefill($bg, 0, 0, $white); 

// Merge the two images. 
imagecopyresampled(
    $bg, $src, 
    0, 0, 0, 0, 
    $width, $height, 
    $width, $height); 

// Save the finished image. 
imagepng($bg, 'merged.png', 0); 
+0

에 의문을 제기하지만 메서드가 가산 적으로 리샘플링됩니다. –

관련 문제