Imagemagick背景渐变

I've got some basic code working to generate an image with a solid background. However I was wondering how I can make a gradient. This is my code:

<?php
function process($inputdata)
{
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );

/* New image */
$image->newImage(400, 300, $pixel);

/* Black text */
$draw->setFillColor('black');

/* Font properties */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );

/* Create text */
$image->annotateImage($draw, 10, 45, 0, $inputdata);

/* Give image a format */
$image->setImageFormat('png');

/* Output the image with headers */
header('Content-type: image/png');
echo $image;
return;
}

The closest code I could find is something like this:

$gradient = new Imagick();
$gradient->newPseudoImage(400, 300, 'gradient:blue-red');

But I don't know how I then combine the gradient with the text.

Please use compositeImage function. It combine one image into another. Create one Imagick instance with gradient, second one with text and alpha channel background, and combine them into the one.

Reference: http://php.net/manual/en/imagick.compositeimage.php

$text = 'The quick brown fox jumps over the lazy dog';
$width = 1000;
$height = 1000;

$textBackground = new ImagickPixel('transparent');
$textColor = new ImagickPixel('#000');

$gradient = new Imagick();
$gradient->newPseudoImage($width, $height, 'gradient:blue-red');

$image = new Imagick();
$image->newImage($width, $height, $textBackground);

$gradient->setImageColorspace($image->getImageColorspace());

$draw = new ImagickDraw();
$draw->setFillColor($textColor);
$draw->setFontSize( 10 );

$image->annotateImage($draw, 10, 45, 0, $text);

$gradient->compositeImage($image, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
$gradient->setImageFormat('png');

header('Content-type: image/png');
echo $image;