...

View Full Version : PHP Images Functions Part 2



theory
07-12-2005, 02:42 AM
Alright guys, I've written a script that creates a dynamic image with info grabbed from my database.

Here's it is:

http://www.syntheticapparatus.com/test.php?subsid=320

So yeh, my script grabs the subsid value and creates the image accordingly.

Now my question is:

Lemme see if I can word this correctly... I want the user to be able to access the image directly. So instead of going to:

http://www.syntheticapparatus.com/test.php?subsid=320

The user would be able to go to:

http://www.syntheticapparatus.com/320.png

I hope I didn't confuse you too much.

Thanks.

missing-score
07-12-2005, 03:48 AM
im guessing you dont want to save the image, otherwise it would defeat the point of having it dynamic.

What I would do, is create a special directory where dynamic images will be stored, for the sake of easiness I will assume /dynamicimages/, and 'generate_image.php' as the name of your image generation script.

Ok, so you need to get the script to run when the user accesses ***.png, and take the *** value to be the subsid? Try this, although you will need .htaccess (I dont know of any other possible way)...

In the dynamicimages directory, create a file called .htaccess, Just that, nothing else, not "filename.htaccess" or "htaccess.txt", JUST .htaccess, in that file put the following:

(note that the path starts with a /, which is relvant to the root of your web folder, eg: the www directory or the public_html directory. (you get the idea)



ErrorDocument 404 /dynamicimages/generate_image.php


next, in your generate_image.php file, ensure you have the following to get the subsid: (I have put in certain data checks to ensure the user doesnt try and ruin your script)




$request_name = $_SERVER['REQUEST_URI'];
$request_file = basename($request_name);

$name_parts = explode('.');
if(count($name_parts) == 2){
if(is_numeric($name_parts[0])){
$subsid = $name_parts[0];
} else {
// Input is not a number
}
} else {
// Invalid file name type
}



How this works, is that when an a non-existant file is requested, the error document is used in its place, but the requested uri (found in: $_SERVER['REQUEST_URI']) remains the name of the actual filename the user requested. We can use this information in the generate_image.php page to fine the subsid requested.

Do user types: http://yoursite.com/dynamicimages/320.png and $subsid will be set to 320 by the script!

theory
07-12-2005, 03:54 AM
I think I just got it, thanks so much though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum