Imagick annotateImage: how to set text position from the top left - php

I'm trying to create a wrapper function around annotateImage to be able to set the exact top and left positions of a given text. The default method sets the y-position from the baseline, which means there has to be a lot of experimentation involved if one wants to draw a text at an exact spot on an image. This is what I mean...
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');
In the above code, the text is invisible because the y position is 0. So to fix this, I've started with the following function where I add an offset of 40 to y...
function addText($image, $draw, $x, $y, $text) {
$y = $y + 40;
$image->annotateImage($draw, $x, $y, 0, $text);
}
addText($image, $draw, 0, 0, 'The quick brown fox'); // draw at 0, 0
But it's not very reliable because it doesn't take into account any factors such as font size, etc...
What's the best way to achieve this?

I've found a solution that works well. If the gravity is set to 'NorthWest', the text tends to keep its x y position from the top left.
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');

Related

Center text on a curve, PHP GD

I'm attempting to wrap text around a curve using the function found below. However, this does not take into account aligning the text centered no matter how wide the text maybe be. As an attempt to solve this problem I calculated the width of the text using a binding box.
$type_space=imagettfbbox($fontSize, 0, $fontname, $text);
$image_width = abs($type_space[4] - $type_space[0]) + 10;
However, from this, I need to work out a way that it can adjust the start $angle based on the width. I've played around with lots of settings, however just can't seem to find the correct maths. Any help would be amazing, thank you!
Curve Function
function imagettftextarc($image, $size, $angle, $x, $y, $r, $color, $fontfile, $text, $dir=false){
$sbox=imagettfbbox($size, 0, $fontfile, ' ');
$sbox=($sbox[2]-$sbox[0])*0.1;
$angle=$angle*M_PI/180;
foreach(preg_split('//u', $text) AS $t){
$px=$x+$r*cos($angle);
$py=$y+$r*sin($angle);
$dirangle=(360-(M_PI/2+$angle)*180/M_PI+($dir?180:0))%360;
imagettftext($image, $size, $dirangle, $px, $py, $color, $fontfile, $t);
$box=imagettfbbox($size, 0, $fontfile, $t);
$dx=$box[2]-$box[0];
$da=abs(asin(($dx+$sbox)/$r));
if($dir){
$angle-=$da;
}else{
$angle+=$da;
}
}
}
Possible Solution
I'm not sure how to execute this but the possibility could be to work out the length of the curve if it was a full 360 circle unwrapped horizontally. Then divide the new width with the unwrapped width to create the value that correlates to how many degrees it needs to be altered.

What is the correct way to determine text coordinates a from bounding box?

Given the result of a call to imagettfbbox(), what is the correct, pixel-perfect point to provide to imagettftext() such that the text will not extend beyond its bounding box?
I am determining the width/height and x/y of the baseline from the bounding box like this:
$box = imagettfbbox($size, $angle, $font, $text);
$boxXCoords = array($box[0], $box[2], $box[4], $box[6]);
$boxYCoords = array($box[1], $box[3], $box[5], $box[7]);
$boxWidth = max($boxXCoords) - min($boxXCoords);
$boxHeight = max($boxYCoords) - min($boxYCoords);
$boxBaseX = abs(min($boxXCoords));
$boxBaseY = abs(min($boxYCoords));
I then draw a filled rectangle on my image of the dimensions of the bounding box:
imagefilledrectangle($image, 0, 0, $boxWidth - 1, $boxHeight - 1, $color);
After that, I draw the text:
imagettftext($image, $size, $angle, $boxBaseX, $boxBaseY, $color, $font, $text);
However, this causes the text to extend beyond the rectangle by a pixel or two. I have seen several attempts to fix this issue on PHP's imagettfbbox() documentation, but they all just suggest substracting a pixel or two here and there, which seems like a hack to me. What's happening here, and why should we need to fudge the numbers to get things right?
I believe there is no perfect way to place text with single-pixel precision on an image based on what imagettfbbox() returns and also using .ttf non-monospaced fonts. Over at the PHP manual many users have posted ways to accomplish this (with and without fudging the numbers); I recommend using jodybrabec's simple function over at the PHP manual, which calculates the exact bounding box. I have tested this one and only in extreme cases is the text positioned at most 1 pixel off in one direction. Nonetheless, if you add some padding (even if it is just 2 or 3 pixels) to your image your text will be within the dimensions of the image 100% of the time.
What happens when you don't subtract one from each of the dimensions in this line:
imagefilledrectangle($image, 0, 0, $boxWidth - 1, $boxHeight - 1, $color);
and instead do this:
imagefilledrectangle($image, 0, 0, $boxWidth, $boxHeight, $color);
The SlightlyMagic HQ Card Generator project renders cards for the strategy card game Magic: the Gathering. The generator is powered by PHP with an advanced text rendering engine built in. I don't know about logic behind the calculations, but the renderer is dead accurate for the purposes of this application. Here's the function that calculates proper bounding boxes (HQ Card Generator 8.x/scripts/classes/font.php):
private function convertBoundingBox ($bbox) {
// Transform the results of imagettfbbox into usable (and correct!) values.
if ($bbox[0] >= -1)
$xOffset = -abs($bbox[0] + 1);
else
$xOffset = abs($bbox[0] + 2);
$width = abs($bbox[2] - $bbox[0]);
if ($bbox[0] < -1) $width = abs($bbox[2]) + abs($bbox[0]) - 1;
$yOffset = abs($bbox[5] + 1);
if ($bbox[5] >= -1) $yOffset = -$yOffset;
$height = abs($bbox[7]) - abs($bbox[1]);
if ($bbox[3] > 0) $height = abs($bbox[7] - $bbox[1]) - 1;
return array(
'width' => $width,
'height' => $height,
'xOffset' => $xOffset, // Using xCoord + xOffset with imagettftext puts the left most pixel of the text at xCoord.
'yOffset' => $yOffset, // Using yCoord + yOffset with imagettftext puts the top most pixel of the text at yCoord.
'belowBasepoint' => max(0, $bbox[1])
);
}
I know this is a little late but imagettfbbox is in points not pixels.
pixel font size in imagettftext instead of point size

PHP imagettftext baseline workaround

I am writing to print text to an image using PHP. However, the function imagettftext() uses the baseline, whereas I need the text vertically centered.
So, I either need a method to print text with y not the distance from top to baseline, but from top to top of bounding box OR I need a method using which I could determine the distance between top of bounding box and baseline.
Apparently, I am confusing you. So, to make it clear: I am aware of the function imagettfbbox(). Using that function I can determine height and width of resulting text box. Its height, however, is utterly useless for vertical alignment when printing with imagettftext(), because the Y parameter is not the distance to the top of the box (or even the bottom, but at least something I could have used having the height) but the distance to the baseline of the text within.
EDIT: Why am I not accepting the latest answer?
See my latest comment below the answer, and use this image as a reference.
I do not know if the answer still interested.However, the imagettfbbox() function give you more information than simply the height and the width of the bounding box. It's designed exactly to return information needed by the imagettftext() to manage the text as you want.
The trick lies in the fact that the coordinates returned from imagettfbbox() are not related to the absolute top left corner, but to the baseline of the font for the particular text. This is the reason because the box is specified in point coordinates, and these are often negative.
In short:
$dims = imagettfbbox($fontsize, 0, $font, $text);
$ascent = abs($dims[7]);
$descent = abs($dims[1]);
$width = abs($dims[0])+abs($dims[2]);
$height = $ascent+$descent;
...
// In the example code, for the vertical centering of the text, consider
// the simple following formula
$y = (($imageHeight/2) - ($height/2)) + $ascent;
This works perfectly for my projects.
Hope this help.
Sorry for english.
Marco.
Not entirely sure what your asking...can you give an example? Perhaps imagettfbbox is what you need?
// get bounding box dims
$dims = imagettfbbox($fontsize, 0, $font, $quote);
// do some math to find out the actual width and height
$width = $dims[4] - $dims[6]; // upper-right x minus upper-left x
$height = $dims[3] - $dims[5]; // lower-right y minus upper-right y
edit: Here is an example of vertically centered text
<?php
$font = 'arial.ttf';
$fontsize = 100;
$imageX = 500;
$imageY = 500;
// text
$text = "FOOBAR";
// create a bounding box for the text
$dims = imagettfbbox($fontsize, 0, $font, $text);
// height of bounding box (your text)
$bbox_height = $dims[3] - $dims[5]; // lower-right y minus upper-right y
// Create image
$image = imagecreatetruecolor($imageX,$imageY);
// background color
$bgcolor = imagecolorallocate($image, 0, 0, 0);
// text color
$fontcolor = imagecolorallocate($image, 255, 255, 255);
// fill in the background with the background color
imagefilledrectangle($image, 0, 0, $imageX, $imageY, $bgcolor);
$x = 0;
$y = (($imageY/2) - ($bbox_height/2)) + $fontsize;
imagettftext($image, $fontsize, 0, $x, $y , $fontcolor, $font, $text);
// tell the browser that the content is an image
header('Content-type: image/png');
// output image to the browser
imagepng($image);
// delete the image resource
imagedestroy($image);
?>

Using PHP GD to create image form text with different fonts

I have been using this simple script to generate images from text:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$text = urldecode($_GET['text']);
$font = 'arial.ttf';
$im = imagecreatetruecolor(400, 30);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);
imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
I call the script with file.php?text=testing script&color=000000
Now I'd like to know how could I generate text with normal and bold fonts mixed in the same image, something like file.php?text=testing <b>script</b>&color=000000
Thanks to dqhendricks for helping me figure this out.
Here's a quick script I wrote, still needs lot of improvements but for the basic functionality it seems to be working fine:
<?php
header('Content-type: image/png');
$color = RgbfromHex($_GET['color']);
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$tmp = $_GET['text'];
$words = explode(" ", $tmp);
$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD
$addSpace = 0;
foreach($words as $word)
{
if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE
if(stristr($word, "<b>"))
{
$font = 'arialbd.ttf'; // BOLD FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
}
else
{
$font = 'arial.ttf'; // NORMAL FONT
$x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
}
$addSpace = 1;
}
imagepng($im);
imagedestroy($im);
function RgbfromHex($hexValue) {
if(strlen(trim($hexValue))==6) {
return array(
hexdec(substr($hexValue,0,2)), // R
hexdec(substr($hexValue,2,2)), // G
hexdec(substr($hexValue,4,2)) // B
);
}
else return array(0, 0, 0);
}
?>
Note: This will only work for "bolding" single words separated by spaces and not for bolding part of a word.
Call the script with file.php?text=testing+<b>script</b>&color=000000
you will need to load an arial-bold font file, and do two separate imagettftext() calls, one with each font you want to use. as for parsing the string to find out which parts you would like to be bold, and where you should print each section of text, this sounds like it will become very complicated code. what are you even using this for? there may be better solutions for accomplishing the same thing.
Addition
Use the return value from the imagettftext() function to determine where the next text print should start.
From documentation:
Returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is lower left, lower right, upper right, upper left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontally. Returns FALSE on error.
Response for:
The parsing wouldn't be a problem, what I don't have clear is how I would find the X position for the second wordr in the example ?text=testing script. I mean how do I know where the first word ends so I can place the second one there with another imagettftext() and a bold font. – Meredith Jan 9 '11 at 2:43
funny how someone took the time to say "see edits for the answer to that" when they coulda just said to explode the string at the spaces, then each word is in an array..
<?PHP
$sentence = "I LOVE giving retarded answers that don't amount to jack!";
$sentence = explode(" ",$sentence );
for($s=0;$s<count($sentence);$s++){
#THERES YOUR INDIVIDUAL WORDS!
$word = $sentence[$s];
}
?>
Your X position would be simply $s for your logic. To get the second word you can do this:
<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>
Yes, i named the $words just for a mental visual effect.
$sentence[1] would be word 2

PHP: creating a smooth edged circle, image or font?

I'm making a PHP image script that will create circles at a given radius.
I used:
<?php
imagefilledellipse ( $image, $cx, $cy, $w, $h, $color );
?>
but hate the rough edges it produces. So I was thinking of making or using a circle font that I will output using:
<?php
imagettftext ( $image, $size, $angle, $x, $y, $color, 'fontfile.ttf', $text );
?>
So that the font will produce a circle that has a smooth edge. My problem is making the "font size" match the "radius size".
Any ideas? Or maybe a PHP class that will produce a smooth edge on a circle would be great!
Thank you.
for quick and dirty anti-aliasing, make the image twice the desired size, then down-sample to the desired size.
$circleSize=90;
$canvasSize=100;
$imageX2 = imagecreatetruecolor($canvasSize*2, $canvasSize*2);
$bg = imagecolorallocate($imageX2, 255, 255, 255);
$col_ellipse = imagecolorallocate($imageX2, 204, 0, 0);
imagefilledellipse($imageX2, $canvasSize, $canvasSize, $circleSize*2, $circleSize*2, $col_ellipse);
$imageOut = imagecreatetruecolor($canvasSize, $canvasSize);
imagecopyresampled($imageOut, $imageX2, 0, 0, 0, 0, $canvasSize, $canvasSize, $canvasSize*2, $canvasSize*2);
header("Content-type: image/png");
imagepng($imageOut);
Clever idea, I like that!
But maybe this PHP class already does the trick: Antialiased filled Arcs/Ellipses for PHP (GD)
In many cases websites need dynamically created images: pie charts, rounded corners, menu buttons, etc. This list is endless. PHP, or more precisely the GD library, provides filled elliptical arcs and ellipses, but they are not antialiased. Therefore I have written a PHP function to render filled antialiased elliptical arcs or filled antialiased ellipses (as well as circles..) with PHP easily. Drawing these filled arcs is now a one-liner.
Cairo does antialiasing well.

Categories