PHP image question

Discussion in 'PHP' started by dalv, Jun 9, 2006.

  1. #1
    I have a script that prints a new image location everyday.

    http://www.my-site.com/images/141.jpg
    HTML:
    but I need to be able to call the actual image from that location. I am thinking of using a daily php script, like daily.php that will call the image location allow me to show the image like this:

    <img src="http://www.my-site.com/daily.php">
    HTML:
    How can I do that? Is there a script like that somewhere?
     
    dalv, Jun 9, 2006 IP
  2. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Off the top of my head there are three things you can do:

    1) don't use the php script as the image - use a cron job to rotate the image into a static name on a daily basis
    2) every place the image is called use a php call to generate the name - eg
    $img = get_image(); echo '<img src="'.$img.'">';

    while those are perhaps easier they don't really accomplish what you want

    3) use the php script as the img src as you describe. The php script then need to return the image rather than html. It could be as simple as reading a file and echo'ing it but you'll probably need to figure out how to get php to output the correct mime type to make everyting work.

    It's something I've done before but not in PHP and not recently. Sorry, I don't know any scripts that do that though there are tons of scripts that do that as a small part of what they do - like image galleries and such.
     
    jnestor, Jun 9, 2006 IP
  3. dalv

    dalv Well-Known Member

    Messages:
    130
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Thanks!

    Right now I am using this code (http://www.my-site.com/cgi-bin/image/image.pl is the script that rotates the files):

    <?PHP
    
    $filename = 'http://www.my-site.com/cgi-bin/image/image.pl';
    if (file_exists($filename))
    {
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header ("Content-type: image/jpg");
    readfile("$filename");
    } else {
    $filename = 'http://www.my-site.com/cgi-bin/image/image.pl';
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header ("Content-type: image/jpg");
    readfile("$filename");
    }
    
    ?>
    PHP:
    but it just prints the location of the image file:

    http://www.my-site.com/thumb.php?img=images/142.jpg&w=80&h=95

    Any ideas on how I can make it into a jpg file?
     
    dalv, Jun 10, 2006 IP
  4. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not sure what you're trying to accomplish with your if test because it does effectively nothing.

    The basic sequence of code:

    $filename = 'http://www.my-site.com/cgi-bin/image/image.pl';
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header ("Content-type: image/jpg");
    readfile("$filename");
    Code (markup):
    Is doing what you want. It's reading a file and outputting it. This will accomplish what I talked about in option 3.

    I think your problem comes from this part:
    $filename = 'http://www.my-site.com/cgi-bin/image/image.pl';

    I'm not sure what that perl CGI is outputting but this script as written will write the code of that script out where the image should be. If that code generates the image then you'd want to read the output of the script. If that code generates the name of the file then you'd want to take the output and read the file it points to.

    I suspect you're completely lost at this point since even I'm not sure exactly what all that means ... so lets take a step back.

    What are you trying to do?

    I'm assuming that's the cgi mentioned above. So this CGI gives you a file name? Does it create the image or just select a previously created image? Is there a reason why you want to use a perl script to generate a file and then output that file with PHP? That's certainly complicating things.

    So you want to insert the image into an html page somewhere? Is that a static html page? Something output by PHP? Perl?

    As for cron - I'm saying avoid all of the above. Put the image into a static location - daily.jpg - by writing a script to copy the actual image to that name (or better yet to make a link). All "cron" means is that you can set it up to automatically run the script.

    If your cgi outputs the file name you'd write a script something like:
    rm daily.jpg
    ln -s `perl image.pl` daily.jpg

    and then setup a cron job to run that script on a daily basis. And then your html page would just say: <img src="daily.jpg">
     
    jnestor, Jun 10, 2006 IP