User Avatars

Could someone give me the basics of how to have user avatars. I don’t want the code, just a general idea. If you know a website that explains this well, that would be good too.

Here is what I have so far. User uploads image. Path gets stored in a database*. Make it a thumbnail. Output image (last step is easy).

*What would I store in the database to access the picture. (path?)

Thanks for all the help!

  1. Copy uploaded file to temp location. You can randomly create the path to temp location at this point. This will be a path to original upload

  2. use image library like GD to resize the image if the dimensions of uploaded image are larger than you want an avatar to be. You need to scale width, height proportionally. Most social sites use square avatars, which is a good idea. To make a square avatar you will have to cut-off part of the uploaded image, there is no way around it. This can easily be done with GD.

  3. Now you need to decide where to store the avatar - in file system or in database. Since the final avatar size is fairly small, it’s ok to store the image in the database, but I personally prefer storing in on file system. If you want to store it on file system, then you need to create a path to it. A path could be based on username or on userid. Then in the database you can store only the file extension. For example only ‘jpg’
    To find the avatar you can generate a path like /avatars/username.jpg

If you have thousands of users, then storing all avatars in one directory is not a good idea, in which case you need some way to distribute the files among different directories based on user ID. This is up to you to come up with such algorithm. I use the HEX value of userID and them break down the hex path into 2-char directories.

For example userID 435345 would be 6A491, thus the path to avatar would be /6A/49/1.jpg
This way no directory will have over 255 files in it.