I would like to have code for an image that counts the number of times the image is viewed, regardless of what site the image is found on. I want to use the img src tag, and have src point to a php page that counts that view and then returns the image to be viewed. I was thinking something like this:
<img src="www.mywebsiteurl.com/something.php" />
How would I go about writing "something.php" so that it returns the proper image file? I know how to record the page view, but if I must do something different in this case, please tell me.
Your script needs to:
header('Content-Type: image/jpeg');
)readfile( $path_to_image )
);For examples, please see the readfile documentation and Output an Image in PHP.
Yes you're on a good start. I'd do something like this
In image_counter.php, take
$src = $_GET['src']
for image source.Then +1 the hit for that image in the database.
Then do
This is the most barebones version you can have:
You can call an image using a URL similar to:
www.mywebsiteurl.com/image_renderer.php?url=url_to_image
.image_rendered.php:
Hope this will point you into the right direction.
Something like this should work:
Note: this would make something like:
show the correct image after incrementing the views.
You could always add some .htaccess to make all calls to /images/anything go through your image file. It'd mean you could keep the URL looking as though it's getting an actual image file (e.g. /images/logo.jpg would be re-written to actually go through getimage.php?img=logo.jpg)
Loading images / files from php is slow than normal loading files.
Main cons:
If you use database and want to increment column after image loads than a person can load your image again and again. So you need to use conditions on every query.
Alternative Solution
Enter this code in footer of page on which you want to show images
A ajax call