2010-08-21 9 views
7

Quel est le meilleur moyen de remplacer les couleurs transparentes par des blancs dans les images gif et png avec php?Comment supprimer la couleur transparente dans les images?

// 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); 

ne fonctionne pas.

+0

bon, et bien, question – Latze

Répondre

12

Je n'utilise pas vraiment beaucoup GD - je préfère ImageMagick. La méthode suivante fonctionne, mais je ne suis pas sûr si elle est la plus efficace:

// 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

J'aime cette solution car il ne cherche pas remplacer les couleurs si elles sont à moitié transparents, la méthode est rééchantillonnée additivement. –