combining color in a text

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
skyjan

combining color in a text

Post by skyjan »

how do i combine color in a text?

something like this

This is my text


currently what i have right now is this
$draw = new ImagickDraw();
$im = new Imagick("thisistheimagefile.jpg" );
$draw->setFont( "Fonts/arial.ttf" );
$draw->setFontSize(12 );
$draw->setGravity( Imagick:: GRAVITY_WEST );


$draw->setFillColor(new ImagickPixel("#40FF80r"));
$im->annotateImage( $draw, 0, 0, 0, "this" );
$im->drawImage($draw);
$draw->setFillColor(new ImagickPixel("#8040BF"));
$im->annotateImage( $draw, 0, 0, 0, "is " );
$im->drawImage($draw);
$draw->setFillColor(new ImagickPixel("#BF00BF"));
$im->annotateImage( $draw, 0, 0, 0, "my text" );
$im->drawImage($draw);


im a little bit lost on what to do , im assuming computing the x coordinate or y coordinate "annotateImage", but its too complicated, since the text entry is a user entry (it has WYSWYG)

TIA
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: combining color in a text

Post by el_supremo »

You need to get the font metrics for each string and accumulate that in the x coordinate for the next annotate.
I don't know how to do this in IMagick but in C it looks like this:
First get the metrics of a space:

Code: Select all

	// Get the FontMetrics of a space
	fm = MagickQueryFontMetrics(mw, dw, " ");
	sx = (int)fm[4];
	if(fm)RelinquishMagickMemory(fm);
The font metrics are returned as an array of 13 double floating point values and fm[4] will be the width of the space. Each time you call font metrics it returns a new array so you have to deallocate it once you're done with it.

Drawing the first string:

Code: Select all

	dx = 10;
	dy = 10;
	MagickAnnotateImage(mw, dw, dx, dy, 0, "this" );
	MagickDrawImage(mw,dw);
	// get the font metrics
	fm = MagickQueryFontMetrics(mw, dw, "this");
	dx += (int)fm[4]+sx;
	if(fm)RelinquishMagickMemory(fm);
Once "this" has been drawn, the dx coordinate is increased by the width of "this" plus the width of a space.

Then the next one:

Code: Select all

	PixelSetColor(pw,"#8040BF");
	DrawSetFillColor(dw,pw);
	MagickAnnotateImage(mw, dw, dx, dy, 0, "is" );
	MagickDrawImage(mw,dw);
	fm = MagickQueryFontMetrics(mw, dw, "is");
	dx += (int)fm[4]+sx;
	if(fm)RelinquishMagickMemory(fm);
and so on.

Pete
Post Reply