How to create multigradients with PHP 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
KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

Hi, I already found out how to create radial gradients with transparency in PHP Imagick:

Code: Select all

$gradient->newPseudoImage( $radius*2, $radius*2, "radial-gradient:white-transparent");
Is there a similar way to realize multigradients like shown on http://www.fmwconcepts.com/imagemagick/ ... /index.php? Or, if there isn't, is there an effective way to use Fred's solution as-is within my PHP Imagick project, without writing a tmp-file and reopening it? My aim is, to start the (fully opaque) white not at the center, but at a certain radius.

Thanks for any help! :-)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create multigradients with PHP Imagick?

Post by fmw42 »

I doubt that Imagick has anything like my script. It is not maintained as far as I know any longer and has not even kept up with improvements in Imagemagick. My script can be called from PHP exec().
KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

Re: How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

Hi Fred, thanks for your answer! Ah, now I see that what you did is not just an example calling existing ImageMagick commands, in fact you extended ImageMagick with a complex script. Well, of course then there can't be a PHP Imagick command for that ;-)
KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

Re: How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

Here's my solution:

Code: Select all

$gradient = new ImagickDraw();
$gradient->setFillColor('white');
for( $i=0; $i<100; $i++)
{
	$gr = $innerRadius + $outerRadius/100 * ($i+1);
	$gradient->setFillOpacity((1-($i+1)/100)/15);
	$gradient->circle( $pos[0], $pos[1], $pos[0]+$gr, $pos[1]+$gr);
}
I thought, the fill opacity should sum up to 1 with 100 overlying circles, if I would make it 1/100 - in fact it didn't. That's why I change it in the loop and modified the formula until the result suited my needs.
DJ Mike
Posts: 33
Joined: 2010-06-29T19:07:53-07:00
Authentication code: 8675308

Re: How to create multigradients with PHP Imagick?

Post by DJ Mike »

Append some gradients then arc distort them

Code: Select all

<?php
$file = "radialgrad.gif"; # name of output gif
$w = 200;
$h = 200;

    ### First make a rainbow line 
# new imagick object
$img1 = new Imagick();

# some gradients
$img1->newPseudoImage( 10, 100, 'gradient:red-orange' );
$img1->newPseudoImage( 10, 100, 'gradient:orange-yellow' );
$img1->newPseudoImage( 10, 100, 'gradient:yellow-green' );
$img1->newPseudoImage( 10, 100, 'gradient:green-blue' );
$img1->newPseudoImage( 10, 100, 'gradient:blue-purple' );
$img1->newPseudoImage( 10, 100, 'gradient:purple-red' );

# go back to top of stack
$img1->resetIterator();
$line = $img1->appendImages(true);

$line->rotateImage(new ImagickPixel(), 90);
$line->scaleimage($w, $h, FALSE ); 
$line->distortImage( Imagick::DISTORTION_ARC, array( 361 ), TRUE ); 

# Write final image
$line->writeimages( "$file", TRUE);
header( "location:$file" );
exit;
?>
Image

Animated version: viewtopic.php?f=18&t=18448&p=70903#p70903
DJ Mike's Tutorials: PHP
ImageMagick Functions
http://eclecticdjs.com/mike/tutorials/p ... /index.php
KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

Re: How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

not, what I needed, but very nice :-)
DJ Mike
Posts: 33
Joined: 2010-06-29T19:07:53-07:00
Authentication code: 8675308

Re: How to create multigradients with PHP Imagick?

Post by DJ Mike »

Do you need the colors concentric? Delete
$line->rotateImage(new ImagickPixel(), 90);
DJ Mike's Tutorials: PHP
ImageMagick Functions
http://eclecticdjs.com/mike/tutorials/p ... /index.php
KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

Re: How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

Hi DJ Mike, thank you very much! For anyone else reading this, here's how I made it work for my purpose:

Code: Select all

$gradient = 200;
$opaque = 100;

# new imagick object
$img1 = new Imagick();

# some gradients 
$img1->newPseudoImage( 10, $gradient, 'gradient:transparent-white' );
$img1->newImage( 10, $opaque, 'white' );

# double height for concentric radial gradient
$img1->newImage( 10, $gradient+$opaque, 'white' );

# go back to top of stack
$img1->resetIterator();
$line = $img1->appendImages(true);

# image size and radial distortion
$line->scaleimage($gradient+$opaque, $gradient+$opaque, FALSE ); 
$line->distortImage( Imagick::DISTORTION_ARC, array( 361 ), TRUE ); 

# Show final image
$line->setImageFormat('PNG32'); 
header('Content-Type:image/png'); 
exit( $line);

KorbinianMoser
Posts: 6
Joined: 2014-01-07T10:38:09-07:00
Authentication code: 6789

Re: How to create multigradients with PHP Imagick?

Post by KorbinianMoser »

Sadly I found no way to make this precise :-( So I better keep my draw-100-circles approach.
Post Reply