1

Users can upload an image for their avatar, or specify a URL from another website.

This is stored in a 'user.image' column in a database.

User uploads image - A randomly generated 10 letter filename with the original extension is saved locally, and the filename is saved to the database (not the entire path)

User specifies URL - The entire URL is saved to the database

However, when going to display the image, how am I to know whether it was an uploaded image (e.g. filename only in column), or if the user specified a full URL?

Possible thoughts are:

  • Checking to see if 'user.image' begins with the string 'http://' - but could be expensive performance wise under high traffic
    • Physically downloading the specified URL at the time of saving, and saving that image to the local server, and then updating the database with only the filename (not the path, since its local now)

Or is there a better way?

Greg Boyle
  • 13
  • 2
  • 1
    If you are concerned about performance when fetching the avatar, then download it once and save it in a file or as an object in the database. – Gordon Linoff Jan 03 '16 at 17:27

1 Answers1

-1

As already suggested, you can store your images in database or in the file system. Check this question and answer to decide which is better for your case.

Doing this also allows you to store image at a maximum resolution (auto resize them before storing), perform some optimizations to obtain smaller size etc.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164