SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Image Expiration Issue

    I wasn't sure what section to post this in since it spans both PHP and HTML, but here goes.

    I've built a small gallery for my SNS website and I'm trying to figure out how to manage thumbnails without having to fetch something from the database everytime a thumbnail is displayed.

    Once the user has uploaded the image, they can set it as their main profile image. Once its been set, a small thumbnail mini version of the image is created.

    <img src="http://website/storage/thumbs/userID.jpg" />

    This works, but lets say that the same user decides to change their profile image to something new that they've uploaded; The database gets notified and then the profile picture is displayed and the thumbnail is updated.

    But wait!

    <img src="http://website/storage/thumbs/userID.jpg" />

    The thumbnail source is still the same, and whenever the thumbnail is displayed then the old thumbnail is displayed instead (unless the browser cache has expired or the user hits refresh).

    Currently, I apply a querystring timestamp value to the end of the img URI:

    <img src="http://website/storage/thumbs/userID.jpg?1234567890" />

    This works, but that timestamp value is something that's stored in the database and therefore must be fetched everytime the users' image is displayed (whether it be directly in PHP or in an AJAX result).

    Is there any way around having to fetch the database timestamp for the thumbnail each time?
    I can't believe I ate the whole thing

  2. #2
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,782
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Since you are just trying to fetch the image and no personal data is involved you could use a cookie to hold the query string for the image and just use the cookies value. Each time the user wants a new image make that cookie expire and set a new cookie.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why store the hash in the db?
    Simply using one in the request defeat the cache management of the browser.

    You could have a random value generated on each page load, and use it on the request.
    like
    HTML Code:
    <?php
    hash=md5(uniqid(rand(),true));
    ?>
    <html>
    <body>
    <img src="/storage/thumbs/userID.jpg?hash=<?=$hash;?>" alt="user avatar"/>
    </body>
    </html>

  4. #4
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The cookie idea is good, but it is only relative to the user that has updated the image. If I was to use the cookie system for other users whom are viewing a bunch of thumbnails then there would be hundreds of cookies per browser (I'm not sure how much clientside data a browser is set to store for a website).

    The hash querystring solution also works, but it contradicts my original goal to having an updated thumbnail system. This is because if I do include the HASH querystring, then the browser would download the thumbnail EVERYTIME it renders it (I need it to be cached). This would be the same as if I removed all the expires information for all my thumbnail images.
    I can't believe I ate the whole thing

  5. #5
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,782
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matsko View Post
    The cookie idea is good, but it is only relative to the user that has updated the image. If I was to use the cookie system for other users whom are viewing a bunch of thumbnails then there would be hundreds of cookies per browser (I'm not sure how much clientside data a browser is set to store for a website).
    I believe the limit to each user is 20 cookies per site. The gurus here can correct me.

    Well no, if you are viewing thumbnails then it will be retrieving the value of the cookie, which you will be storing in a database, possibly. You would need to make sure that the value of the cookie is not already stored in the database then use that. I would actually think that using database values would be good. Just have a random generator of a number and use that as your hash value.

    Quote Originally Posted by matsko
    The hash querystring solution also works, but it contradicts my original goal to having an updated thumbnail system. This is because if I do include the HASH querystring, then the browser would download the thumbnail EVERYTIME it renders it (I need it to be cached). This would be the same as if I removed all the expires information for all my thumbnail images.

    Well let's combine it shall we?

    Is there anyway at all that the users OWN thumbnail will be saved onto their computer via cookie and you also store that value into a database which you will loop through each time the user wants to add a new thumbnail to see if that value exist already?
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the limit to each user is 20 cookies per site. The gurus here can correct me.
    exactly, at least in IE6.
    http://www.thismuchiknow.co.uk/?p=13
    I have found that the 20 cookies per domain limit is not just a minimum, but also the maximum, for IE6. If you set a 21st cookie for a given domain, then the 1st cookie is forgotten - so only the most recently-created 20 cookies are kept.

  7. #7
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,782
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Ah yes I was right . So let's wait for the OP's response.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •