Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 919
Ramon Avatar asked Nov 29 '25 23:11

Ramon


2 Answers

colorizeImage has issues will making a slightly darker version of the text becuase it blends funny with the black. Use clutImage instead.

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

$clut = new Imagick();
$clut->newImage(1, 1, new ImagickPixel('#0000b0'));
$txt->clutImage($clut);
$clut->destroy();
like image 130
Eric Avatar answered Dec 01 '25 12:12

Eric


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;
like image 31
Deskin Avatar answered Dec 01 '25 11:12

Deskin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!