Imagemagick背景渐变

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.

我有一些基本代码可以生成具有纯色背景的图像。 但是我想知道如何制作渐变。 这是我的代码: p>

 &lt;?php 
function process($ inputdata)
 {
 / *创建一些对象* / 
 $ image = new Imagick(  ); 
 $ draw = new ImagickDraw(); 
 $ pixel = new ImagickPixel('grey'); 
 
 / *新图像* / 
 $ image-&gt; newImage(400,300,$ pixel)  ); 
 
 / *黑色文字* / 
 $ draw-&gt; setFillColor('black'); 
 
 / *字体属性* / 
 $ draw-&gt; setFont('Bookman-DemiItalic'  ); 
 $ draw-&gt; setFontSize(30); 
 
 / *创建文本* / 
 $ image-&gt; annotateImage($ draw,10,45,0,$ inputdata); 
 
  / *为图像提供格式* / 
 $ image-&gt; setImageFormat('png'); 
 
 / *使用标题输出图像* / 
header('Content-type:image / png'); \  necho $ image; 
return; 
} 
  code>  pre> 
 
 

我能找到的最接近的代码是这样的: p>

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

但是 我不知道如何将渐变与文本结合起来。 p> div>

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;