Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中更改png非透明部分的颜色_Php - Fatal编程技术网

在PHP中更改png非透明部分的颜色

在PHP中更改png非透明部分的颜色,php,Php,我想用php用任何颜色或图像填充png不透明部分 下面是基本图像 下面是目标图像 我使用了以下php代码来填充png的不透明部分 $im = imagecreatefrompng(dirname(__FILE__) . '/images/cat_1.png'); $red = imagecolorallocate($im, 255, 0, 0); imagefill($im, 0, 0, $red); header('Content-type: image/png'); imagepng($

我想用php用任何颜色或图像填充png不透明部分

下面是基本图像

下面是目标图像

我使用了以下php代码来填充png的不透明部分

$im = imagecreatefrompng(dirname(__FILE__) . '/images/cat_1.png');
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
但它给了我以下输出

请帮我完成我的任务


提前感谢。

此行
imagefill($im,0,0,$red)在左上角的坐标(0,0)处用红色填充图像。因此,它从左上角开始填充,并填充所有内容,就像在MSPaint中使用油漆桶一样。你可以使用
imagefill($im,150150,$red)例如(如果150150是中心)。

保存此版本的基本图像:

此图像已保存为索引PNG格式,非常适合颜色替换。在本例中,索引0是猫的颜色,索引1是背景(不理想,但这是GIMP给我的)

在这种情况下:

$img = imagecreatefrompng("cat_1.png");
imagecolorset($img,0, 255,0,0);
imagepng($img); // output red cat

如果您有一个基础图像,可以通过这种方式轻松编辑,则图像编辑通常会变得容易得多;)

使用此函数,它将返回base64图像

public static function colorImage($url, $hex = null, $r = null, $g = null, $b = null)
{

    if ($hex != null) {
        $hex = str_replace("#", "", $hex);
        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));
    }
    $im = imagecreatefrompng($url);
    imageAlphaBlending($im, true);
    imageSaveAlpha($im, true);

    if (imageistruecolor($im)) {
        $sx = imagesx($im);
        $sy = imagesy($im);
        for ($x = 0; $x < $sx; $x++) {
            for ($y = 0; $y < $sy; $y++) {
                $c = imagecolorat($im, $x, $y);
                $a = $c & 0xFF000000;
                $newColor = $a | $r << 16 | $g << 8 | $b;
                imagesetpixel($im, $x, $y, $newColor);
            }
        }
    }
    ob_start();

    imagepng($im);
    imagedestroy($im);
    $image_data = ob_get_contents();

    ob_end_clean();

    $image_data_base64 = "data:image/png;base64," . base64_encode($image_data);

    return $image_data_base64;
}
公共静态函数colorImage($url,$hex=null,$r=null,$g=null,$b=null)
{
如果($hex!=null){
$hex=str#u替换(“#”)、“”、$hex);
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
}
$im=imagecreatefrompng($url);
imagealphabling($im,true);
imageSaveAlpha($im,true);
if(imageistruecolor($im)){
$sx=imagesx($im);
$sy=imagesy($im);
对于($x=0;$x<$sx;$x++){
对于($y=0;$y<$sy;$y++){
$c=imagecolorat($im,$x,$y);
$a=$c&0xFF000000;

$newColor=$a |$r您知道如何应用图案图像而不是红色吗?不太容易,但您可以在所需图案中使用两种以上的颜色,并动态更改这些颜色。