php imagick simple vertical or horizontal gradient image

In this post I will show you how to create a simple vertical gradient with php’s imagick object.

First, set some variables for the colors you want. In this example we are creating a simple 2 color gradient on a opaque background. Once you have specified which colors you want for the background, the top of the gradient, and the bottom of the gradient, the script will create a new imagick object using the png image type with the background color you have specified.

Next the script creates another imagick object which will be our gradient. On this new blank canvas, the script uses imagick’s “PseudoImage” function which can take a gradient color as it’s background color when creating the image. The first color is the top color of the gradient and the second color is the bottom color of the gradient.

Once this gradient image is created, the script then composites the gradient image on top if the first image you created. I have tried many different constants for imagick’s “compositeImage” function, but the one we will use for this example is “imagick::COMPOSITE_OVER” as we are just combining the two images without any fancyness. You can experiment with other constants such as “imagick::COMPOSITE_SCREEN” or “imagick::COMPOSITE_MULTIPLY” which behave much like photoshop layers. You can find all the available composite constants here.

Once the two images are combined, you can spit out the resulting image to the browser. The image in this example is opaque, but if you were to set the background color to the string “transparent” and one of the two gradient images to “transparent” you would get a png image with a transparent gradient.

If you want a horizontal gradient, simply uncomment one of the two “rotateImage” lines. The first will rotate the image counterclockwise, placing the color specified for the “top” of the gradient to the right, the second will rotate the image clockwise, placing the “top” color to the left side of the image.

If you wanted to create a gradient that was at an arbitrary angle, you would have to first make the image larger than you wanted, change the second parameter of the “rotateImage” function to the angle you want, and then use the “cropImage” function to get the gradient inside the bounds of the rotated rectangle which was just rotated. It might take some trial and error to get your crop bounds right, but that’s the general concept.

<?
 
$background = "#FFFFFF";
$top = "#73AEE5";
$bottom = "#003C74";
 
//$cachefile = 'path/to/your/cachefile.png';
//if (!file_exists($cachefile)) {
 
	try{
 
		$canvas = new Imagick();
		$canvas->newImage(80, 80, $background, "png");
 
		$gradient = new Imagick();
		$gradient->newPseudoImage(80, 80, "gradient:".$top."-".$bottom);
		$canvas->compositeImage( $gradient, imagick::COMPOSITE_OVER, 0, 0 );
 
		//$canvas->rotateImage($background, 90);
		//$canvas->rotateImage($background, -90);
		//$canvas->rotateImage($background, 45);
 
		//$canvas->cropImage(height, width, x, y);
		//$canvas->cropImage(50, 50, 30, 30);
 
		header( "Content-Type: image/png" );
		echo $canvas;
 
		//$canvas->writeImage($cachefile);
 
		$canvas->clear();
		$canvas->destroy();
 
		$gradient->clear();
		$gradient->destroy();
 
	}catch(Exception $e){
		echo 'Error: ',  $e->getMessage(), "";
	}
 
/*
}else{
 
	$canvas = new Imagick($cachefile);
	header("Content-Type: image/png");
	echo $canvas;
 
}
*/
?>

NOTE: I have commented out the code which will cache the image, but you can uncomment it if you want to save CPU cycles =) “$cachefile” path must be writable by the server.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>