The online racing simulator
PHP - little help with userbars, please
I know this is not directly an LFS addon question, but some insight would be very appreciated.

We at Fun Racing League are trying to do dynamic userbars to be used here in the forums and we've come to a wall. We have a php script that generates and deploys a dynamic userbar containing some driver data fetched from the DB.

The thing is, right now that script receives an lfs username and generates the userbar at that moment and delivers it. I thought that would be very resource consuming as the userbars would be generated time and time again for no reason.

So I thought the right way would be to generate/update all userbars on a certain event, that is when we upload new race results (at that moment userbars must be updated). Then all those userbars are saved into a folder as they are created as png images. Then we have a php file that translates a request to a png image. For example:
1.- Race results are uploaded (in this process userbars are automatically generated and saved into the directory http://www.funracingleague.com/images/ubars
2.- Then we have a php file like this: http://www.funracingleague.com/userbars.php?lfsname=makakazo that the only thing it does is to return this image http://www.funracingleague.com/images/ubars/makakazo.png

I think this is the right way to do it. Anyway, right now I don't know exactly how to do some of the things. For example I don't know which functions should be used to store the png generated userbars in the web server, and also I don't know how the php file in point (2.-) does the translation from a GET request to the actual userbar stored. I don't have much knowledge of HTML, though I think that these things can be done rather easily.

Am I on the right path? Any suggestions/tips?

Thanks in advance
It depends, at first it does matter how you generate the pngs.
With gd, which is not the fastest but very reliable, the function imagepng can be used to write the image to a certain location.
Second is you can get the get request with superglobal array $_GET.
So http://www.funracingleague.com/userbars.php?lfsname=makakazo
can be retrieved via $_GET['lfsname'], but be sure to strip any tags and quote it for use in database statements.

To pump a file from disk to the browser u can use readfile.
Send the header for the png first to inform the browser of the data type eg. header('Content-type: image/png');.
Thats basically it.

A better solution however would be the use of mod_rewrite.
It depends on the host if it is available.
With mod_rewrite you can rewrite requests to the webserver via regular expressions.

A .htaccess for your problem should be a in a seperate folder and look like this.

RewriteEngine On
RewriteRule ^(.*)\.png$ userbar.php?lfsname=$1

This would call userbar.php everytime a png is requested. This way userbar.php can directly choose between generating a new png or deliver an already generated one.

The fastest solution from my experience would be somethink like this.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.png$ userbar.php?lfsname=$1

It says if the requested file is there deliver it to the browser, if not call the script to generate and deliver it.
Now a second script can be run via cron at a certian interval to delete the already generated ones, forcing the generation of new pngs.
Thanks for the quick reply

That's a lot of info and I'm quite tired now. Tomorrow I'll take my time to analyse everything in detail, and I'll check if we have availability in the host to use mod_rewrite and .htaccess files. I think with this we'll be able to eventually get to it via trial/error
Yeah, just ask if there is anything you need to know.
Your user bars only update after the event has ended correct? In this case, why don't you use a php script to read all of the users in the data base, or read only the new result data that you have and update only these users' user bars.

For example, should there be MaKaKaZo, Dygear and yankman in an event on your server. After the race is done, you go to your website, execue a script to update user bars. This script will look for new results in the table and update Dygear's, MaKaKaZo's and yankman's user bar as it has new information on these clients.

This way you only serve the final out put of the file, and you don't have to execute the script every time to generate the image every time. While the script might not be that resource intensive, it being called 10,000 times in a day might slow down your server to a point where you would notice it. It's like doing 2 + 2 over and over again in your head 10,000 times, yeah, it's easy to do but it's just better knowing the answer is 4 and producing that 10,000 times.

You can save the result of your gd image with imagepng like yankman said, and place it into a file with the second paramater.

bool imagepng (resource $image [, string [b]$filename[/b] [, int $quality [, int $filters ]]])


<?php 
$png 
imagecreatefrompng('./stdBackgound.png');

// Place data on to the userbar.

imagepng($im"{$username}.png");

imagedestroy($im);
?>

I have "finished" working on this userbar stuff but I can't try it myself, my admin pal has to upload the files and do the testing on a mirror image of the site (I don't have a dump). I think it might actually work on the first try.

I just wanted to post the contents of the final php file that is supposed to receive requests and outputs the corresponding userbar. Isn't this a little bit too simple? Even before trying I have a feeling this might not work.

<?php 
php

// array nome
$uname strip_tags($_GET['nome']);

//Header e output
header('Content-type: image/png');

if (
file_exists("/userbars/$uname.png"))
{    
    
readfile("/userbars/$uname.png");
}
else
{
    
readfile("/userbars/supporter.png");
}


?>

PS: If everything goes OK we'll try and investigate on the mod_rewrite and .htaccess way to do it so it looks better



@ Dygear:

I'm already doing everything you say The same script that uploads the results to the DB calls the function to generate/update the userbars for each driver who took place in that race. Anyway, we have to call the script once for everybody in the DB to generate all userbars based on the data collected so far.

an
Quote from Dygear :

<?php 
$png 
imagecreatefrompng('./stdBackgound.png');

// Place data on to the userbar.

[B]imagepng($im"{$username}.png");[/B]

imagedestroy($im);
?>


Do I need to use "{$username}"? I used just imagepng($im, "$username.png");
I think you can.
I also think it should work, but it not nice way of programming.

Use {$} or ${} to identify variables in strings.

I use string concatenation for variable substitution,

<?php 
readfile
('/userbars/'.$uname.'.png');
?>

just for better reading the code and syntax highlighting.

It is bad you can't test the code yourself.
You should think of a local installation for testing or at least have a local php interpreter to prevent typos.
It looks like it's already working, though we have to do the final tweaks and finishing touches to the base userbar. Thanks guys for your support

EDIT: Anyway... I have noticed that the result obtained when rendering the text is not optimal. I don't know if it's a matter of the library used, the ttf font, or the browser, or a combination. I like the shape and size of the text, but sometimes some letters overlap in a wrong way, or get stretched oddly, causing little imperfections of 1 pixel. Please take a look at the userbar attached and notice the odd separation in the username "YACOMO87" between the letter CO and MO and also in the word PODIUMS in several places.

Do you know what might be causing this? Also, when I generate this userbars locally with my own WAMP installation that I got to work for tests it doesn't look exactly the same. Id it the GD library acting different in different versions?
Attached images
yacomo87.png

FGED GREDG RDFGDR GSFDG