News:

Wondering if this will always be free?  See why free is better.

Main Menu

get the width of a string in pixels? (php)

Started by Windy, October 27, 2006, 10:47:48 AM

Previous topic - Next topic

Windy

All unsolicited PM's will be ignored.  Any support requests should go in their topics.

My Mods


Image Zoom Tag
Image Quote Removal
Color Picker
Additional Polls
Simple Awards System

Mods are only updated to the latest same major version of smf on request.

Daniel15

You can do this using the GD library, but it requires you to have the font available as a .TTF file:


<?php

function fixbbox($bbox) {
   
$xcorr=0-$bbox[6]; //northwest X
   
$ycorr=0-$bbox[7]; //northwest Y
   
$tmp_bbox['left']=$bbox[6]+$xcorr;
   
$tmp_bbox['top']=$bbox[7]+$ycorr;
   
$tmp_bbox['width']=$bbox[2]+$xcorr;
   
$tmp_bbox['height']=$bbox[3]+$ycorr;
   
   return 
$tmp_bbox;
}

$font 'arial.ttf';
$fontSize 9.5;
$text 'Test 123';

$bbox fixbbox(imagettfbbox($fontSize0$font$text));
echo 
'The text is '$bbox['width'], ' pixels wide';
?>


[/i](don't ask about the fixbbox function, I don't know how it works, as I didn't write it. I just got it from the PHP website, under the user comments)[/i]

Replace arial.ttf with the name of the font, 9.5 with the font size and Test 123 with the text. On my computer, this shows output like:
QuoteThe text is 46 pixels wide

:D
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Windy

All unsolicited PM's will be ignored.  Any support requests should go in their topics.

My Mods


Image Zoom Tag
Image Quote Removal
Color Picker
Additional Polls
Simple Awards System

Mods are only updated to the latest same major version of smf on request.

Advertisement: