Background floodfill with Imagick

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
colnector

Background floodfill with Imagick

Post by colnector »

My question is regarding creating a ‘fake’ transparent background with floodfill. I’ve managed to find an ImageMagick example but couldn’t figure out yet how to use Imagick to do the same.

Here:
http://www.imagemagick.org/Usage/channels/


convert cyclops.png -bordercolor white -border 1×1 -matte \
-fill none -fuzz 20% -draw ‘matte 0,0 floodfill’ \
-shave 1×1 cyclops_flood_3.png


Thanks :)
colnector

Re: Background floodfill with Imagick

Post by colnector »

ping
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Background floodfill with Imagick

Post by mkoppanen »

Hi,

if cyclops.png has transparent background you could just composite it on color ?
Mikko Koppanen
My blog: http://valokuva.org
colnector

Re: Background floodfill with Imagick

Post by colnector »

mkoppanen wrote:Hi,

if cyclops.png has transparent background you could just composite it on color ?
Sorry, but this doesn't look like an answer. 10x
QooooQ

Re: Background floodfill with Imagick

Post by QooooQ »

Well, the only way I found to reproduce the above was this (yeah 18000 otherwise it won’t work 8-} [at least for me])
<?php
$cyclops= new Imagick("cyclops.png");
$cyclops->setFormat("png");
$cyclops->borderImage("white",1,1);

$cyclops->paintFloodfillImage ( "none" , 18000 , "#ffffff" , 1 , 1 );
$cyclops->shaveImage(1,1);

header("Content-Type: image/png");
echo $cyclops;
?>
But I'm not very happy with this solution

Maybe someone has a more propper translation of the ImageMagick Convert Command ? :shock: ? :shock: ?

My data:
Xampp 1.6.8 (Apache 2.2.9, PHP 5.2.6 + PHP 4.4.9 + PEAR)
php_imagick_st-Q16.dll
WinXp SP3
ImageMagick 6.4.5-4
colnector

Re: Background floodfill with Imagick

Post by colnector »

This seems like a nice start but you've limited yourself to a known color (white) for the background. What's the easiest way to pick the color from an image's corner?

Also, on my dev machine it doesn't work and I get

Code: Select all

Fatal error: Call to undefined method Imagick::paintFloodfillImage()
is this a new addition to Imagick?
QooooQ

Re: Background floodfill with Imagick

Post by QooooQ »

It seems that way
on http://de.php.net/manual/de/class.imagick.php
its listed but not yet commented, so i was confused, if its integrated in imagick or not, but i tried and it worked.

So I guess you simply have to download a new version of imagick and then it should work for you, too

with
ImagickPixel Imagick::getImagePixelColor ( int $x , int $y )
you should be able to get the color of the corner, but I haven't tried this yet
colnector

Re: Background floodfill with Imagick

Post by colnector »

Fortunately, on the production server the version is newer.

This code works quite well for me:

Code: Select all

			$ipColor = $im->getImagePixelColor(1, 1);
			$im->borderImage($ipColor, 1, 1);			
			$im->paintFloodfillImage("#e8f0fc", 12000, $ipColor, 1 , 1);
			$im->shaveImage(1, 1);
BTW: 12000 instead of 18000 worked better for me.

Thanks and good luck :)
QooooQ

Re: Background floodfill with Imagick

Post by QooooQ »

If you want to translate a % value of the ImageMagick convert command into the fuzz value of the paintFloodfillImage function...

0% equals 0
100% quals 65535

so to have a fuzz value of 20 % you need to enter 13107
with 12000 you got very close :D


In general (cite from Mikko):
Opacity values are usually 0.0 - 1.0 and color values are 0 - QuantumDepth (65535)
Post Reply