Calculate string width using some font.

This is a discussion on Calculate string width using some font. within the PHP General forums, part of the PHP Programming Forums category; Hey list! :) I've got a problem trying to calculate a string width using an X font with some Y ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-05-2007
Juan Felipe Alvarez Saldarriaga
 
Posts: n/a
Default Calculate string width using some font.

Hey list! :)

I've got a problem trying to calculate a string width using an X font
with some Y font size to use in an svg-to-pdf conversion.

This is what I try to do:
1. Create a dummy image.
2. Set the text there using font X and the font size Y (it seems that
when I create the image it renders it at 72dpi so I need to reduce it at
300dpi)

I'm not sure what the imagettftext() function returns, are those values
measured in pixels ? I read in some page that the proportion to reduce
the image at 300dpi is reduce the size to 24% of its original size.

http://www.printingforless.com/resolution.html

That's the code:

***

// Create dummy image.
$rsc_image = imagecreate( 1, 1 );

// Set image.
$arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
"./fonts/{$str_font_file}", $str_variable_value );
// Destroy dummy image.
imagedestroy( $rsc_image );

// Set structure of widths.
// TODO: Magik numbers.
$arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) / 100 );

***

Thx for any help.
Reply With Quote
  #2 (permalink)  
Old 02-05-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

Juan Felipe Alvarez Saldarriaga wrote:
> Hey list! :)


my name's not list but what the heck:

take a look at these function, they should light the way:

http://php.net/manual/en/function.imageftbbox.php
http://php.net/manual/en/function.imagepsbbox.php
http://php.net/manual/en/function.imagettfbbox.php
http://php.net/imageloadfont

everything is pixels.

there is no dpi - your screen & printer has a certain dpi and that determine
how big the image displays on either.

okay not much help - I had brain freeze half thru.

>
> I've got a problem trying to calculate a string width using an X font
> with some Y font size to use in an svg-to-pdf conversion.
>
> This is what I try to do:
> 1. Create a dummy image.
> 2. Set the text there using font X and the font size Y (it seems that
> when I create the image it renders it at 72dpi so I need to reduce it at
> 300dpi)
>
> I'm not sure what the imagettftext() function returns, are those values
> measured in pixels ?


yes.

> I read in some page that the proportion to reduce
> the image at 300dpi is reduce the size to 24% of its original size.
>
> http://www.printingforless.com/resolution.html
>
> That's the code:
>
> ***
>
> // Create dummy image.
> $rsc_image = imagecreate( 1, 1 );
>
> // Set image.
> $arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
> "./fonts/{$str_font_file}", $str_variable_value );
> // Destroy dummy image.
> imagedestroy( $rsc_image );
>
> // Set structure of widths.
> // TODO: Magik numbers.
> $arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) / 100 );
>
> ***
>
> Thx for any help.
>

Reply With Quote
  #3 (permalink)  
Old 02-05-2007
Juan Felipe Alvarez Saldarriaga
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

Jochem Maas wrote:
> Juan Felipe Alvarez Saldarriaga wrote:
>
>> Hey list! :)
>>

>
> my name's not list but what the heck:
>
> take a look at these function, they should light the way:
>
> http://php.net/manual/en/function.imageftbbox.php
> http://php.net/manual/en/function.imagepsbbox.php
> http://php.net/manual/en/function.imagettfbbox.php
> http://php.net/imageloadfont
>
> everything is pixels.
>
> there is no dpi - your screen & printer has a certain dpi and that determine
> how big the image displays on either.
>
> okay not much help - I had brain freeze half thru.
>
>


K, np, Ill try use imagettfbbox() function instead.

Thx.
>> I've got a problem trying to calculate a string width using an X font
>> with some Y font size to use in an svg-to-pdf conversion.
>>
>> This is what I try to do:
>> 1. Create a dummy image.
>> 2. Set the text there using font X and the font size Y (it seems that
>> when I create the image it renders it at 72dpi so I need to reduce it at
>> 300dpi)
>>
>> I'm not sure what the imagettftext() function returns, are those values
>> measured in pixels ?
>>

>
> yes.
>
>
>> I read in some page that the proportion to reduce
>> the image at 300dpi is reduce the size to 24% of its original size.
>>
>> http://www.printingforless.com/resolution.html
>>
>> That's the code:
>>
>> ***
>>
>> // Create dummy image.
>> $rsc_image = imagecreate( 1, 1 );
>>
>> // Set image.
>> $arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
>> "./fonts/{$str_font_file}", $str_variable_value );
>> // Destroy dummy image.
>> imagedestroy( $rsc_image );
>>
>> // Set structure of widths.
>> // TODO: Magik numbers.
>> $arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) / 100 );
>>
>> ***
>>
>> Thx for any help.
>>
>>

>
>
>

Reply With Quote
  #4 (permalink)  
Old 02-05-2007
setcookie
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

pdf_stringwidth() may help you

regards fra*


> >> Hey list! :)
> >>

> >
> > my name's not list but what the heck:
> >
> > take a look at these function, they should light the way:
> >
> > http://php.net/manual/en/function.imageftbbox.php
> > http://php.net/manual/en/function.imagepsbbox.php
> > http://php.net/manual/en/function.imagettfbbox.php
> > http://php.net/imageloadfont
> >
> > everything is pixels.
> >
> > there is no dpi - your screen & printer has a certain dpi and that

determine
> > how big the image displays on either.
> >
> > okay not much help - I had brain freeze half thru.
> >
> >

>
> K, np, Ill try use imagettfbbox() function instead.
>
> Thx.
> >> I've got a problem trying to calculate a string width using an X font
> >> with some Y font size to use in an svg-to-pdf conversion.
> >>
> >> This is what I try to do:
> >> 1. Create a dummy image.
> >> 2. Set the text there using font X and the font size Y (it seems that
> >> when I create the image it renders it at 72dpi so I need to reduce it

at
> >> 300dpi)
> >>
> >> I'm not sure what the imagettftext() function returns, are those

values
> >> measured in pixels ?
> >>

> >
> > yes.
> >
> >
> >> I read in some page that the proportion to reduce
> >> the image at 300dpi is reduce the size to 24% of its original size.
> >>
> >> http://www.printingforless.com/resolution.html
> >>
> >> That's the code:
> >>
> >> ***
> >>
> >> // Create dummy image.
> >> $rsc_image = imagecreate( 1, 1 );
> >>
> >> // Set image.
> >> $arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
> >> "./fonts/{$str_font_file}", $str_variable_value );
> >> // Destroy dummy image.
> >> imagedestroy( $rsc_image );
> >>
> >> // Set structure of widths.
> >> // TODO: Magik numbers.
> >> $arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) /

100 );
> >>
> >> ***
> >>
> >> Thx for any help.
> >>
> >>

> >
> >
> >

Reply With Quote
  #5 (permalink)  
Old 02-05-2007
Juan Felipe Alvarez Saldarriaga
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

setcookie wrote:
> pdf_stringwidth() may help you
>
> regards fra*
>
>
>


Yea I saw that too but you know, pdflib is not free.

>>>> Hey list! :)
>>>>
>>>>
>>> my name's not list but what the heck:
>>>
>>> take a look at these function, they should light the way:
>>>
>>> http://php.net/manual/en/function.imageftbbox.php
>>> http://php.net/manual/en/function.imagepsbbox.php
>>> http://php.net/manual/en/function.imagettfbbox.php
>>> http://php.net/imageloadfont
>>>
>>> everything is pixels.
>>>
>>> there is no dpi - your screen & printer has a certain dpi and that
>>>

> determine
>
>>> how big the image displays on either.
>>>
>>> okay not much help - I had brain freeze half thru.
>>>
>>>
>>>

>> K, np, Ill try use imagettfbbox() function instead.
>>
>> Thx.
>>
>>>> I've got a problem trying to calculate a string width using an X font
>>>> with some Y font size to use in an svg-to-pdf conversion.
>>>>
>>>> This is what I try to do:
>>>> 1. Create a dummy image.
>>>> 2. Set the text there using font X and the font size Y (it seems that
>>>> when I create the image it renders it at 72dpi so I need to reduce it
>>>>

> at
>
>>>> 300dpi)
>>>>
>>>> I'm not sure what the imagettftext() function returns, are those
>>>>

> values
>
>>>> measured in pixels ?
>>>>
>>>>
>>> yes.
>>>
>>>
>>>
>>>> I read in some page that the proportion to reduce
>>>> the image at 300dpi is reduce the size to 24% of its original size.
>>>>
>>>> http://www.printingforless.com/resolution.html
>>>>
>>>> That's the code:
>>>>
>>>> ***
>>>>
>>>> // Create dummy image.
>>>> $rsc_image = imagecreate( 1, 1 );
>>>>
>>>> // Set image.
>>>> $arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
>>>> "./fonts/{$str_font_file}", $str_variable_value );
>>>> // Destroy dummy image.
>>>> imagedestroy( $rsc_image );
>>>>
>>>> // Set structure of widths.
>>>> // TODO: Magik numbers.
>>>> $arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) /
>>>>

> 100 );
>
>>>> ***
>>>>
>>>> Thx for any help.
>>>>
>>>>
>>>>
>>>
>>>

>
>

Reply With Quote
  #6 (permalink)  
Old 02-05-2007
tedd
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

At 2:54 PM -0500 2/5/07, Juan Felipe Alvarez Saldarriaga wrote:
>setcookie wrote:
>>pdf_stringwidth() may help you
>>
>>regards fra*
>>
>>

>
>Yea I saw that too but you know, pdflib is not free.


Yes, but there's a free version -- Google fpdf.

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #7 (permalink)  
Old 02-06-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

On Mon, February 5, 2007 11:22 am, Juan Felipe Alvarez Saldarriaga wrote:
> 1. Create a dummy image.
> 2. Set the text there using font X and the font size Y (it seems that
> when I create the image it renders it at 72dpi so I need to reduce it
> at
> 300dpi)


When I did a 300-dpi PDF thingie, I defined a variable $resolution
which was 304.8, and multiplied it by the inches I wanted to get 300
dpi out...

I don't know *how* I got the 304.8 number...

> I'm not sure what the imagettftext() function returns, are those
> values
> measured in pixels ?


I believe those are in raw pixels, yes.

> I read in some page that the proportion to reduce
> the image at 300dpi is reduce the size to 24% of its original size.


You'd want to make it 300/72 times as large as you would think?

Only I obviously didn't do that, so I dunno...

> http://www.printingforless.com/resolution.html
>
> That's the code:
>
> ***
>
> // Create dummy image.
> $rsc_image = imagecreate( 1, 1 );
>
> // Set image.
> $arr_ftx = imagettftext( $rsc_image, $int_font_size, 0, 0, 0, -1,
> "./fonts/{$str_font_file}", $str_variable_value );
> // Destroy dummy image.
> imagedestroy( $rsc_image );
>
> // Set structure of widths.
> // TODO: Magik numbers.
> $arr_variable_pixels[$str_variable_index] = ( ( $arr_ftx[2] * 24 ) /
> 100 );


I think you've got the percentage backwards here, as it's 24% to go
from 300 to 72... You want to force a 72 DPI thing to end up being
scalable down by 24% to end up at 300.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
  #8 (permalink)  
Old 02-06-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Calculate string width using some font.

On Mon, February 5, 2007 1:54 pm, Juan Felipe Alvarez Saldarriaga wrote:
> setcookie wrote:
>> pdf_stringwidth() may help you
>>
>> regards fra*
>>
>>
>>

>
> Yea I saw that too but you know, pdflib is not free.


Depends on your usage and if you need the PDI part or not...

PDI == Smush two PDF together.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0