Jump to content

Calculate the width of a string in cm


Ballad

Recommended Posts

Hi, I use this script below to se how different texts looks with diffrents fonts.

Now I would like to know how wide the text is, in cm if you enter the hight of the text in cm, or viseversa.

 

I have looked around alot but could´t find a sulition, but I found this forum and maybe someone can help me.

 

/Runar

 

 

 

 

   <?php
header('Content-Type: image/png');


$myImage = imagecreatefrompng($vag);

$path = '/fonts/'; 
if (!$_GET['font']) { $font = "arial"; } 
$usefont = $path . $font . '.ttf';

$rad = $_GET['text'];

$fontcolor = $_GET['fontcolor'];
$fontsize = $_GET['size'];

if (!$fontcolor) { $fontcolor = "ffffff"; } 
if (!$fontsize) { $fontsize = 40; } 

$fc1 = hexdec(substr($fontcolor,0,2));
$fc2 = hexdec(substr($fontcolor,2,2));
$fc3 = hexdec(substr($fontcolor,4,2));

$fontcolor = imagecolorallocate($myImage,$fc1,$fc2,$fc3);

            
$angle = 0;   // Angle of the text
$px = 10;     //X-koordinat were the center of the text start in the image
$y = 42.5;   //Y-koordinat were the center of the text start in the image

               
           // retrieve boundingbox
           $bbox = imagettfbbox($fontsize, $angle, $usefont, $rad);
         
           // calculate deviation
           //$dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;        // deviation left-right
           $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom
           
           // new pivotpoint
           //$px = $x-$dx;
           $py = $y-$dy;


                
          //Text Rad 
          $res = imagesx($myImage);
          $txtsize = imagettfbbox($fontsize,0,$usefont,$rad);
          $txtwidth = abs($txtsize[2]) + abs($txtsize[0]);
          $txt_x = ($res / 2) - ($txtwidth / 2);

         imagettftext($myImage, 40, $angle, $px, $py, $fontcolor, $usefont, $rad);
        
        
        
imagepng($myImage);
imagedestroy($myImage);
?>
      

Link to comment
https://forums.phpfreaks.com/topic/52117-calculate-the-width-of-a-string-in-cm/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.