0

Can anyone help me I'm not having much luck I want to try workout the ideal fontsize for my text based on the width of an image it's overlaying and the amount of characters in the text string. could anyone help me with the formula for this.

I got it semi-working but only due to the Infinite monkey theorem!

// gets text1 count 
$charcount = strlen($line1);
// gets text1 size from count
$newsize = ($width / floor($charcount));
$add = ( 20 % $newsize );
$newsize = ($newsize + $add);

// Changes Font Size
$text1->setFontSize( $newsize );
  • Perhaps this other post will help: [Calculate font size to ensure text fits image width](http://stackoverflow.com/questions/10689225/imagettftext-calculate-font-size-to-ensure-text-fits-image-width) – lurker Apr 22 '14 at 16:07
  • I've taken a look- it explains why it would never be perfect as some characters are wider than others. But I still think there should be a formula to calculate a rough size. – user3259342 Apr 22 '14 at 20:20
  • It's GD image as well I need something similar with Imagick - close though! – user3259342 Apr 22 '14 at 20:25

1 Answers1

0

I use something like this based on the width or height of your image:

 resize_font = function () {

   // then play around with the denominator until the font is the size you want 

var fontSize = parseInt($("#your_image_div").width()) / 2 + "%";



   //then apply that to your css for that text div element 

$(".your_text_class").css('font-size', fontsize);



};

        // then hook it to whatever event you want to 

        $(document).ready(resize);

Hope this helps. This trick is handy for making elements resize in relation to the veiwport too. Hope it helps.

briskovich
  • 670
  • 1
  • 11
  • 26
  • Hey- thanks for the code but because this is for use with Imagick and not css then I don't think it will apply. The formula part is relevant. I've just tried it but not much luck the problem is you can get it perfect with the dominator but when you change the image width it doesn't fit again. Not sure where I'm going wrong- maths is not my strong point! ha – user3259342 Apr 22 '14 at 19:29