You need to be viewing the image on a PHP page, or link to a PHP script that outputs the image just like a static image. Then you can incerement your hit counter in that script.
If you are using a database you can do something like this to increment the hit count
PHP Code:
// once connected to the DB, obviously
// $_GET['id'] holds the image ID in the database
$count = @mysql_result(@mysql_query(
'select distinct `hit_count` from `images` where '.
'`image_id` = '.(int)$_GET['id']
), 0);
if (!mysql_errno()) {
// the image exists, update the hit count:
mysql_query(
'update `images` set `hit_count` = '.$count++.
' where `image_id` = '.(int)$_GET['id']
);
}
else {
// throw an error, or whatever
}
Note that the @ symbol prepended to a statement suppresses error messages for that statement. It's useful if you have an alternate method of checking for errors, such as mysql_errno() in this case (to handle invalid image IDs), but you should use proper error handling methods otherwise.