如何使用PHP更改图像中的颜色

如何使用PHP更改图像中的颜色,php,image,gd,imagefilter,Php,Image,Gd,Imagefilter,我有一个条形码的图像,我想把黑色改成其他颜色,颜色更鲜艳。如何在PHP中执行此操作?如果您的图像是单色的,则可以使用以下函数: $image = imagecreatefromjpeg('filename.jpg'); imagefilter($image, IMG_FILTER_COLORIZE, 0, 0, 255); // make it blue! imagejpeg($image, 'filename.jpg'); 把这个网站的代码和我自己的代码结合起来,我已经找到了答案。享受 fu

我有一个条形码的图像,我想把黑色改成其他颜色,颜色更鲜艳。如何在PHP中执行此操作?

如果您的图像是单色的,则可以使用以下函数:

$image = imagecreatefromjpeg('filename.jpg');
imagefilter($image, IMG_FILTER_COLORIZE, 0, 0, 255); // make it blue!
imagejpeg($image, 'filename.jpg');

把这个网站的代码和我自己的代码结合起来,我已经找到了答案。享受

function updateThumb($image, $newColor) {
    $img = imagecreatefrompng($image);

    $w = imagesx($img);
    $h = imagesy($img);

    // Work through pixels
    for($y=0;$y<$h;$y++) {
        for($x=0;$x<$w;$x++) {
            // Apply new color + Alpha
            $rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y));

            $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
            imagesetpixel($img, $x, $y, $transparent);


            // Here, you would make your color transformation.
            $red_set=$newColor[0]/100*$rgb['red'];
            $green_set=$newColor[1]/100*$rgb['green'];
            $blue_set=$newColor[2]/100*$rgb['blue'];
            if($red_set>255)$red_set=255;
            if($green_set>255)$green_set=255;
            if($blue_set>255)$blue_set=255;

            $pixelColor = imagecolorallocatealpha($img, $red_set, $green_set, $blue_set, $rgb['alpha']);
            imagesetpixel ($img, $x, $y, $pixelColor);
        }
    }

    // Restore Alpha
    imageAlphaBlending($img, true);
    imageSaveAlpha($img, true);

    return $img;
}

function makeThumb($path, $top, $bottom=FALSE) {
    $width = imagesx($top);
    $height = imagesy($top);

    $thumbHeight = $bottom != FALSE ? $height * 2 : $height;

    // Create Transparent PNG
    $thumb = imagecreatetruecolor($width, $thumbHeight);
    $transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
    imagefill($thumb, 0, 0, $transparent);

    // Copy Top Image
    imagecopy($thumb, $top, 0, 0, 0, 0, $width, $height);

    // Copy Bottom Image
    if ($bottom != FALSE) {
        imagecopy($thumb, $bottom, 0, $height, 0, 0, $width, $height);
    }

    // Save Image with Alpha
    imageAlphaBlending($thumb, true);
    imageSaveAlpha($thumb, true);
    header('Content-Type: image/png');
    imagepng($thumb, $path); // save image as png

}

$thumbTop = updateThumb('input/path', array(240,105,15));
函数updateThumb($image,$newColor){
$img=imagecreatefrompng($image);
$w=图像X($img);
$h=imagesy($img);
//通过像素工作
对于($y=0;$y255)$green\U set=255;
如果($blue\u set>255)$blue\u set=255;
$pixelColor=imagecolorallocatealpha($img、$red\U set、$green\U set、$blue\U set、$rgb['alpha']);
imagesetpixel($img,$x,$y,$pixelColor);
}
}
//还原阿尔法
imageAlphaBlending($img,true);
imageSaveAlpha($img,true);
返回$img;
}
函数makeThumb($path、$top、$bottom=FALSE){
$width=imagesx($top);
$height=imagesy($top);
$thumbHeight=$bottom!=FALSE?$height*2:$height;
//创建透明PNG
$thumb=ImageCreateTureColor($width,$thumbHeight);
$transparent=imagecolorallocatealpha($thumb,0,0,0127);
imagefill($thumb,0,0,$transparent);
//复制顶部图像
imagecopy($thumb、$top、0、0、0、$width、$height);
//复制底部图像
if($bottom!=FALSE){
imagecopy($thumb、$bottom、0、$height、0、0、$width、$height);
}
//使用Alpha保存图像
ImageAlphabling($thumb,true);
imageSaveAlpha($thumb,true);
标题('Content-Type:image/png');
imagepng($thumb,$path);//将图像另存为png
}
$thumbTop=updateThumb('input/path',数组(240105,15));

图像的格式是什么?它有半色调吗?不确定这是否是同一个问题,但答案可能会帮助您找到答案:常用的格式是jpeg。如果是其他格式,我认为可以更改为jpeg。谢谢您这么快的回答@它有jpeg造成的人工制品吗?很好的解决方案。我还建议对条形码使用gif或png,因为jpg可能有可能导致代码无法扫描的瑕疵。另外,请确保您测试条形码,因为您使用的颜色可能不会在所有扫描仪上注册。条形码通常是黑白的,因为它对比度高(而且打印成本低)。这个解决方案要求图像是png吗?你能解释一下你的解决方案吗?此解决方案是否要求图像为png?第二个参数是十六进制值吗?这会创建新图像并使用新颜色保存它,还是不会修改现有图像,而只以不同颜色显示给当前用户?你能给出一个使用示例吗?只要rgb使用一个数组来存储rgb值。但是,您可以修改它以生成rgba。自动取款机,我把它设置成保留图像的阿尔法,你的颜色在变化。我最初使用它来更改绘制字符的颜色,更高级的是将眼睛的虹膜更改为不同的颜色。我也使用了png来实现这一点,但如果删除alpha位,您可能可以使用jpeg