Digital Point Forums
Quick Collect

Go Back   Digital Point Forums > Design & Development > Programming > PHP
Google Analytics
Log In to view
your analytics

Reply
 
Thread Tools
  #1  
Old Feb 29th 2008, 9:42 am
Cobnut's Avatar
Cobnut Cobnut is offline
Champion of the Naaru
 
Join Date: Jul 2007
Posts: 160
Cobnut is on a distinguished road
Calculating string length for use in image

I'm building a simple graphing function and would like to include some labels within the bars of the graph; like this:



I've got tests for the width of the bars that increases/decreases the font number (or doesn't show text at all) but I need some way to limit the length of the text dependent upon the size of the bar itself. Ideally, I'd like to be able to say 'if the text gets within x pixels of the top of the bar, then substr it down to a point where it's within the bar'. As it happens, for this particular project I can be fairly certain that the bars are always going to be around 75% of the chart but I can't control the label text itself and some could be longer than those shown on the example.

I suppose what I'm after is some direct equation between the standard font number in imagestring and the 'size' per character.

Anyone have any ideas?

Jon
__________________
http://www.cobnut.net/

Last edited by Cobnut; Feb 29th 2008 at 9:57 am.
Reply With Quote
  #2  
Old Mar 1st 2008, 12:26 am
RoscoeT's Avatar
RoscoeT RoscoeT is offline
Grunt
 
Join Date: Jan 2008
Posts: 64
RoscoeT is on a distinguished road
I think you should likely do this with CSS rather than in your php code. Font size and pixel size vary from machine to machine.
Reply With Quote
  #3  
Old Mar 1st 2008, 3:44 am
stoli stoli is offline
Grunt
 
Join Date: Feb 2008
Posts: 69
stoli will become famous soon enough
The imagefontwidth function should give you what you want:
http://www.php.net/manual/en/functio...efontwidth.php

Here is a way you could use it to truncate a string to fit in a given image:
php Code:
<?php

$text = "Pack my box with five dozen liquor jugs";
$font = 3; // font size
$outputimage = 1; // 1 to output image, 0 to output text

// create a 140*30 image
$imgwidth = 140;
$imgheight = 30;
$im = imagecreate($imgwidth, $imgheight);

// blue background and white text
$bg = imagecolorallocate($im, 0, 0, 255);
$textcolor = imagecolorallocate($im, 255, 255, 255);

// limit font range
if ($font < 0 || $font > 5) $font = 0;

// truncate the text to fit the image width
$width = strlen($text) * imagefontwidth($font);
while ($width > $imgwidth) {
  $text = substr($text, 0, -1);
  $width = strlen($text) * imagefontwidth($font);
}

// write the string at the top left of image
imagestring($im, $font, 0, 0, $text, $textcolor);

// output the image
if ($outputimage) {
  header("Content-type: image/png");
  imagepng($im);
} else {
  echo $text;
}

?>
Try it with different font sizes, either outputting the text or image to see how it works.
Reply With Quote
Reply

Bookmarks

Thread Tools

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 On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Domain name length vs keywords length FiReaNG3L Keywords 5 Oct 12th 2008 9:42 pm
[HELP] Calculating font/text string width Swefx PHP 5 Jun 26th 2007 5:47 am
javascript string -> php string SedNaX PHP 9 May 14th 2007 11:48 pm
EM and PX question length of string?? thechasboi CSS 3 Jan 6th 2007 1:59 am
Reducing the length of the content string in MagpieRSS baselinej70 XML & RSS 3 Jun 8th 2006 10:21 am


All times are GMT -8. The time now is 1:45 am.