如何使用PHP Imagick的newPseudoImage函数更改标题的颜色?

3
我将使用Imagick::newPseudoImage函数创建一张带有标题的图片,具体如下所示:

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

这将绘制一个黑色的标题。我想自定义此标题的颜色。我知道有其他使用Imagick绘制文本的方法。但是,我需要使用newPseudoImage与标题而不是这些其他方法,因为它会自动换行和调整文本大小以适应给定矩形。

2个回答

2

colorizeImage在制作略微较暗的文本版本时存在问题,因为它与黑色混合后效果不佳。请改用clutImage

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

1
你可以使用 colorizeImage。希望这能帮到你:
$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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接