Replace a color with another in an image with PHP

随声附和 提交于 2020-01-12 05:25:27

问题


Can someone help me with a simple script to replace a specific color with another color in an image using PHP? Here is a example (color changed from green to yellow).


回答1:


If you meant using GD library in PHP, you should give a check on imagefilter()

Steps are:

  • Start with a .PNG image, use white for inner, alpha for outer.
  • Use imagefilter($img, IMG_FILTER_COLORIZE, 0, 255, 0)) Where 0,255,0 is your RGB color (bright green in this example)
  • Save the alpha and print out result.

Edit, Working code and clarification.

I meant, using alpha for OUTER of the black lines, and white INSIDE. Here's the sample image:

And here's a working code for colorizing white parts:

header('Content-Type: image/png');

/* RGB of your inside color */
$rgb = array(0,0,255);
/* Your file */
$file="../test.png";

/* Negative values, don't edit */
$rgb = array(255-$rgb[0],255-$rgb[1],255-$rgb[2]);

$im = imagecreatefrompng($file);

imagefilter($im, IMG_FILTER_NEGATE); 
imagefilter($im, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]); 
imagefilter($im, IMG_FILTER_NEGATE); 

imagealphablending( $im, false );
imagesavealpha( $im, true );
imagepng($im);
imagedestroy($im);

Note: We must negate values since colorize only works for non-white parts. We could have a workaround to this by having white-bordered image with black inside.

Note: This code only works for black-border and white-inner images.




回答2:


I guess the answer would be to have multiple versions of the image and then load the correct image depending on the chosen colour?

You could use a switch statement to load the correct image

//get selected colour
    switch ($colour) {
    case "red":
        echo "<img src='RED IMAGE' ";
        break;
    case "blue":
        echo "<img src='blue IMAGE' ";
        break;
    case "green":
        echo "<img src='green IMAGE' ";
        break;
}

Hope this helps.




回答3:


I tried this:

<?php
$imgname = "1.gif";
$im = imagecreatefromgif ($imgname);
$index = imagecolorexact ($im,0,128,0);
imagecolorset($im,$index,240,255,0);
$imgname = "result.gif";
imagegif($im,$imgname);
?>
<img src="result.gif">

And instead of replacing every green pixel I got this (shirt color didn't changed):




回答4:


<?php
header("Content-type: image/png");
$im = imagecreate(200, 200)
imagefill($im, 0, 0, $red);

// above could come from an uploaded image
// find a blue in the image
$newblue = imagecolorclosest($im, 0, 0, 255);
// change it to green
imagecolorset($im, $newblue, 0, 255, 0);
imagepng($im);
imagedestroy($im);
?php>

Here you find closest color to the blue and replace with green.




回答5:


The slow but sure approach, iterating over every pixel.

function ReplaceColour($img, $r1, $g1, $b1, $r2, $g2, $b2)
{
    if(!imageistruecolor($img))
        imagepalettetotruecolor($img);
    $col1 = (($r1 & 0xFF) << 16) + (($g1 & 0xFF) << 8) + ($b1 & 0xFF);
    $col2 = (($r2 & 0xFF) << 16) + (($g2 & 0xFF) << 8) + ($b2 & 0xFF);

    $width = imagesx($img); 
    $height = imagesy($img);
    for($x=0; $x < $width; $x++)
        for($y=0; $y < $height; $y++)
        {
            $colrgb = imagecolorat($img, $x, $y);
            if($col1 !== $colrgb)
                continue; 
            imagesetpixel ($img, $x , $y , $col2);
        }   
}


来源:https://stackoverflow.com/questions/12178874/replace-a-color-with-another-in-an-image-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!