PHP Imagick how to best fit text annotation - php

I'm adding annotation text to a newPseudoImage which works fine but I'd like to make the text scale to fit the image size.
Any ideas how I might do this?
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFillColor($color);
$draw->setFont($font);
$draw->setFontSize(($width, $height) / 100) * 15);
$draw->setGravity(Imagick::GRAVITY_CENTER);
$im->newPseudoImage($width, $height, "canvas:{$bg}");
$im->annotateImage($draw, 0, 0, 0, $text);
$draw->clear();
$draw->destroy();
$im->setImageFormat('gif');
header("Content-Type: image/gif");
echo $im;

I think you could use the imageftbbox function to help you out.
This will give you the bounding box for a text string, with the given ttf font, size, and angle.
You could create a loop to increase or decrease the font size as long as the text is not fitting the image properly.
<?php
$bbox = imageftbbox(12, 0, 'Arial.ttf', 'This is a test');
$width_of_text = $bbox[2] - $bbox[0];
You could look at the $width_of_text and adjust the font size as long as the font isn't scaled to your liking. Keep in mind, as you increase the font, the width and height will grow.
Depending on what you are trying to scale it to that may help.

I'm facing the same issue and although I've not tried this yet as I'm away from my machine, I'm going to give this a go.
Using the query font metrics function of the class I will be able to get the calculated width of the text and then compare it with the specified width of its container. I'll make adjustments to the font size and repeat until its near enough. You could get it quite accurate this way but bare in mind possible performance issues if you have multiple text items in the image.
On the other hand, if you weren't concerned about styling the text as much you could use caption.

This is a slightly naive solution (I could have used binary search to find the proper font size) , but it works for me.
In my example I want to place text on a box in the image, so I calculate the proper font size with imageftbbox.
$size = $MAX_FONT_SIZE;
while (true){
$bbox = imageftbbox($size, 0, $font, $text );
$width_of_text = $bbox[2] - $bbox[0];
if ($width_of_text > $MAX_TEXT_WIDTH) {
$size -= 1;
}
else {
break;
}
}
$height_of_text = ($bbox[3] - $bbox[1]);
$draw->setFontSize( $size );
$image->annotateImage($draw, $TEXT_WIDTH_CENTER - $width_of_text/2, $TEXT_HEIGHT_CENTER - $height_of_text/2, 0, $text);

Related

Crop/cut-out image based on existing selection/path already drawn on image (ideally via PHP)

I've scoured around the internet a fair bit and I can't seem to find any reference to what I am attempting to achieve... I fear that means I'm probably going about doing something the wrong way, but I'll pose this question here anyways in hopes that maybe I am not.
I would like to take an already generated image that has a rectangular selection already drawn on it via a specific color and a dynamic (but always rectangular) path, and crop or cut-out (and use) the inner area of that rectangular path.
Let's use an image generated by google maps as an example for this:
I thought perhaps the imagemagick library would hold a solution for this, but, I don't know if it's because I haven't quite narrowed down the exact key terms for what I am looking to do exactly, or if it's because it cannot (at least not simply) be done, but I haven't turned up any solutions.
Any solutions, advice, or smacks to the head are welcome.
[Please note that (for now) I would like to operate under the assumption that these images already exist, so any information regarding the pixel coordinates of the relative selection area on the image doesn't exist]
Your problem seems to boil down to this: How do I find a red rectangle in an image?
This is quite an open-ended problem, and could actually be quite difficult to solve. However, if the following assumptions can be made, then the task will be a lot easier:
The rectangle is drawn in pure RGB red (#ff0000).
The rectangle is aligned parallel with the image edges.
The image is saved in a lossless format like PNG.
The image contains no other pixels of this exact colour.
We know the width of the rectangle's edges.
The example you provided seems to tick all these boxes. Since it's stored as an 8-bit indexed color image, the first step would be to convert it into a true color image. This makes it easier to check the pixel values.
Then find the outermost edges of the frame, inset the coordinates by the frame width, and crop the image. Here's some code that will do this for you:
<?php
$src_img = 'er7RT.png';
$frame_color = 0xff0000;
$frame_width = 6;
// Load image and copy to true color image resource
$im = imagecreatefrompng($src_img);
$sw = imagesx($im);
$sh = imagesy($im);
$im1 = imagecreatetruecolor($sw, $sh);
imagecopy ($im1, $im, 0, 0, 0, 0, $sw, $sh);
imagedestroy($im);
// Get outer dimensions of frame.
// Assume the frame color appears nowhere else in the image.
$minx = $miny = 999999;
$maxx = $maxy = -$minx;
for ($x=0; $x<$sw; $x++) for ($y=$sh/20; $y<$sh; $y+=$sh/10) {
if (imagecolorat($im1,$x,$y)==$frame_color) { $minx = $x; break 2; }
}
for ($x=$sw-1; $x>=0; $x--) for ($y=$sh/20; $y<$sh; $y+=$sh/10) {
if (imagecolorat($im1,$x,$y)==$frame_color) { $maxx = $x; break 2; }
}
for ($y=0; $y<$sh; $y++) for ($x=$sw/20; $x<$sw; $x+=$sw/10) {
if (imagecolorat($im1,$x,$y)==$frame_color) { $miny = $y; break 2; }
}
for ($y=$sh-1; $y>=0; $y--) for ($x=$sw/20; $x<$sw; $x+=$sw/10) {
if (imagecolorat($im1,$x,$y)==$frame_color) { $maxy = $y; break 2; }
}
if ($minx>=$maxx || $miny>=$maxy) die("Couldn't locate frame");
// Subtract frame width to obtain crop region
$minx += $frame_width;
$maxx -= $frame_width;
$miny += $frame_width;
$maxy -= $frame_width;
// Create new image with cropped dimensions
$im2 = imagecreatetruecolor($maxx-$minx, $maxy-$miny);
imagecopy ($im2, $im1, 0, 0, $minx, $miny, $maxx-$minx, $maxy-$miny);
// Finish up
header("Content-Type: image/png");
imagepng($im2);
imagedestroy($im1);
imagedestroy($im2);

Drawing a curved line between two points in PHP

I want to draw a simple curved line between two points. More specifically, the top left and bottom right corner of an image of arbitrary size.
I tried using imagearc, but apparently that's not what I'm looking for.
To illustrate what I mean:
I can't find any function to help me along, so any help would be appreciated :)
You could use ImageMagick instead of image gd. Image gd has no build-in support for curves.
If you don't have the possibility to use ImageMagick, you could still use imagesetpixel and create your own curve with a simple de casteljau algorithm
I solved it using imagearc after all.
The trick is to set the bottom left corner as the center, -90° start angle, 0° end angle and double the size of the image:
//GET VARS
$width = $_GET['width'];
$height = $_GET['height'];
//CREATE IMGS
$image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($image, 255, 0, 0);
imagearc( $image,
0, 0, //center point = bottom-left corner
$width*2, $height*2, //size = image size * 2
-90, //top left
0, //bottom right
$color);
//OUTPUT IMAGE
header('Content-Type: image/png');
imagepng($image);
//DESTROY IMAGE
imagedestroy($image);
Looks like this:
http://www.schizosplayground.com/pers/curvedlinetest.php?width=132&height=163
I solved a similar problem by generating a vector with points ($polygon) via any convinient function and then drew a lines inbetween the points:
$numberofpoints=count($polygon)/2-1; // XY coordinates, so points is just half and subtracting the end point
for ($i=0; $i < $numberofpoints;$i++) {
imageline($image, $polygon[2*$i], $polygon[2*$i+1], $polygon[2*$i+2], $polygon[2*$i+3], $Color); // connect two consecutive points with a line
}

PHP GD - Align text center-horizontally and decrease font size to keep it inside the image

Hope you are doing great.
I’m still a newbie with php so after making some reading and while checking some posts here I was able to put some text over an image with the imagecreatefrompng() function using the PHP GD, users will come to a form and they will be able to enter their name and the name will be written over the image , unfortunately I have been unable to align the text center horizontally, I tried all ways possible (my ways obviously and must be wrong) with imagettfbbox but I failed in all my attempts, could you please guys help me out a little bit to align the string center horizontally? Also since I’m using a kind of alternative big font I need that the size decrease if the name entered is kind of long so this way it will not surpass the image limits and will stay at the center. I’m getting the value of the text from a form as you may check at the beginning of my code:
<?php
$nombre=$_POST['nombre'];
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('fabian.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'fabian.TTF';
// Set Text to Be Printed On Image , I set it to uppercase
$text =strtoupper($nombre);
// Print Text On Image
imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);
// Send Image to Browser
imagepng($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
Your help will be highly appreciated, later on I will break my head trying to save the image by clicking a submit button since I do not want the users to save the image by right clicking on it.
Thanks pals!
You need the width of the image and the width of the text to relate both.
// get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");
// find font-size for $txt_width = 80% of $img_width...
$font_size = 1;
$txt_max_width = intval(0.8 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size, 0, $font_path, $text);
$txt_width = $p[2] - $p[0];
// $txt_height=$p[1]-$p[7]; // just in case you need it
} while ($txt_width <= $txt_max_width);
// now center the text
$y = $img_height * 0.9; // baseline of text at 90% of $img_height
$x = ($img_width - $txt_width) / 2;
imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);
You can use stil/gd-text class to align the text. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$jpg_image = imagecreatefromjpeg('fabian.jpg');
$textbox = new Box($jpg_image);
$textbox->setFontSize(75);
$textbox->setFontFace('fabian.TTF');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
50, // distance from left edge
50, // distance from top edge
200, // textbox width
100 // textbox height
);
// text will be aligned inside textbox to center horizontally and to top vertically
$textbox->setTextAlign('center', 'top');
$textbox->draw(strtoupper($nombre));
However it is not a complete answer, because it can't decrease font size automatically.

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);
?>

help me to write text on the center of image using PHP GD

This one is empty image before writing
Here is my code
<?php
function LoadJpeg($imgname)
{
$im = #imagecreatefromjpeg($imgname);
$grey = imagecolorallocate($im, 255, 255, 0);
// The text to draw
$text = 'http://www.stackoverflow.com';
// Replace path by your own font path
$font = 'CONSOLA.TTF';
list($width, $height) = getimagesize($imgname);
// wants to know how to use this width/height dynamically //
imagettftext($im, 20, 45, 200, 450, $grey, $font, $text);
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('Blue_hills.jpg');
imagejpeg($img);
imagedestroy($img);
?>
Image after writing text on it
What I want is vertically and horizontally center the text on 45 degree. Please help me on this. Thanks for you all.
Use the imagettfbbox function to retrieve the dimensions the text rendering would require. Then use that information to calculate the x,y coordinate you should target within the destination image for the text to be centered respective to the width/height.
I know very little about PHP (I'm making this up as I go along).
I'd recommend making a new square image, setting it to have a transparent background with imagecolortransparent(). Then write the text to the transparent image.
Next I'd try using imagecopyresized() to copy and scale the text to the new window. Use the minimum of the original's height and width for the destination size. The offset would be something like (max($width, $height)-min($width, $height))/2. Apply the offset to whichever dimension is greater.

Categories