PAINT

This tutorial covers Paint using PHP with Image Magick from the command line.
Replace one color
with another by pixel
covers that color only
<?
$IN="SGfish.jpg";
$PAINT="-fill royalblue -draw 'color 0,0 replace'";
$OUT="SGfishPr.jpg";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL

REPLACE NO FUZZ

Add fuzz to widen color
range to be painted
<?
$IN="SGfish.jpg";
$PAINT="-fuzz 5000 -fill royalblue -draw 'color 0,0 replace'";
$OUT="SGfishPrF.jpg";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
REPLACE AND FUZZ
Replace two colors
<?
$IN="SGfish.jpg";
$PAINT="-fuzz 7000 -fill aqua -draw 'color 0,0 replace'";
$PAINT2="-fuzz 15000 -fill fuchsia -draw 'color 16,55 replace'";
$OUT="SGfishPrY.jpg";
exec ("/usr/bin/convert $IN $PAINT $PAINT2 $OUT");
?>
ORIGINAL
REPLACE 2 COLORS
Replace with transparent with matte (gif or png)
<?
$IN="Examples/SGfishPrY.jpg";
$PAINT="-fuzz 7000 -matte -fill none -draw 'color 0,0 replace'";
$OUT="SGfishPrYT.gif";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
TRANSPARENT BG REPLACE
Floodfill transparent with matte (gif or png)
<?
$IN="Examples/BWBflyFL1.jpg";
$PAINT="-fuzz 5000 -fill none -draw 'matte 50,20 floodfill'";
$OUT="BWBflyFL1T.png";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
TRANSPARENT FLOODFILL
Tile background pattern
with IM built in patterns
<?
$IN="Examples/SGfishPrY.jpg";
$PAINT="-fuzz 7000 -tile pattern:left30 -draw 'color 0,0 replace'";
$OUT="SGfishPrYT.png";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
TILE BACKGROUND
<?
$IN="SGfish.jpg";
$PAINT="-fill white -fuzz 5% -floodfill +1+1 '#997766'";
$OUT="SGfishFL.jpg";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
FLOODFILL AND FUZZ
The next three examples are used to show the advantage of bordercolor. I have a black and white butterfly that I want to add color to. First I replaced the white with gold.
<?
$IN="BWBfly.jpg";
$PAINT1="-fuzz 6000 -fill gold -draw 'color 50,0 replace'";
$OUT="BWBflyFL1.jpg";
exec ("/usr/bin/convert $IN $PAINT1 $OUT");
?>
ORIGINAL
REPLACE
Now I want to change the background color. I can't use replace because it will change all the gold in the image. So I will use floodfil. But floodfil only fills the section of color with the same pixel and since the gold is separated by the black I would need to do that in sections if I just used floodfill.
<?
$IN="Examples/BWBflyFL1.jpg";
$PAINT2="-fuzz 6000 -fill '#997766' -draw 'color 50,0 floodfill'";
$OUT="BWBflyFL2.jpg";
exec ("/usr/bin/convert $IN $PAINT2 $OUT");
?>
ORIGINAL
FLOODFILL
Here is where bordercolor is useful. You can draw a border around the image that is the same color as the color you want to replace. Floodfill it all at once, and then shave off the added border.
<?
$IN="Examples/BWBflyFL1.jpg";
$PAINT3="-fuzz 6000 -bordercolor gold -border 3x3 -fill '#997766' -draw 'color 50,0 floodfill' -shave 3x3";
$OUT="BWBflyFL3.jpg";
exec ("/usr/bin/convert $IN $PAINT3 $OUT");
?>
ORIGINAL
BORDERCOLOR
<?
$IN="BWBfly.jpg";
$PAINT="-fuzz 5000 -fill 'rgba(255,0,0,0.3)' -draw 'matte 50,20 floodfill'";
$OUT="BWBflyT.png";
exec ("/usr/bin/convert $IN $PAINT $OUT");
?>
ORIGINAL
TRANSPARENT COLOR
PNG
The transparent png takes
on the color of the background
Same image as above
Alpha transparency with a Hex number is done by adding two more numbers at the end of the Hex number '#RRGGBBAA' like this '#FF000000'. Below are samples of those 2 numbers ranging from 00 transparent to FF full color.
With Dec numbers use an alpha channel: 'rgba(0,255,0,0.1)'. Values go from 0.0 transparent to 1.0 full color.


Below are a zip of a sample Paint script plus a Paint form. You will need to make a temp directory as it outputs images to that Directory. There is also a text version of the form if you prefer that to the zip.

PAINT ZIP

PAINT TEXT