drawing with gradients

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mizerydearia

drawing with gradients

Post by mizerydearia »

I would like to draw a circle and to have it filled with a gradient as well as possible having the border/thickness of the circle to have a gradient as well.

I examined http://www.imagemagick.org/Usage/draw/ as well as several other pages but was unable to find any reference to drawing a shape and to have it filled with a gradient.

I am writing a php script to generate an image based on user input. This is what I wrote so far:

Code: Select all

<?
	foreach(${"_" . $_SERVER["REQUEST_METHOD"]} as $k=>$v) $$k=$v;

	// Create image
		$w = 240; $h = 240;
		$im = new Imagick();
		$im->newPseudoImage($w, $h, "gradient:$bgFrom-$bgTo");
		$im->setImageFormat("png");
	// Draw coin border
		$draw = new ImagickDraw();
		$draw->setStrokeColor( new ImagickPixel("blue"));
		$draw->setStrokeWidth(4);
		$draw->setFillColor( new ImagickPixel( "green" ) );
		$draw->circle(120, 120, 38, 38);
		$im->drawImage( $draw );
	// Output image
		header("Content-type: image/png"); echo $im; $im->clear(); $im->destroy();
?>
You can see it in action visiting http://nullvoid.org/bitcoin/big/forumex ... =%23FFFFFF

Currently the background is created with a gradient. The circle, however, is solid green with blue border. How can I produce a circle filled with a gradient?

I stumbled upon viewtopic.php?t=8149 and it seems related, but it also from a few years ago and doesn't offer a solution.
mizerydearia

Re: drawing with gradients

Post by mizerydearia »

bump
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: drawing with gradients

Post by Bonzo »

I would guess you need to create the gradient on a square image and then create a mask to give you the circle with the gradient.
You would then need to composite the two images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: drawing with gradients

Post by fmw42 »

I don't know if this is what you are looking for, but see radial-gradients at http://www.imagemagick.org/Usage/canvas ... l-gradient
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: drawing with gradients

Post by anthony »

linear gradients are posible with draw. But it is not straight forward.

Best idea is to grab the SVG file...
http://www.imagemagick.org/Usage/draw/diagonal.svg
Used for examples in IM examples
http://www.imagemagick.org/Usage/draw/#svg

Now generate the mvg (magick Vector graphics) that this svg produces...

Code: Select all

     convert msvg:diagonal.svg diagonal.mvg
Also shown in that part of IM examples.

This shows how a gradient is defined and used in MVG draw commands.
For a linear gradient in a circle, replace the 'rectangle' draw with a 'circle' draw command.


It is probably time to write up soem examples of drawing with gradients.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply