存储在数据库中的图像直接或为Base64的数据? [英] Storing image in database directly or as base64 data?

查看:2687
本文介绍了存储在数据库中的图像直接或为Base64的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储在数据库中的图像的常用方法是将图像转换成的base64数据然后存储。这个过程会增加尺寸的33%。可替代地,可以直接存储图片的BLOB;例如:

The common method to store images in database is to convert image to base64 data then storing. This process will increase the size 33%. Alternatively it is possible to directly store image BLOB; for example:

$image = new Imagick("image.jpg");
$data = $image->getImageBlob();
$data = $mysqli->real_escape_string($data);
$mysqli->query("INSERT INTO images (data) VALUES ('$data')");

和与显示图像

<img src="data:image/jpeg;base64,' .  base64_encode($data)  . '" />

通过后一种方法,我们节省1/3的存储空间。存储的base64数据到数据库中,为什么是在数据库中存储的图像常用的方法?

By the latter method, we save 1/3 storage space. Why storing base64 data into database is the common method for storing images in database?

更新:大约有许多优点辩论和数据库存储图像的缺点,大多数人认为它不是一个实用的方法。无论如何,这里我假设我们存储的图像数据库,并讨论最好的方法这样做。

UPDATE: There are many debates about advantages and disadvantages of storing images in databases, and most people believe it is not a practical approach. Anyway, here I assume we store image in database, and discussing the best method to do so.

推荐答案

专业的base64:带连接codeD重新$ P $你处理psentation是pretty安全的字符串。它包含既不控制字符,也没有报价。后一点有助于防止SQL注入尝试。我不希望任何问题到刚刚添加到手codeDSQL查询字符串。

Pro base64: the encoded representation you handle is a pretty safe string. It contains neither control chars nor quotes. The latter point helps against SQL injection attempts. I wouldn't expect any problem to just add the value to a "hand coded" SQL query string.

专业的BLOB:数据库管理软件,知道它会发生什么类型的数据。它可以优化的。如果您想存储的base64文本字段它可能会尝试为它的一些指标或其他数据结构,这将是非常好的,有益的真正的文本数据,但没有意义的,时间和空间的图像数据的浪费。它是较小的,如在字节数,再presentation。

Pro BLOB: the database manager software knows what type of data it has to expect. It can optimize for that. If you'd store base64 in a TEXT field it might try to build some index or other data structure for it, which would be really nice and useful for "real" text data but pointless and a waste of time and space for image data. And it is the smaller, as in number of bytes, representation.

这篇关于存储在数据库中的图像直接或为Base64的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆