Do I need to store image paths in a database?

Firstly, I noticed that there are many questions in this that are marked as duplicates.

In the end, I came to this one .

And the accepted answer to this question, although it partially solves my question, does not answer all this.

My question is: user uploads image. I save the path in the database and the image file in the file system. However, I am making 3 copies of this image (large, medium and small). So, I have 4 images - original, large, medium, small.

Should I store all 4 paths in the database, for example

ID  |      original      |    large        |    medium       |    small       |
----+--------------------+-----------------+-----------------+----------------+
 1  |  /path/to/original | /path/to/large/ | /path/to/medium | /path/to/small |

or just keep the original path and give another 3 naming convention, for example car.jpg, car.jpg, large-car.jpg, medium-car.jpg, small-car.jpg:?

I feel that this method will be less difficult in the database, and that if I later want to add another size (i.e. extra small), I will not need to modify the database.

+3
source share
6 answers

If all the images in a given row live in the same place, I would say that their base path should be its own column (and not by retrieving the base path from the original full image path).

If all the images in the database are in one place, do not store the base path in this table; have it in code or in the global configuration table.

+7
source

It looks like you are trying to abuse the database. How about this method.

ImageID  | UserID  | name.. 
---------+---------+-----
1        | 495     | car454.jpg
2        | 495     | house.jpg
3        | 44      | kittysmall.jpg

And save all the images in one place.

IMAGES_PATH = "///"

imageID (Auto Increment), 5.ori.jpg 5.large.jpg ..

, , , .

+2

, , - , memcache, db i/o

+1

, , ( , ), . , - , .

+1

, , , .

- , . , ( ).

,

//////

//////

//////

//////

. .

PATH_ORIGINAL =/home/user/site/images/original/

PATH_SMALL =/home/user/site/images/small/

PATH_MEDIUM =/home/user/site/images/medium/

PATH_LARGE =/home/user/site/images/large/

-

smallcar = PATH_TO_SMALL. car.jpg;

, .

, , , , , .

+1

.. (:../generated/), , , - "car-large.jpg" ..

0

Source: https://habr.com/ru/post/1712403/


All Articles