Y
Y
yury_borisov2016-08-05 16:50:28
Software design
yury_borisov, 2016-08-05 16:50:28

What structure do you use to store images?

I want to make a storage for images, but I can’t choose the architecture :) I have little experience, I just came up with this, namely by date for example /p/2016/12/03/23/45/12/name.jpg
but I’m going to store not only the original, and another 200, 400 px width,
how best to do all this? I use nginx to return, an example url ( http://img.project.loc/p/2016/12/03/23/45/12/name.jpg) is obtained for the same image, but only width 200 will be such a url http://img.project.loc/p/2016/12/03/23/45/12/name_2...
Maybe something is wrong, tell me :)
And how is it arranged for you?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
T
trevoga_su, 2016-08-05
@trevoga_su

but I'm going to store not only the original, but also 200, 400 px width
cloudinary.com
little experience, just came up with this, namely by date for example /p/2016/12/03/23/45/12/name.jpg
no need to reinvent the
md5 wheel from the file name + some salt,
we get something like 2cb126f9bd4e36cc8818183c785879d7.jpeg,
then we write a script that creates the required number of levels:
/i/150x100/2/c/b/2cb126f9bd4e36cc8818183c785879d7.jpeg
Here is my stray to generate
$directory_generator = new Krugozor_Utility_Upload_DirectoryGenerator(/* имя загруженного файла */);

// создали директорию в THUMBNAIL_150x100...
$directory = $directory_generator->create(DOCUMENTROOT_PATH . Krugozor_Registry::getInstance()->UPLOAD['THUMBNAIL_150x100']);

// отресайзили...
$creator = Krugozor_Thumbnail_Factory::create(
  DOCUMENTROOT_PATH . Krugozor_Registry::getInstance()->UPLOAD['THUMBNAIL_ORIGINAL'] . $upload->getFileNameWithExtension(), 
  $directory . $upload->getFileNameWithoutExtension()
);
$creator->setResizedWidth(150);
$creator->setResizedHeight(100);
$creator->resizeFixed();

// http-путь до картинки
$this->getView()->path_to_image = $directory_generator->getHttpPath() . $creator->getFileNameWithExt();

S
Stanislav Pochepko, 2016-08-05
@DJZT

/images/users/avatars/lksdhg324nk54h.jpg
/images/users/avatars/lksdhg324nk54h-200x200.jpg
/images/users/avatars/lksdhg324nk54h-400x400.jpg

/images/posts/kjnh345jkb43jb.jpg
/images/posts/kjnh345jkb43jb-200x200.jpg
/images/posts/kjnh345jkb43jb-400x400.jpg

If you want to have more normal names, then create a table in the database where you will store the original name. Any descriptions and additional info and refer to the file in the file system. But not to store the whole path, but either from the root images/ .... or images/posts/ .....
But in the first case, you will need to store the type of the object to which the file belongs in the database in order to substitute users or posts, etc.
I also made a structure when only one picture belongs to one object.
/images/users/user_id.jpg
and so on
Then you don't need a database and you always know what image to look for. With dimensions, add the folder
/images/users/300/user_id.jpg
/images/users/600/user_id.jpg to the path in the same way

D
Dmitry Belyaev, 2016-08-05
@bingo347

I generate uuid - for example 39c807bc-5b1f-11e6-95a6-a3eadeba2e44
On the disk it will be like this:
images/39c807bc/5b1f/11e6/95a6 /
a3eadeba2e44.jpg
5b1f/11e6/95a6/a3eadeba2e44-preview.jpg
as well as writing to the database with the same id

D
Dimonchik, 2016-08-05
@dimonchik2013

here,
as they have already written more than once - the originals are in md5, the nesting of directories - two
in total gives 65536 directories with 65536 max files in each
, enough for a career

D
dev400, 2016-08-05
@dev400

you can make a unique structure and a unique name from a hash from a file

M
Maxim Timofeev, 2016-08-05
@webinar

/ images / category name(goods, redactor, user, etc.) / year / photo size('400x', '200x200', 'x1000', etc.) /hash of product id, etc. .jpg
You can select all photos of a certain size without going to
the
database
.

X
xmoonlight, 2016-08-05
@xmoonlight

General form:
A hash can be formed based on a timestamp, the storage path to the file, and a "salt".

S
Sunsh1ne, 2016-08-05
@Sunsh1ne

I've only recently been interested in this issue.
Let me clarify, for starters, that I use a ready-made solution for working with images, and I advise you)
I did this:
1. It is necessary to determine how many images there will be in the future of the project.
If there are 100 and after 5 years of the existence of the project, then there is no need to invent anything complicated.
If there are more, then you will need to scatter them into folders.
Roughly for myself, I figured that about 20 photos would be stored in my folder.
And in the project, I set the bar for myself at about 9000 images + their copies 300x200. and here are the calculations.
9000*2/20 = 900 folders.
In fact, 20 photos is not even enough ... But I chose this figure for personal reasons.
Generation code like this:

$rand_int = rand(1,5);
$rand_md5 = md5(str_random(rand(6,15)));
$dir = '/images/uploads/minerals/' . $rand_int . '/' . $rand_md5[0] . '/' . $rand_md5[1] . '/';

If you count the potential folders of the last level, you get 1280. Like)) I need so much just with a margin.
How it happened:
It is clear from the code that after the minerals folder there will be a folder with a name in the range from 1 to 5.
Then there will be a folder with a name in the range of md5 hash characters, that is: 0123456789abcdef.
If I'm not mistaken!!..
and the name of the last folder is randomly in the same character span.
Total 5*16*16 = 1280. Potential, mind you, folders.
All paths are stored in the database. Both original and preview images.
You can play with the number of nested folders, with a dictionary for generation, not limited to md5 characters, you can give folder names from 2 characters or more. It all depends on your needs, I just showed the way.
If the folder name contains 2 characters or more, then it is considered differently.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question