问题
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