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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post

    Arrow I think this is PHP... I need to make an image rotator

    that works at the filename level?

    I would like to be able to have an img element with src="x.jpg" (so I must be able to reference the image by a link like "www.domain.com/x.jpg" and have it turn up differently each time.

    I know it can be done (I've seen it before), but the thing is I need to be able to get the effect by using img source of a .png rather than a .php

    Any suggestions?
    Last edited by AndrewRyan; 06-09-2011 at 09:34 AM.

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    write apache rewrite routine for png that will rewrite it to image.php which in turn will serve requested image
    in that way you can rotate or do any other modification on the image in php file and serve it as a png at the end

    i would personally do it using js
    but I suppose it's a matter of specifications and needs
    Examples are not to be copied to servers which are accessible from real world
    Use libs and examples with caution

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by flaMasta View Post
    write apache rewrite routine for png that will rewrite it to image.php which in turn will serve requested image
    in that way you can rotate or do any other modification on the image in php file and serve it as a png at the end

    i would personally do it using js
    but I suppose it's a matter of specifications and needs
    I'm a bit better with JS then I am with PHP, but I'm not sure the best way to go about this.

    If was thinking set a js file to select an image from several, then go to that image, but if I call that script 'x.jpg' or whatever, won't it be read improperly by browsers?

    I think I'm probably misunderstanding how MIME type is determined, but bare with me ^^

    Thanks for the help so far.

  • #4
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    What I mean:
    On server you will have script 'image.php'
    in .htaccess you will have rewrite routine that will start 'image.php' script for each requested image
    that script will then read image originally requested by browser query
    it will rotate it and serve it as a image to the browser

    in that case user's browser will have no idea that any script have ran, thus it will be transparent to user
    browser will simply request image and get image

    I would write you example code that would do it, however I am at work now and today I am leaving for the weekend.. I ll try to explain in more detail or forward you to some resources if I find some time
    Examples are not to be copied to servers which are accessible from real world
    Use libs and examples with caution

  • Users who have thanked flaMasta for this post:

    AndrewRyan (06-27-2011)

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by flaMasta View Post
    What I mean:
    On server you will have script 'image.php'
    in .htaccess you will have rewrite routine that will start 'image.php' script for each requested image
    that script will then read image originally requested by browser query
    it will rotate it and serve it as a image to the browser

    in that case user's browser will have no idea that any script have ran, thus it will be transparent to user
    browser will simply request image and get image

    I would write you example code that would do it, however I am at work now and today I am leaving for the weekend.. I ll try to explain in more detail or forward you to some resources if I find some time
    That would be greatly appreciated, though I'm already beginning to understand what you're suggesting. I can't test anything for a couple more days anyways.

  • #6
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    I did it at work, so here you go:

    file: ".htaccess"
    content:
    PHP Code:
    RewriteEngine on
    RewriteCond 
    %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
    RewriteRule (.*) ./image.php?img=$
    file: "image.php"
    content:
    PHP Code:
    <?php
    //make sure scripts is not accessed directly here !!
    $file "./" $_REQUEST['img'];
    //filter $file here !!
    //make sure it's not an attack
    if (file_exists($file)) {
        
    header('Content-type: image/png');
        
    header('Content-Length: ' filesize($file));
        
    ob_clean();
        
    flush();
        @
    readfile($file);
    } else {
        
    header("HTTP/1.0 404 Not Found");
        echo 
    "404 - file not found";
    }
    exit;
    ?>
    I think it's self explanatory, isn't it ?

    use .htaccess to route all image requests to image.php script
    in that script you have to filter request parameter, if no parameter then display 404
    if no such a file return 404

    if there is such file, send headers, send file

    you have to take care of image extensions as well, depending on extension you have to change header
    current .htaccess as you can see routes jpg, gif, png
    you can extend it of course
    Examples are not to be copied to servers which are accessible from real world
    Use libs and examples with caution

  • #7
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    one more thing, taken from stackoverflow, no point and no time to re write it, so here you go, take a look at headers you should send.
    Last-Modified
    The last modified date for the requested object, in RFC 2822 format
    header(Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT');
    See the function filemtime
    You can exit the script after sending a 304 if the file modified time is the same.
    status code
    header("HTTP/1.1 304 Not Modified");
    you can exit now and not send the image one more time

    For last modified time, look for this in $_SERVER

    If-Modified-Since
    Allows a 304 Not Modified to be returned if content is unchanged
    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    Is in $_SERVER with the key http_if_modified_since
    Examples are not to be copied to servers which are accessible from real world
    Use libs and examples with caution


  •  

    Posting Permissions

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