Как удалить прозрачный цвет в изображениях?

Каков наилучший способ заменить прозрачные цвета белым в gif и png изображениях с помощью php?

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

не работает.

Я действительно не очень использую 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);