To achieve something like this, your script will need to :
- send the right headers, which depend on the type of the image : image/gif, image/png, image/jpeg, ...
- send the data of the image
- making sure nothing else is sent (no white space, no nothing)
This is done with the header
function, with some code like this :
header("Content-type: image/gif");
Or
header("Content-type: image/jpeg");
or whatever, depending on the type of the image.
To send the data of the image, you can use the readfile
function :
Reads a file and writes it to the
output buffer.
This way, in one function, you both read the file, and output its content.
As a sidenote :
- you must put some security in place, to ensure users can't request anything they want via your script : you must make sure it only serves images, from the directory you expect ; nothing like
serveImage.php?file=/etc/passwd
should be OK, for instance.
- If you're just willing to get the number of times a file was loaded each day, parsing Apache's log file might be a good idea (via a batch run by cron each day at 00:05, that parses the log of the day before, for instance) ; you won't have real-time statistics, but it will require less resources on your server (no PHP to serve static files)