PHP: Display image on the fly everyday

software development

#1

hi there i want to display an image(on the fly) everyday. i mean it will show different image base on the day.

i made a code like this

<?php header ("Content-type: image/jpeg"); $date = date("d"); $canvasImage = "http://mysite.com/image/$date.jpg"; imagejpeg ($canvasImage); imagedestroy ($canvasImage); ?>

but its not working.
can someone pls help me
thanks.


#2

one thing you could try is change these lines:

from:
$canvasImage = “http://mysite.com/image/$date.jpg”;
imagejpeg ($canvasImage);

to
$canvasImage = “image/$date.jpg”;
echo “<img src=”$canvasImage">";

it will then output the image as a standard html image tag


#3

I haven’t used this function personally, but the PHP documentation suggests that a URL is not a valid input to imagejpeg().

danhill has a good suggestion for how to go about what you’re doing if this script is meant to stand alone, but is probably not what you’re looking for if this script is meant to be invoked by another page’s . If you want your script to be invoked by another page’s tag, I would suggest this:

[code]<?php

$date = date(“d”);
$canvasImage = “http://mysite.com/image/$date.jpg”;
header(“HTTP/1.0 302 Found”);
header(“Location: $canvasImage”);

?>
[/code]What that will do is redirect the browser to ask the server for $canvasImage. The server will then give $canvasImage to the browser exactly as if the JPEG’s URL had been used directly in the first place.


#4

@danhill
thanks but i think alpicolas code is the one am looking for. thanks again for the input.

@alpicola
i tried it and it works. iam not sure if this is the best way to do it but nonetheless ill use it. i have some problem though i want to use the url
http://mysite.com/image/mycode.jpg
instead of
http://mysite.com/image/mycode.php

i tried to use the htaccess like this

RewriteEngine On
RewriteBase /

RewriteRule^(.+).jpg $1.php

but its not working.

oh one more thing, how can i get the computers day? bec if i use your code it gets the day of the server right?
thanks again


#5

My guess is that your RewriteRule is way too general, so rather than rewriting just that particular URL, you’re actually rewriting every .jpg you use anywhere on your site!

As a result, in your particular problem at hand, you’re rewriting and redirecting yourself in a circle, like so:

  1. You navigate to http://mysite.com/image/mycode.jpg
  2. The rewrite rule matches and calls for the script at /image/mycode.php
  3. That script returns a redirect to http://mysite.com/image/15.jpg
  4. The browser requests that URL
  5. The rewrite rule matches and calls for the file at /image/15.php
  6. Apache can’t find a file there and returns a 404 File Not Found message to the browser

To get around this, make your RewriteRule more specific: RewriteRule ^(.*mycode)\.jpg $1.php Now, it’ll only try to rewrite URLs with mycode.jpg, so 5 and 6 above will never happen, and you should get the result you’re after.

As to the client date issue, I don’t know of a good solution for that. Maybe you could use some JavaScript to modify the URL to look like http://mysite.com/images/mycode.jpg?client_date=whatever


#6

thanks for the reply but i still get the 404 error.


#7

Hmm. The only other thing I can think of is that maybe your .htaccess file is in the wrong place. If your .htaccess is anywhere other than the root directory for your domain, then it probably won’t work. Try keeping the changes above, but also remove the RewriteBase and see if that helps.


#8

its working now, i just deleted the RewriteBase.
what if i want to make 3 or more image withe the same function.

RewriteRule ^(.*mycode).jpg $1.php

do i need to use 3 RewriteRule for every file?
thanks again.


#9

Not if you use the same naming convention (e.g. Name all the images “{something}mycode.jpg”)

1mycode.jpg
2mycode.jpg
Wednesday.mycode.jpg

Stuff like that.

Visit me, please…
www.theraven7.com