Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Images Functions Part 2

    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.

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

    Code:
    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)

    PHP Code:

    $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!

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I just got it, thanks so much though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •