0

I have a script that takes an image and generates a resized version.

i.e. requesting /images/50x50-logo.png will call image-conroller.php which will then return a 50x50 version of logo.png

The image is returned using;

header('Content-Type: '.mime_content_type($sOutputPath));
readfile($sOutputPath);

As far as I'm aware, using this method to return the image, there is no safe way to check if the user already has a cached a version of this image. So how am I supposed to know if to send a 304 or serve the image?

Would someone please mind explaing if it's possible to achieve this. Or provide some direction to a soloution/workaround?

N.B. I can't think of a suitable title, any suggestions/edits would be apprecaited.

atoms
  • 2,993
  • 2
  • 22
  • 43
  • 1
    Possible duplicate of [How "304 Not Modified" works?](http://stackoverflow.com/questions/20978189/how-304-not-modified-works) – Matt S Jun 01 '16 at 13:19
  • I dont think this would solve my issue. You would still need to send the image with etag/lastModified, if your still sending the headers and the image, its not a 304 but a 200? – atoms Jun 01 '16 at 13:22
  • 1
    If you don't get an etag or last modified in the request, or you don't recognize the ones you get, then send a 200 with headers and image. If you get a valid etag or last modified, send back just the headers with 304. Another option is to set up a cache server in front of your PHP app and let it figure it out. – Matt S Jun 01 '16 at 13:25
  • great thank you, will give it a go now – atoms Jun 01 '16 at 13:26
  • @MattS got the Etag working. Made an md5 of the file and used that as the Etag. Am concerned about the performance of hashing the image each time. Using IIS so checking last modified timings etc will be a pain. – atoms Jun 02 '16 at 11:51

0 Answers0