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

Каков наилучший способ замены прозрачных цветов белым в изображениях 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);

не работает.

11
задан Qiao 21 August 2010 в 15:52
поделиться

1 ответ

Я не очень часто использую 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);
15
ответ дан 3 December 2019 в 08:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: