Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.