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

▼魔方 西西 提交于 2019-12-10 18:32:50

问题


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.


回答1:


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;



回答2:


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();


来源:https://stackoverflow.com/questions/7891013/how-do-i-change-the-color-of-a-caption-drawn-with-php-imagicks-newpseudoimage-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!