PHP: Display image on the fly everyday

software development


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 = "$date.jpg"; imagejpeg ($canvasImage); imagedestroy ($canvasImage); ?>

but its not working.
can someone pls help me


one thing you could try is change these lines:

$canvasImage = “$date.jpg”;
imagejpeg ($canvasImage);

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

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


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:


$date = date(“d”);
$canvasImage = “$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.


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

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
instead of

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


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
  2. The rewrite rule matches and calls for the script at /image/mycode.php
  3. That script returns a redirect to
  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


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


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.


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.


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


Stuff like that.

Visit me, please…