views:

37

answers:

2

I'm using imagettftext to make a bar graph and at the top of each bar I want to put the value.

I have the following variables for each bar (which are really rectangles)

$x1 $y1 $x2 $y2 $imagesx $imagesy $font_size

Also, The fontsize should decrease as the string length increases.

alt text

+1  A: 

You can calculate the center of your text by using PHP's imagettfbbox-function:

// define text and font
$text = 'some bar label';
$font = 'path/to/some/font.ttf';

// calculate text size (this needs to be adjusted to suit your needs)
$size = 10 / (strlen($text) * 0.1);

// calculate bar center
$barCenter = $x1 + ($x2 - $x1) / 2;

// calculate text position (centered)
$bbox = imagettfbbox($size, 0, $font, $text);
$textWidth = $bbox[2] - $bbox[0];
$positionX = $textWidth / 2 + $barCenter;
$positionY = $y1 - $size;

EDIT: Updated the code to do all the work for you.

elusive
How *exactly* would you use it to get the place to write it so it becomes centered
Mark
I edited my post accordingly. The code calculates the x-axis position of the given text with center alignment `$positionX`.
elusive
Hmm let me re-write the question
Mark
Well, this is an incomplete answer, but you can't expect people to do all of your work for you. `imagettfbbox` is a tool you are definitely going to need for this, as it will give you the projected dimensions of the text. You can use that to center the text relative to each bar.
Pekka
Updated my code to do all the stuff.
elusive
+3  A: 

Do it like this. Remember to place the font file "arial.ttf" in current directory:

<?php
// Create a 650x150 image and create two colors
$im = imagecreatetruecolor(650, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Set the background to be white
imagefilledrectangle($im, 0, 0, 649, 149, $white);

// Path to our font file
$font = './arial.ttf';

//test it out
for($i=2;$i<10;$i++)
    WriteTextForMe($im, $font, str_repeat($i, $i), -140 + ($i*80), 70 + rand(-30, 30), -160 + (($i+1)*80), 150, $black);

//this function does the magic
function WriteTextForMe($im, $font, $text, $x1, $y1, $x2, $y2, $allocatedcolor)
{
    //draw bars
    imagesetthickness($im, 2);
    imagerectangle($im, $x1, $y1, $x2, $y2, imagecolorallocate($im, 100,100,100));

    //draw text with dynamic stretching
    $maxwidth = $x2 - $x1;
    for($size = 1; true; $size+=1)
    {
        $bbox = imagettfbbox($size, 0, $font, $text);
        $width = $bbox[2] - $bbox[0];
        if($width - $maxwidth > 0)
        {
            $drawsize = $size - 1;
            $drawX = $x1 + $lastdifference / 2;
            break;
        }
        $lastdifference = $maxwidth - $width;
    }
    $size--;
    imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
}

// Output to browser
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

It uses imagettfbbox function to get width of the text, then loops over the font size to get correct size, centers it and displays it.

So, it outputs the following:

alt text

shamittomar