Skip to main content

Posts

Showing posts from May, 2017

Clearing image cache when updated with same name

When we update an image but the name remains the same, the browser keeps on showing the old image. This is very confusing for the admin and annoying too. If we use a decaching code, we end up reloading the image from the server on each visit and thus a lot of bandwidth is wasted. The best solution to this is to use a URL that changes when a image is updated. The best approach to achieve this is appending a dummy part to the image src that changes when the image is updated, and what could be better than the file timestamp !! So instead of <img src="image.jpg" width="200"> use <?php echo '<img src="image.jpg?m=' . filemtime ( 'image.jpg' ) . '" width="200">' ; ?>

Handling MISSING Image in HTML

It often happens that we allow users to upload a profile pic. But all the users do not upload a pic. Thus either we have to copy the same template image for all users or we get a broken image in the page. To fix this, use onerror code. Approach 1 - Hide the image if no image is available. onerror = " this . style . display = 'none' " Approach 2 - Use a template / filler image. onerror = " this . src = 'fallback-img.jpg' "