Create Horizontal Gradient With PHP Imagick

Create Horizontal Gradient With PHP Imagick

The PHP imagick extension allows us many useful tools from the ImageMagick library. One of these is teh newPseudoImage method which allows us to create gradients. This is often useful in a variety of situations. The default behaviour is for a vertical gradient where the gradient to begin at the top and work its way down.

I found a need in my application for a horizontal gradient and here provide a simple example snippet fo your use. Note that the rotation is 270 degrees rather than 90 degrees. This is so the gradient colors are true to their notation in the newPseudoImage gradient declaration. That is, from red to black, and not black to red from left to right, just as it is written red-black.


/*** new imagick object ***/
$im = new Imagick();

/*** a new image with gradient ***/
$im->newPseudoImage40200'gradient:red-black' );

/*** rotate the image ***/
$im->rotateImage(new ImagickPixel(), 270);

/*** set the image format to png ***/

header"Content-Type: image/png" );


To see this script in action simply follow this link: