Question: I want to create an image that has the exact dimensions of a given text and place that text onto that image. The position values I'm getting from imagettfbbox do not seem to be exact, especially for italic font styles. This leads to some characters being cut at the right.

Here is my code:

```
<?php

Header("Content-type: image/png");

$font = 'fonts/ArialItalic.ttf';
$text = "SOB";
$size = 48;

$bounds = imagettfbbox($size, 0, $font, $text);

$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);

$offset_y = $height;
$offset_x = $bounds[0];

$image = imagecreate($width,$height);

$background = ImageColorAllocate($image, 0, 0, 0);
$foreground = ImageColorAllocate($image, 255, 255, 255);

ImageTTFText($image, $size, 0, $offset_x, $offset_y, -$foreground, $font, $text);

imagePNG($image);
imagedestroy($image);

?>

```

What I get from this is [![Cutted edges](https://i.stack.imgur.com/OkMF3.png)](https://i.stack.imgur.com/OkMF3.png)

What I want is: [![Text fitting inside the image](https://i.stack.imgur.com/MoORk.png)](https://i.stack.imgur.com/MoORk.png)

The calculated width and height seem to be correct; however, the X/Y positions ($offset\_x and $offset\_y) are not. 

How can I set $offset\_x and $offset\_y dynamically so the text fits exactly in the image for different fonts, sizes and styles?

Answer: 
OP's `unsigned long` appears to be a 32-bit and so cannot represent the 51-bit `2349872764943587`.

If stuck using a compiler that lacks 64-bit integers, code could use `double`, which typically handles 53-bit whole number values without loss of precision. 

```
double data;
char buf[40] = "data 2349872764943587";

if (sscanf (buf, "data %lf", &data) == 1)
{
    printf ("%s\n", buf);
    printf ("data:\t%.0f\n", data);
}

```

Output

```
data:   2349872764943587

```
For portability, [avoid using](https://matt.sh/howto-c) `int`, `long`, etc. In your platform, for example, `long unsigned int` is 32 bits, which is why you have your problem.

Instead, `#include <stdint.h>` then use standard types, such as `int32_t`, `int64_t`, `uint32_t`, `uint64_t`. In your particular case, `uint64_t` does the trick.

To use `uint64_t data;` in `printf` and `scanf`, you need to `#include <inttypes.h>` and then `printf("%" PRIu64 "\n", data);`

More info [here](https://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c).