【问题标题】:How to delete transparent color in images?如何删除图像中的透明色?
【发布时间】:2011-04-02 01:45:48
【问题描述】:

用php在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);

不工作。

【问题讨论】:

    标签: php image gd


    【解决方案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);
    

    【讨论】:

    • 我喜欢这个解决方案,因为如果颜色是半透明的,它不会尝试替换颜色,该方法会以附加方式重新采样。
    • 谢谢你,迈克。新文件的文件大小是原文件大小的两倍半是否正常。
    猜你喜欢
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2011-12-23
    • 2013-09-19
    • 2011-12-06
    • 2022-01-20
    • 2021-06-20
    • 2017-02-27
    相关资源
    最近更新 更多