是否需要在数据库中存储图像路径?

时间:2021-12-29 16:58:22

First, I noticed there are many questions regarding this, lots marked as duplicate.

首先,我注意到有很多关于此的问题,批次标记为重复。

I eventually came to this one.

我终于来到了这一个。

And the accepted answer for that question although partially solves my question, it doesn't answer all of it.

对于这个问题的公认答案虽然部分地解决了我的问题,但它并没有回答所有问题。

My question is, a user uploads an image. I store the path in the database and the image file in the file system. However, I make 3 copies of that image (large-, medium- and small-sized). So all in all I have 4 images - original, large, medium, small.

我的问题是,用户上传图片。我将路径存储在数据库中,将映像文件存储在文件系统中。但是,我制作了3张图片(大,中,小)。总而言之,我有4张图片 - 原创,大,中,小。

Should I store all 4 paths in the database, like so

我应该在数据库中存储所有4个路径,就像这样

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

or just store the original's path, and give the other 3 a naming convention, like so: car.jpg, car.jpg, large-car.jpg, medium-car.jpg, small-car.jpg?

或者只是存储原始路径,并给其他3个命名约定,如:car.jpg,car.jpg,large-car.jpg,medium-car.jpg,small-car.jpg?

I feel this way would be less heavy on the database, and that if later, I wanted to add another size (ie. extra small) I wouldn't have to moditfy the database.

我觉得这种方式对数据库来说不那么沉重,如果以后我想添加另一个大小(即超小)我不需要修改数据库。

6 个解决方案

#1


If all the images in a given row live in the same place, I'd say their base path should be its own column (rather than re-deriving the base path from the original image's full path all the time).

如果给定行中的所有图像都存在于同一位置,我会说它们的基本路径应该是它自己的列(而不是始终从原始图像的完整路径重新导出基本路径)。

If all the images in the database live in the same place, don't store the base path in this table at all; have it in code, or in a global configuration table.

如果数据库中的所有图像都位于同一位置,则根本不要将基本路径存储在该表中;将它放在代码中或全局配置表中。

#2


Seems like you are trying to overuse the Database. How about this method instead.

好像你正试图过度使用数据库。相反,这个方法怎么样。

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

And Store all the images in one place.

并将所有图像存储在一个地方。

IMAGES_PATH = "/path/to/images"

IMAGES_PATH =“/ path / to / images”

And name the images by the imageID (Auto Increment), so for the 5th image, it would be 5.ori.jpg or 5.large.jpg etc

并通过imageID(自动增量)命名图像,因此对于第5张图像,它将是5.ori.jpg或5.large.jpg等

This way you can easily see who owns what image, and also the user can upload different images with the same filename and not have to worry about that.

通过这种方式,您可以轻松查看谁拥有了哪些图像,并且用户也可以使用相同的文件名上传不同的图像,而不必担心。

#3


For sure have a solid naming convention for the various sizes of the original image, this will help you with generating known cache keys so you can store the images in some cache, like memcache, this relieves the load on the db and server's disk i/o

对于原始图像的各种大小,确实有一个可靠的命名约定,这将帮助您生成已知的缓存键,以便您可以将图像存储在某些缓存中,如memcache,这可以减轻数据库和服务器磁盘上的负载i / Ø

#4


To generalise, I'd say if you can recreate the information (because the base is always the same, followed by the users name), don't store it in the database. If you later want to change the directory where you store images for whatever reason, you'd be in trouble.

为了概括,我想说如果你可以重新创建信息(因为基数总是相同的,后跟用户名),不要将它存储在数据库中。如果您以后想要更改存储图像的目录,无论出于何种原因,您都会遇到麻烦。

#5


If the specific paths are consistent except for the file names why not use constants for the paths and then just store the different sized images in the appropriate directories and reference just the file names in the database.

如果特定路径是一致的,除了文件名之外,为什么不使用路径的常量,然后只将不同大小的图像存储在适当的目录中,并仅引用数据库中的文件名。

The main principle here is avoiding duplicate information, in the database and in your code. For the database you achieve higher normal form, and for the code you achieve DRY (Don't Repeat Yourself).

这里的主要原则是避免在数据库和代码中重复信息。对于数据库,您可以获得更高的正常形式,并且对于您实现DRY的代码(不要重复自己)。

Assume you structure is something like

假设你的结构是这样的

/home/user/site/images/original/

/home/user/site/images/small/

/home/user/site/images/medium/

/home/user/site/images/large/

you could use constants for that info. e.g.

你可以使用常量来获取该信息。例如

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

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

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

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

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

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

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

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

Then in your code you could do something like

然后在你的代码中你可以做类似的事情

smallcar = PATH_TO_SMALL . car.jpg;

smallcar = PATH_TO_SMALL。 car.jpg;

Or just insert the appropriate constant variable inside whatever query output you have for loading the images.

或者只是在您加载图像的任何查询输出中插入适当的常量变量。

The added benefit is that you have one place to change paths if you need to tweak directory structures or move code between servers, rather than update a whole slough of database records, which might be more problematic and error prone.

如果您需要调整目录结构或在服务器之间移动代码,而不是更新整个数据库记录,这可能会更有问题且容易出错,您可以在一个地方更改路径。

#6


As a minor tweak, I'd be tempted to place the generated thumbnails, etc. in a different path (e.g.: ../generated/) to ensure that you don't overwrite the source image if someone uploads a file called 'car-large.jpg', etc.

作为一个小调整,我很想将生成的缩略图等放在不同的路径中(例如:../ generated /),以确保如果有人上传了一个名为'car的文件,则不会覆盖源图像-large.jpg'等

#1


If all the images in a given row live in the same place, I'd say their base path should be its own column (rather than re-deriving the base path from the original image's full path all the time).

如果给定行中的所有图像都存在于同一位置,我会说它们的基本路径应该是它自己的列(而不是始终从原始图像的完整路径重新导出基本路径)。

If all the images in the database live in the same place, don't store the base path in this table at all; have it in code, or in a global configuration table.

如果数据库中的所有图像都位于同一位置,则根本不要将基本路径存储在该表中;将它放在代码中或全局配置表中。

#2


Seems like you are trying to overuse the Database. How about this method instead.

好像你正试图过度使用数据库。相反,这个方法怎么样。

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

And Store all the images in one place.

并将所有图像存储在一个地方。

IMAGES_PATH = "/path/to/images"

IMAGES_PATH =“/ path / to / images”

And name the images by the imageID (Auto Increment), so for the 5th image, it would be 5.ori.jpg or 5.large.jpg etc

并通过imageID(自动增量)命名图像,因此对于第5张图像,它将是5.ori.jpg或5.large.jpg等

This way you can easily see who owns what image, and also the user can upload different images with the same filename and not have to worry about that.

通过这种方式,您可以轻松查看谁拥有了哪些图像,并且用户也可以使用相同的文件名上传不同的图像,而不必担心。

#3


For sure have a solid naming convention for the various sizes of the original image, this will help you with generating known cache keys so you can store the images in some cache, like memcache, this relieves the load on the db and server's disk i/o

对于原始图像的各种大小,确实有一个可靠的命名约定,这将帮助您生成已知的缓存键,以便您可以将图像存储在某些缓存中,如memcache,这可以减轻数据库和服务器磁盘上的负载i / Ø

#4


To generalise, I'd say if you can recreate the information (because the base is always the same, followed by the users name), don't store it in the database. If you later want to change the directory where you store images for whatever reason, you'd be in trouble.

为了概括,我想说如果你可以重新创建信息(因为基数总是相同的,后跟用户名),不要将它存储在数据库中。如果您以后想要更改存储图像的目录,无论出于何种原因,您都会遇到麻烦。

#5


If the specific paths are consistent except for the file names why not use constants for the paths and then just store the different sized images in the appropriate directories and reference just the file names in the database.

如果特定路径是一致的,除了文件名之外,为什么不使用路径的常量,然后只将不同大小的图像存储在适当的目录中,并仅引用数据库中的文件名。

The main principle here is avoiding duplicate information, in the database and in your code. For the database you achieve higher normal form, and for the code you achieve DRY (Don't Repeat Yourself).

这里的主要原则是避免在数据库和代码中重复信息。对于数据库,您可以获得更高的正常形式,并且对于您实现DRY的代码(不要重复自己)。

Assume you structure is something like

假设你的结构是这样的

/home/user/site/images/original/

/home/user/site/images/small/

/home/user/site/images/medium/

/home/user/site/images/large/

you could use constants for that info. e.g.

你可以使用常量来获取该信息。例如

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

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

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

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

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

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

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

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

Then in your code you could do something like

然后在你的代码中你可以做类似的事情

smallcar = PATH_TO_SMALL . car.jpg;

smallcar = PATH_TO_SMALL。 car.jpg;

Or just insert the appropriate constant variable inside whatever query output you have for loading the images.

或者只是在您加载图像的任何查询输出中插入适当的常量变量。

The added benefit is that you have one place to change paths if you need to tweak directory structures or move code between servers, rather than update a whole slough of database records, which might be more problematic and error prone.

如果您需要调整目录结构或在服务器之间移动代码,而不是更新整个数据库记录,这可能会更有问题且容易出错,您可以在一个地方更改路径。

#6


As a minor tweak, I'd be tempted to place the generated thumbnails, etc. in a different path (e.g.: ../generated/) to ensure that you don't overwrite the source image if someone uploads a file called 'car-large.jpg', etc.

作为一个小调整,我很想将生成的缩略图等放在不同的路径中(例如:../ generated /),以确保如果有人上传了一个名为'car的文件,则不会覆盖源图像-large.jpg'等