How to color text and background separatly for Imagick caption?

纵然是瞬间 提交于 2020-01-14 22:59:22

问题


I only want to color the caption text, and not the entire caption box (or background). Before Imagemagick 6.3.7 I could use this code to have a red colored text :

$im->newPseudoImage(300, 300, "caption:" . "Put your text" );
$im->colorizeImage('#ff0000',1.0);

I have upgraded because I need to set a font and a font size with the following code :

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

Now colorizeImage does not work the same way, as it doesnt' color only the caption TEXT but the caption BACKGROUND also ..!

For example if I set a black background and a white text :

$im->newPseudoImage(300, 300, "caption:" . "Put your text" );
$im->setBackgroundColor('black');
$im->colorizeImage('white',1.0);

I have a white background behind a white text, or a white box (the color of the text for the box) !

I tried different things, setBackgroundColor before or after colorizeImage, still the same... I've made lots of researches but found nothing else to color the caption and the background caption separately.

Anybody with an idea to help me ? Thx in advance :)


回答1:


  1. You want a transparent background. Then you will only color the foreground.
  2. Use clutImage to apply the color. colorizeImage has issues with applying a slightly darker color when replacing black.
$im = new Imagick();
$im->newPseudoImage(300, 300, "caption:" . "Put your text" );
$im->setBackgroundColor('transparent');

$clut = new Imagick();
$clut->newImage(1, 1, new ImagickPixel('#ff0000'));
$txt->clutImage($clut);
$clut->destroy();



回答2:


Not sure if this is what you want but this gives me white text on a black background:

$width = '600';
$height = '200';
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFont('arial.ttf');
$draw->setFontSize( 96 );
$fillcolor = new ImagickPixel( "white" );
$draw->setFillColor( $fillcolor );
$draw->setGravity( Imagick::GRAVITY_CENTER );
$bgcolor = new ImagickPixel( "black" );
$text = 'Rubblewebs';
$im->newImage($width, $height, $bgcolor );
$im->annotateImage($draw, 0, 0, 0, $text);
$im->setImageFormat("png");
$im->writeImage( 'text.png' );


来源:https://stackoverflow.com/questions/10750352/how-to-color-text-and-background-separatly-for-imagick-caption

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