Web Hosting Talk







View Full Version : PHP: Updating images on request


IceKickz
11-03-2007, 02:46 PM
Hi there!

I've been deadlocked in some coding (PHP) for a day now and just had to seek out for some more eyes and ears to check if there was anyone who could brainstorm here a little with me... I can't really go into details since it's partly under a NDA.

The problem
Let's use a daily visitors graph as an example... You have a folder with a lot of pictures which shows daily stats, i.eg
Jan01-2007.png, Jan02-2007.png etc etc. I already have a script that generates the information I need to create the picture, but really no routine to update it...

The creation was really just by making a custom 404 redirect from .htaccess (ErrorDocument 404 /myscript.php), so if someone tries to open a picture that doesn't exists, the script creates it. In this example it wouldn't be necessary to update Jan01 after that date, but the generated statistics in the real case will always change...

The problem is having thousands pictures. I don't want to add a php script to the crontab to update them all if they aren't needed. I would want them to be updated everytime someone tries to access that specific picture. I.eg a user goes to somedomain.com/images/Jan01-2007.png. What I wanted to do than was for the script to run a filemtime() (plus i.eg 30 minutes to make it cache it for so long) on the file to get the unix time for the file creation and compare it to the current time(). So if the file was created more than 30 minutes ago, it should recreate it.

Since the script is tied to the 404 error, it would bypass the script when somedomain.com/images/Jan01-2007.png is accessed for the second time; the file exists now, so it should return a 200 http code.

I don't have any problem with the coding part. I've already created all the code for the creation and updating. The problem is redirecting the pictures to the script again when it's accessed. I tried to add ErrorDocument 200 in .htaccess without any luck.

Any suggestions?

zacharooni
11-03-2007, 02:49 PM
You could use mod_rewrite to redirect .jpg requests to a php script for processing, then outputting the image.

01globalnet
11-03-2007, 08:03 PM
Mod_rewrite or an htaccess wrapper.

See http://www.devpapers.com/article/323 (bottom of the page, referencing image processing).

dmspilot
11-04-2007, 02:09 AM
If you don't have mod_rewrite, another idea would be to make sure all requests for images go through a PHP (or whatever language) script. For example, someone wants to look at January 1 stats, they go to www.yoursite.com/stats.php?date=Jan01-2007. The php script checks to see if Jan012007 exists. If it exists, it can either redirect to the actual image, or send an image/jpeg header, open the image, and output the file contents. If the image doesn't exist, and the date is valid, it can create it then...

foobic
11-04-2007, 06:24 AM
If you already have a system for generating new images on demand, and you just want images to be recreated on the next access, why are you making this so complicated? Just delete the old images - anything older than your cache interval.