Page 1 of 1

Simple Mask creation

Posted: 2008-04-09T00:28:16-07:00
by hankr
I'm trying to emulate this simple Usage example which works fabulously and is exactly what I need:

Code: Select all

convert -size 640x480 xc:none -fill white -draw 'roundRectangle 15,15 624,464 15,15' logo: -compose SrcIn -composite  result.png
I've been beating my head against a wall trying every permutation I can find, and reading every incomplete thread on the forums. Is this even possible with PHP? I'm beginning to think not... Here is some code I have been eternally tinkering with:

Code: Select all

<?php
$logoWand = NewMagickWand();
MagickReadImage( $logoWand, '/tmp/logo.png' );
$maskWand = NewDrawingWand();
DrawSetFillColor( $maskWand, NewPixelWand( 'white' ) );
DrawRoundRectangle( $maskWand, 15,15, 624,464, 15,15 );
DrawMatte( $maskWand, 0, 0, MW_FloodfillMethod );
DrawComposite( $maskWand, MW_SrcInCompositeOp, 0, 0, 640, 480, $logoWand );
// Save composited image
$outWand = NewMagickWand();
MagickNewImage( $outWand, 640, 480 );
MagickDrawImage( $outWand, $maskWand );
MagickWriteImage( $outWand, '/tmp/logo_masked.png' );

// Output to browser
header("Content-Type: image/png");
header("Content-Length: " . filesize('/tmp/logo_masked.png'));
$fh = fopen( '/tmp/logo_masked.png', 'r' );
fpassthru( $fh );
die;
?>

Re: Simple Mask creation

Posted: 2008-04-09T08:17:32-07:00
by el_supremo
This C code fragment replicates your command line. I presume it can be translated into equivalent PHP.

Code: Select all

	PixelSetColor(p_wand,"none");
	MagickNewImage(m_wand,640,480,p_wand);

	PixelSetColor(p_wand,"white");
	DrawSetFillColor(d_wand,p_wand);
	DrawRoundRectangle( d_wand, 15,15, 624,464, 15,15 );
	MagickDrawImage(m_wand, d_wand);

	MagickReadImage(l_wand,"logo:");
	MagickCompositeImage(m_wand,l_wand,SrcInCompositeOp,0,0);

	/* Write the new image */
	MagickWriteImage(m_wand,"mask_result.png");
Pete

Re: Simple Mask creation

Posted: 2008-04-09T10:57:34-07:00
by hankr
Pete,

Thanks so much! This did in fact do the trick. Here is the PHP equivalent of the code:

Code: Select all

<?php
$m_wand = NewMagickWand();
$p_wand = NewPixelWand();
$d_wand = NewDrawingWand();
MagickSetFormat($m_wand, "PNG32");
PixelSetColor( $p_wand, "none" );
MagickNewImage( $m_wand,640,480, $p_wand );

PixelSetColor($p_wand,"white");
DrawSetFillColor($d_wand,$p_wand);
DrawRoundRectangle( $d_wand, 15,15, 624,464, 15,15 );
MagickDrawImage($m_wand, $d_wand);

$l_wand = NewMagickWand();
MagickReadImage($l_wand,"logo:");
MagickCompositeImage( $m_wand, $l_wand, MW_SrcInCompositeOp, 0, 0 );

/* Write the new image */
MagickWriteImage($m_wand,"/tmp/logo_masked.png");
header("Content-Type: image/png");
header("Content-Length: " . filesize('/tmp/logo_masked.png'));
$fh = fopen( '/tmp/logo_masked.png', 'r' );
fpassthru( $fh );
die;
?>