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