how to use this command in php?

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
sptx

how to use this command in php?

Post by sptx »

The command is:
convert i.png -bordercolor white -border 1x1 -matte -fill none -fuzz 20% -draw "matte 0,0 'floodfill'" -shave 1x1 o.png

I use imagemagick by php_imagick.dll. I want use the command in php,but i can't run 'exec' or 'passthru'.

so what I write like this...

$image = new Imagick('i.png');
//what code???
$image->writeImage('o.png');

thanks a lot.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: how to use this command in php?

Post by Bonzo »

Try looking on this site and you may find an example you can use:

http://valokuva.org/?cat=1.
sptx

Re: how to use this command in php?

Post by sptx »

thanks,but i found nothing about how to remove background from image. :(
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to use this command in php?

Post by el_supremo »

I have no way to test this (I haven't installed Imagick and don't use PHP) but I think the code will look something like this:

Code: Select all

	$image = new Imagick('i.png');
	
	// -bordercolor white -border 1x1
	// note that $border is also used later in the floodfill operation
	$border=new ImagickPixel();
	$border->setColor("white");
	$image->borderImage($border,1,1);
	
	// -matte
	$image->setImageMatte(TRUE); 
	
	// -fill none
	$fill=new ImagickPixel();
	$fill->setColor("none");
	$image->setFillColor($fill);
	
	// -fuzz 20% -draw "matte 0,0 'floodfill'"
	$draw = new ImagickDraw();
	// I don't know if 20% is passed as 20 or .2 - try both
	// see http://ca3.php.net/manual/en/function.imagick-mattefloodfillimage.php
	$draw->matteFloodfillImage(0.0,20,$border,0,0);
	
	// Now draw the $draw object on to $image
	$image->drawImage($draw);
	
	// -shave 1x1
	$image->shaveImage(1,1);
	
	$image->writeImage('o.png');
	
	$draw->destroy();
	$border->destroy();
	$fill->destroy();
Pete
sptx

Re: how to use this command in php?

Post by sptx »

Thanks el_supremo.
I tested it. but it can't work.
setFillColor is the ImagickDraw class function, so $image->setFillColor($fill) can't run,
and matteFloodfillImage is the Imagick class function, so $draw->matteFloodfillImage(0.0,20,$border,0,0) can't run. :(
How to fix it?
I change $image->setFillColor into $draw->setFillColor, but it can't work too... :?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to use this command in php?

Post by el_supremo »

Give this a try:

Code: Select all

   $image = new Imagick('i.png');
   
   // -bordercolor white -border 1x1
   // note that $border is also used later in the floodfill operation
   $border = new ImagickPixel();
   $border->setColor("white");
   $image->borderImage($border,1,1);
   
   // -matte
   $image->setImageMatte(TRUE);
   
   // -fill none -fuzz 20% -draw "matte 0,0 'floodfill'"
   // I don't know if 20% is passed as 20 or .2 - try both
   // see http://ca3.php.net/manual/en/function.imagick-mattefloodfillimage.php
   $image->matteFloodfillImage(0.0,20,$border,0,0);

   // -shave 1x1
   $image->shaveImage(1,1);
   
   $image->writeImage('o.png');
I can't find a way to set a fuzz value for a draw operation so I've removed the draw operations and tried to implement the command by using only Imagick and ImagickPixel methods. I'm afraid I don't know enough about Imagick to get any further with this so if this doesn't work I'm out of ideas.

Pete
sptx

Re: how to use this command in php?

Post by sptx »

It's run, but the image has no change...... :(
Thank you very much. :D
Post Reply