0

I've setup Amazon S3 to host my user avatars, but the problem I'm having is how to display the default avatar if a user hasn't uploaded a photo. Is there a way to return an image on 404?

I want to minimize overhead and have it so a user image has a url like this:

http://example.s3.amazonaws.com/avatar/52752545b960b3181226a2f346e2f466714_64.jpg

Where the hash before '_64' is the userid hashed with a salt, and '64' being the dimension. This way I don't have to check on my server whether the user has uploaded an photo or not on every avatar display.

Thanks in advance

Mista Sage
  • 63
  • 1
  • 6

2 Answers2

4

Here is a real-world example that I use. The source image lives on s3, pulled from a CNAME.

So example:

<img class="image" src="http://cache03.zoomphoto.ca/i/15665/thumbs/t_15665-101-    15032855.jpg" hspace="1" vspace="1" alt=""     onerror="this.src='http://static.zoomphoto.ca/images/processing_thumb.jpg';">

So you show the image, if nothing is available (onerror), then show this.

2

No but if the paths to the avatars are saved in a database, you could an "if" statement to choose whether to display the image from Amazon S3 or use a default avatar.

Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • damn. any suggestions on relieving the overhead of checking for filename on every avatar display? – Mista Sage Jun 14 '11 at 03:03
  • 1
    I'm assuming that you're loading the data from a database so there wouldn't be any additional overhead really. All you have to do is a `if ( ! empty($avatar) ) echo ''; else echo '';`. The overhead of an "if" statement is negligible. – Francois Deschenes Jun 14 '11 at 03:07
  • no i'm not checking the db. thus i'm only using the hashed userid so the url is constant and predictable for every user even without accessing db. – Mista Sage Jun 14 '11 at 03:20
  • @Mista - You should see if [this solutions](http://stackoverflow.com/questions/92720/jquery-javascript-to-replace-broken-images) will work for you. It's a JavaScript solution to broken images. – Francois Deschenes Jun 14 '11 at 03:33