开发者

The best way to store and serve up user profile images

开发者 https://www.devze.com 2023-04-12 13:30 出处:网络
My website has public-facing profiles. Users may have 145x145 profile images that are displayed to people. At the moment, I store profile images under a directory on my server: /images/users/ I then u

My website has public-facing profiles. Users may have 145x145 profile images that are displayed to people. At the moment, I store profile images under a directory on my server: /images/users/ I then use a .ashx file to serve the right image up for each page:

<!-- from the html -->
<img id="imgLogo" runat="server" alt="Company Logo" src="" />

// from the page code behind Page_Load event
imgLogo.Src = "userImage.ashx?id=" + UserId;

// from the .ashx file
String imagePath = context.Server.MapPath("~/images/users/")+context.Request.QueryString["id"]+".jpg"; 

This code was in place when I joined the project (the guy who wrote it has since left.) It seems to work nicely enough, but I'm new to web development, and unfamiliar with other ways of implementing systems like this.

I now need to add a d开发者_JS百科ifferent kind of image to the site - something like a tall banner ad for a user's profile. As such, these images will be much larger than the 145x145 profile avatars.

How should I implement these for my site? Should I use a similar system to the one currently in place, with images stored in /images/users/largeImage and the src being set to a largeUserImage.ashx file? Or should I store the images in my sql database?

It's likely that I'll want to add more user-defined images in the future - maybe stuff like user photo collections and similar. Is there anything I should consider to make a system that is easily scaleable?


As 3rd party framework like Gravatar doesn't work for your case, you have to do it on your own.

Don't save photos in SQL database, save its paths (image paths of different size) instead. In your userImage.ashx, you can pass 1 more parameter to tell the script which size of image should be fetched, such as:

imgLogo.Src = "userImage.ashx?id=" + UserId + "&size=" + ImageSize;

where ImageSize is your defined variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消