SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: User Avatars

  1. #1
    SitePoint Enthusiast BMorganVA's Avatar
    Join Date
    Mar 2010
    0 Post(s)
    0 Thread(s)

    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!

  2. #2
    PHP Guru's Avatar
    Join Date
    Jan 2009
    0 Post(s)
    0 Thread(s)
    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.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!


Posting Permissions

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