Ballad Posted May 19, 2007 Share Posted May 19, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52117-calculate-the-width-of-a-string-in-cm/ Share on other sites More sharing options...
Orio Posted May 19, 2007 Share Posted May 19, 2007 In different screen resolutions the length in cm will change so there's nothing you can do... Orio. Quote Link to comment https://forums.phpfreaks.com/topic/52117-calculate-the-width-of-a-string-in-cm/#findComment-257117 Share on other sites More sharing options...
cooldude832 Posted May 19, 2007 Share Posted May 19, 2007 what you could do is measure it in pixels and then use some basic math to convert n pixels to x where x is a result in metric assuming you can do those conversion for all screen res out there. I am pretty sure php can pick up screen resolution Quote Link to comment https://forums.phpfreaks.com/topic/52117-calculate-the-width-of-a-string-in-cm/#findComment-257139 Share on other sites More sharing options...
utexas_pjm Posted May 19, 2007 Share Posted May 19, 2007 PHP can not know the screen resolution as the client's screen resolution is not sent in with the HTTP request header. You might be able to pass it in via javascript though (shrug). Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/52117-calculate-the-width-of-a-string-in-cm/#findComment-257190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.