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 |