Jump to content

[SOLVED] Progress type bar, trouble adding text


MasterACE14

Recommended Posts

hello,

 

I found a simple PHP progress bar and I simply want to add some text in the center of the progress bar.

 

here's the code(relevant code only):

<?php

// creating bar
(int)$barWidth = $percent*$barX/100; 
$img = imagecreate($barX, $barY);
$bgColor		= ImageColorAllocate ($img, $bg_rgb[0], $bg_rgb[1], $bg_rgb[2]);
$barColor	= ImageColorAllocate ($img, $bar_rgb[0], $bar_rgb[1], $bar_rgb[2]);
ImageString($img, "testing", "FFFFFF"); // this is what im trying to add unsuccessfully
ImageFilledRectangle($img, 0, 0, $barWidth, $barY, $barColor);

// output bar
imagepng($img);

// destroy !!!
imagedestroy($img);

?>

 

it all works fine, except in the ImageString() function line it doesn't add the text.

 

besides that the actual bar works fine.

 

Regards ACE

Hey, here is code that i have used way back to create text on an image.

 

 

 

<?php

$colorBlack=imagecolorallocate($image, 0, 0, 0);

imagestring ($image, 3, 200, 10, "hey there", $colorBlack);
?>

 

FONT - Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts)

I have it working now, just gotta center the text now.

 

latest code:

<?php

// creating bar
(int)$barWidth = $percent*$barX/100; 
$img = imagecreate($barX, $barY);
$bgColor		= ImageColorAllocate ($img, $bg_rgb[0], $bg_rgb[1], $bg_rgb[2]);
$barColor	= ImageColorAllocate ($img, $bar_rgb[0], $bar_rgb[1], $bar_rgb[2]);
$colorBlack=imagecolorallocate($img, 255, 255, 255);
ImageString($img, 3, 1, 1, "testing", $colorBlack); 
ImageFilledRectangle($img, 0, 0, $barWidth, $barY, $barColor);

?>

sorry for double post...

 

latest code:

<?php

// creating bar
(int)$barWidth = $percent*$barX/100; 
$img = imagecreate($barX, $barY);
$bgColor		= ImageColorAllocate ($img, $bg_rgb[0], $bg_rgb[1], $bg_rgb[2]);
$barColor	= ImageColorAllocate ($img, $bar_rgb[0], $bar_rgb[1], $bar_rgb[2]);
$textColor=imagecolorallocate($img, 255, 255, 255);
ImageFilledRectangle($img, 0, 0, $barWidth, $barY, $barColor);
ImageString($img, 5, 1, 1, $text, $textColor);

try taking the $barwidth and dividing it by two to get the text in the center of the image.

 

I.e. add:

 

<?php

// creating bar
(int)$barWidth = $percent*$barX/100; 
$img = imagecreate($barX, $barY);
$bgColor		= ImageColorAllocate ($img, $bg_rgb[0], $bg_rgb[1], $bg_rgb[2]);
$barColor	= ImageColorAllocate ($img, $bar_rgb[0], $bar_rgb[1], $bar_rgb[2]);
$textColor=imagecolorallocate($img, 255, 255, 255);

$barCenter = $barWidth / 2;

ImageFilledRectangle($img, 0, 0, $barWidth, $barY, $barColor);
ImageString($img, 5, 1, 1, $text, $textColor);

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.