繁体   English   中英

Mysql IMAGE表设计

[英]Mysql IMAGE Table Design

我是设计数据库的新手,我需要有关图片表的帮助。

CREATE TABLE LANGUAGE (
    id int(10) unsigned not null auto_increment,
    LANGUAGE VARCHAR(45),
    IMAGE_id INT(10) UNSIGNED NOT NULL,
    // some more
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE PRODUCT(
    id int(10) unsigned not null auto_increment,
    // some more
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE PRODUCT_IMAGE(
        PRODUCT_id INT(10) UNSIGNED NOT NULL,
        IMAGE_id INT(10) UNSIGNED NOT NULL
    )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE CATEGORY(
        id int(10) unsigned not null auto_increment,
        IMAGE_id INT(10) UNSIGNED NOT NULL,
        // some more
    )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

我想创建一个图像表,但是我还没有找到正确的方法。

  • 一种产品可能只具有一张图片
  • 每个类别和语言都有一张图片
  • 我想要一个可以在网站和应用程序中使用的弹性图像表
  • 我可能需要不同尺寸的图像

所以我设计了这样的东西

CREATE TABLE IMAGE(
    id int(10) unsigned not null auto_increment,
    extension varchar(5),  
    file_name varchar(45),
    file LONGBLOB,
    //some more
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

我应该保留图像的某些特定大小,然后在应用程序或网站中调整图像大小吗?

将图像放入数据库不是很安全,我不明白为什么要这么做...

create table imagetest (
    image_id        tinyint(3)  not null default '0',
    image_type      varchar(25) not null default '',
    image           blob        not null,
    image_size      varchar(25) not null default '',
    image_ctgy      varchar(25) not null default '',
    image_name      varchar(50) not null default ''
);
Then you can write an image to the database like:
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
mysql_connect("localhost", "$username", "$password");
mysql_select_db ("$dbname");
$sql = "INSERT INTO testblob
    ( image_id , image_type ,image, image_size, image_name)
    VALUES
    ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', 
     '{$_FILES['userfile']['name']}')";
mysql_query($sql);
You can display an image from the database in a web page with:
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("testblob");
$sql = "SELECT image FROM testblob WHERE image_id=0";
$result = mysql_query("$sql");
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
mysql_close($link);

我可以建议您不要将图像放入数据库,而只存储文件路径。 我将不做过多解释,因为它比我在这里可能做的更好。

我希望您考虑以下答案: 在DB中存储图像-是或否?

并可能阅读了詹姆斯·汉密尔顿的这篇可爱的文章: https : //perspectives.mvdirona.com/2008/06/facebook-needle-in-a-haystack-efficient-storage-of-billions-of-photos/

此外,这篇文章确实对我有所帮助,因为它说明了flickr.com的扩展方式。 http://highscalability.com/flickr-architecture

我知道在没有抓住链接本质的情况下抛出链接并不是一种好习惯。 但是本质可以归结为:不要将图像存储在db中。

您可以考虑将其用于图像表:

CREATE TABLE IMAGE(
    id int(10) unsigned not null auto_increment,
    image_location varchar(255),
    //some more
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

我将其保留为varchar,但如果您的路径长度将超过255,或者您的路径中的字符将被多字节编码,则可能需要将其更改为某种形式的文本格式,因为这会从255。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM