Jump to content

Call a script when directly accessing an image in the browser


Recommended Posts

I'm working with a lightbox plugin to show images when thumbnails are clicked on. The plugin works by wrapping the thumbnail in an anchor tag that has the href as the direct path to the full-size image. This acts as a non-js fallback, so that if JS isn't present for whatever reason, the user still sees the image. When JS is enabled, the image is shown in the lighbox, resized to fit in the lightbox, which is a variable size depending on the browser.

 

I need to keep a record of the number of times that the image has been clicked on, as thumbnails will be ordered by the number of views. The problem of course is that I'm directly linking to the image, so I can't put a script on this page.

 

I've tried two things instead, neither of which worked:

 

1) Instead setting the target as the path to the image, I set it to a PHP script, then outputted the image in an <img/> tag. The problem was that the image is then not resized according to the size of the lighbox. So this is no good.

2) I set the target of the lightbox as a PHP script which recorded the viewing of the image, and redirected to the image page. This didn't work either, as the lightbox showed the data for the image (text) instead of the image itself.

 

So, I've hit a bit of a barrier. I'm wondering if I can set something up in my PHP settings that will automatically call a script when hitting images, or maybe something in an .htaccess file that will do the same, but I'm not aware of if this can be done, nor how to do it, and I haven't found anything yet with my googling. Does anyone have any thoughts?

 

Thanks.

Edited by haku

I guess another way to explain it is that I'd like to call a PHP script in the background when files of a certain type are accessed. I want this to happen automatically through some configuration of PHP or .htaccess (or some other server config).

I got it! I didn't figure out how to call a script in the background, but what I did was create a callback PHP script, which instead of returning the image in an image tag, serves the image directly to the browse using a force download script, setting the relevant headers etc. Now my PHP script behaves the same as when directly accessing an image, and I am able to mark the image as having been viewed before serving the image to the browser.

You can route requests for image files to a php script, do whatever you want, then just output the image from php by setting the relevant content type.

 

Htaccess can be used to route requests for the image. This solution only works if you're hosting the images, however.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.