Ramon Ramon - 16 days ago 6
PHP Question

How do I change the color of a caption drawn with PHP Imagick's newPseudoImage function?

I am creating an image with a caption using the

Imagick::newPseudoImage
function as follows:

$txt = new Imagick();
$txt->setFont("templates/fonts/Gloria.ttf");
$txt->setGravity(imagick::GRAVITY_CENTER);
$txt->newPseudoImage( $image_width, $image_height, "caption:" . $text );


This draws a black caption. I want to customize the color of this caption. I know there are other methods of drawing text with Imagick. I need to use the
newPseudoImage
with caption instead of these other methods because it automatically wraps and sizes the text to fit in a given rectangle.

Answer

You can use colorizeImage. I hope this can help you:

$im = new Imagick();
$background = new ImagickPixel('none');

$im->setBackgroundColor($background);
$im->setFont("somefont.ttf");
$im->setpointsize(72);

$im->setGravity(Imagick::GRAVITY_CENTER);
$im->newPseudoImage(300, 300, "caption:" . "Put your text" );
$im->colorizeImage('#0000b0',1.0);
$im->setImageFormat("png");

header( "Content-Type: image/png" );
echo $im;
Comments