PHP GD and True Type Font - php

I have a weird issue with TTF and PHP GD 2.x.
I have a size 45px TTF at top position 100. The top of the text is perfect align with a line (in the image).
BUT if I change the size of the font, the text is no longer align with the line.
At size 20px I need to subtract 5 to the top value of the text and if the font is size 300px I need to add 56 to the top value to get the perfect alignment.
And the worse if I change font (another TTF) it's the same but with different number.
Did I miss something. Why I need to change the top value of the text depending of the size of it and why the number I have for one font is not the same as another one?????
And if all that is normal. How can I get a formula to always get the text align on the line.
Here some number....
Font 1 . size 20 = top 95, size 45 = top 100, size 300 = top 156.
Font 2 . size 20 = top 91, size 193 = top 100, size 300 = top 105.
I'm not so good in math....sad
Thanks
Bill

You'd be better off using Imagick, which lets you query the font's metrics. You probably need either the font's x-height, or cap height value, which you can't get from GD.
Once you've got those values, then you can properly calculate the vertical offset you need to line everything up.

Related

How does IMG_CROP_THRESHOLD work compared to IMG_CROP_SIDES?

What is the main difference between IMG_CROP_THRESHOLD and IMG_CROP_SIDES?
I have been trying to roughly crop the background out of a photo of a document, but either way I am not able to get the outcome I am aiming for.
Also, I took the null, 16777215 from https://www.php.net/manual/en/function.imagecropauto.php, but I honestly don't understand what they actually do. I would have expected to put something like 0.1, #FFFFFF to crop out a background that is white or close to it. What is the null, 16777215 all about?
$cropped = imagecropauto($img, IMG_CROP_THRESHOLD, null, 16777215);
$cropped = imagecropauto($img, IMG_CROP_SIDES);
IMG_CROP_SIDES works by automatically calculating the average color of the pixels around the border of an image an crops off anything within 50% of that value. -
Correction
IMG_CROP_SIDES uses gdGuessBackgroundColorFromCorners which essentially finds the distance from the corner colors to the closest color in the existing palette and then uses this distance for pixel selection to crop.
IMG_CROP_THRESHOLD does NOT calculate the background color but it does provide more flexibility in this calculation as it allows the dev to specify the border color and also the threshold. Documentation is poor on the threshold stating it's a percentage however this actually means an integer or float between 0 and 100 (e.g. not 0.25 but 25).
If IMG_CROP_SIDES is cutting into a bright image, use IMG_CROP_THRESHOLD instead.
For example with a 25 threshold - Approx anything lighter than #E8E8E8 is border.
$cropped = imagecropauto($img, IMG_CROP_THRESHOLD, 25, 16777215);
https://github.com/libgd/libgd/blob/167ea1f4f0003f3e9f7ca1e586189e99cf33d47f/src/gd.c#L460
https://github.com/libgd/libgd/blob/1e47a89a65d49a9003d8365da4e26a7c1a32aa51/src/gd_crop.c#L112

imagettftext(): calculate font size to ensure text fits image width

I'm using imagettftext() to write dynamic text on an image and I want it to fit my image width.
How can I calculate the font size by the text lenght?
You can calculate the bounding box of TTF text before outputting it with the imagettfbbox function. Unfortunately there is no direct way of scaling to fit a width, so you'll have to do it yourself.
One way of doing it is to pass the text with a default font size of, say 20, to imagettfbbox and retrieve the width from it. You can then calculate how much smaller or bigger the text should be to fit the size you want by calculating a scale factor:
scale = targetWidth / bboxWidth;
Then draw the text with the proper size:
fontSize = 20 * scale;
using the imagettftext function. Fonts don't scale 100% perfectly this way, but you'll get a very good approximation.
See the documentation of imagettfbox here.
while (itsTooBigAccordingToimagettftext() && $fontSize > 0) {
$fontSize--;
}

Starting point in coordinate systems

I have the following php code:
<?php
$image = imagecreatefrompng("captcha_background.png");
$imgcolor = imagecolorallocate($image, 0, 0, 0);
imagesetthickness($image, 2);
imageline($image, 0, 25, 40, 90, $imgcolor);
?>
The method "imageline" draws a straight line on my image from the coordinates 0 (x) 25 (y) to 40 (x) 90 (y).
The result is the following image:
What I'm confused about is the reverse of the bottom and the top when using coordinate systems in php.
Normally 0 (The starting point) would be in the lower left corner, but when assigning coordinates in the method "imageline" the 0 (Starting point) is located in the upper left corner?
Expected result:
(The image is 300x100 pixels)
Could someone please explain why this is happening?
This is not a mathematical graph. The typical coordinate system used in development (as far as I know) is to have the first quadrant at the lower right. That is, 0x0 is at the top left. This applies to all html elements that have widths and heights (the elements drop down, they do not fall up).
The motivation appears to be the fact that it's hard to tell how much height you have to work with without knowing the absolute height of the image, which you may not know at any given time, and which may change frequently.
That's how the coordinates are defined in GD, nothing to worry about.
http://www.php.net/manual/en/function.imagedashedline.php :
y1: Upper left y coordinate 0, 0 is the top left corner of the image.
I believe this is the standard for the GD image library as they define the natural origin as the top-left corner.

What units should the height and width be for a PHPExcel_Worksheet_Drawing_Object?

I'm generating an Excel file in PHP using the PHPExcel library. I am placing an image in the worksheet.
$objDrawing->setWidthAndHeight(100,100);
What are the dimenions of the height and width of the drawing object (is it %, pixels, mm, cm)?
Appreciate the help.
It's point.
1 point = 1/72 inches.
1 inch = 72 points
1 point = 0.035 centimeters ( or .35 millimeters )
Without checking, I believe that it's pixels, with an assumption of 72dpi when converting to Excel's rather quirky internal units.

save image after some data written on that image

Hi all
i am displaying an image in a div and content in another div.using jquery draggable method, i placed the content in that div on the image. Now i want to save that image with content as an image. Is it possible? please answer this as it is important
It's perfectly possible to do this, but it's not trivial and I won't post all the code necessary to do it. Instead, I'll give you some pointers:
You can't save the added text "as is" client-side. That's not possible. You could take a screenshot of it, but that's probably not what you want.
Instead, you need to save the text value and the position and size of where the text is placed relative to the image.
Use relative values, e.g. x = 0.3242, y = 0.5123, width = 0.5123, height = 0.12, where x = 0, y = 0 is the top left corner of the image and x = 1, y = 1 the bottom right corner, width and height similarily representing a fraction of the image size.
POST this information to the server and recreate the same effect by baking the text into the image using, for example, gd.
For finding the right font size to use, futz around with imagettfbbox until you have found the closest equivalent in size to the target coordinates.
Use imagettftext for writing the text into the image.

Categories