Adding data to an image file - Can it be done?
06-30-2006, 11:12 AM
|
Adding data to an image file - Can it be done?
|
Posts: 626
|
I have an image file which is rateboard.gif, and I have a database with all my rates in it which is updated on a daily basis.
Now, the file rateboard.gif is just a background and I want to setup a cron job which will automatically take the rates which are saved in my database and input them into the GIF file using the courier font. Then I want to save the new GIF file.
Is this something that can be done using PHP? If so, can anyone point me to a script or tutorial?
|
|
|
|
06-30-2006, 11:52 AM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 16
Location: Seattle, WA
|
Yes, you can use the GD library to manipulate images from PHP (more info here http://us2.php.net/image). On most servers it's already installed.
|
|
|
|
06-30-2006, 12:00 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 16
Location: Seattle, WA
|
Furthermore, some code that seems to be pretty close to what you're looking to do is listed in the comments. (code is here http://us2.php.net/manual/en/ref.image.php#62436)
Modifying this to pull the text you want from a your rates database should be as simple as connecting to the DB, then modifying this line of code ( $text = @$_GET['text']; ) making the $text var point to the output of the database. Hope that helps! =)
|
|
|
|
06-30-2006, 03:14 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 626
|
Ok... Thanks for the help guys.
I have tried it and I can't figure out why but for some reason the red color is working but the blue and black colors aren't working.
Can anyone see why not?
Code:
header("Content-type: image/gif");
$im = imagecreatefromgif("http://localhost/images/moneytime_rates.gif");
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 10, 10 , 10);
$blue = imagecolorallocate($im, 30, 0, 255);
$string = "VARIABLE RATES";
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 25, $string, $red);
imagegif($im);
imagedestroy($im);
|
|
|
|
06-30-2006, 03:25 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 16
Location: Seattle, WA
|
It's because you have not called these variables in the text display...
PHP Code:
imagestring($im, 3, $px, 25, $string, $red, $black, $blue);
that should do it. 
|
|
|
|
06-30-2006, 05:17 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 626
|
Ya lost me on that one! but that's okay, I was able to figure it out by using this:
Code:
header("Content-type: image/gif");
$im = imagecreatefromgif("C:\Documents and Settings\Administrator\desktop\RateBoard copy.gif");
$font = 'arial.ttf';
$bg = imagecolorallocate($im, 255,255,255);
$red = imagecolorallocate($im,255,0,0);
$blue = imagecolorallocate($im, 30,0,255);
$black = imagecolorallocate($im, 0,0,0);
$string = "VARIABLE RATES";
$px = (imagesx($im) - (imagefontwidth(3)*strlen($string)))/2;
imagettftext($im,9,0, $px, 65,$black,$font,$string);
imagegif($im);
imagedestroy($im);
The problem was that the colors weren't in my pallette for some reason when I saved the background. I added the colors to the pallette and now it works fine.
NEW PROBLEM:
Is it possible to add style to the text that goes in the image... For example in some cases I want to add an underline and in others I want to add bold text. Is this possible? I can't find it anywhere on google. Or in the PHP manual
|
|
|
|
06-30-2006, 05:51 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 16
Location: Seattle, WA
|
Quote:
|
Originally Posted by zincoxide
NEW PROBLEM:
Is it possible to add style to the text that goes in the image... For example in some cases I want to add an underline and in others I want to add bold text. Is this possible? I can't find it anywhere on google. Or in the PHP manual
|
That I have no clue about, you may be able to use HTML code, but I've never tried to add style dynamically to an image, it may just output the tags if you use HTML.
Look through PHP.com for info on working with strings, there should be a way to manipulate the text prior to it being output in the image.
|
|
|
|
07-01-2006, 11:04 PM
|
Re: Adding data to an image file - Can it be done?
|
Posts: 626
|
I have decided that I can just draw a line underneath the text... Also for bold I can just change to the bold font.
Thanks again for the help!
|
|
|
|
|
« Reply to Adding data to an image file - Can it be done?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|