Kirk's avatar
Level 1

Proper way to store images

Hi,

I've been brainstorming for a while now about the proper way to store images uploaded by users. Optimisation is a big issue here and I'm looking forward to find a pattern to store images so that they would be accessible easily in the future.

First of all any image would be processed to create several thumbnails respecting different sizes, like 100x100 200x200 etc.. so any image is in fact 4 to 5 images named respecting the same convention {image-name}_{size}.jpg Now for the most important part, how to store it, and by "how" I mean what directory architecture ? what database setup ?

Is it proper to create a directory for each user, like (...)/images/{user_id}/{image_name}_{size}.jpg or having multiple directories depending on users id is too much trouble ?

Is it better to create a directory for every category of images like profile_pics/ , banner and store any image referring to that category in there ?

I assume than storing every images in the same directory would turn rapidly into hell, so have any of you any thought of what would be the best directory architecture ?

0 likes
5 replies
katifrantz's avatar

I think you need this package : http://image.intervention.io/ . secondly , It's good practice to save files to the storage folder of your project, and then simply save the file name to your database . See Jeffrey upload files easily here : https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/12

If you have to publicly access these images , you should probably read the docs. https://laravel.com/docs/5.3/filesystem#file-uploads

1 like
Kirk's avatar
Level 1

I'm already using Intervention Image to process my images, I was just wondering how to properly store them on my server.

primordial's avatar

@alivemedia Whatever the file system I always store the images in a folder structure of "ModelName/PrimaryKey/..." for ease of retrieval. Create your size specific folders within.

I would never consider storing them in the DB.

katifrantz's avatar
Level 6

Okay I understand . I think It's just developer preference . I would probably have a users directory , then in that directory i would have the following directories : 100,200,300,400,banner,original_image , then have a field in the database containing a simple image name : for example user_id.jpg . Therefore for each user, I will save a 100x100 px image into the 100 folder , 200x200 into the 200 folder, and so on . Then when accessing it , depending on where i need s particular image , i can just reference :

<img src="storage/200/{ $user->id }.jpg">
// and for another example ,
<img src="storage/banner/{ $user->id }.jpg">
1 like

Please or to participate in this conversation.