📌 CREATE TABLE `picture`
(
id int AUTO_INCREMENT PRIMARY KEY,
name varchar(128) not null,
data LongText not null
);
📌 max_allowed_packet=100M
在 my.ini 裡面做設置
可以用CTRL + F 快速查找
📌 class picture
{
public $path;
public $b64_img;
public function put_path($p)
{
$this->path = $p;
}
public function pic_base64()
{
$this->b64_img = chunk_split(base64_encode(file_get_contents($this->path)));
}
public function show_pic()
{
$val = "<img src=\"data:image/png;base64,";
$val = $val . $this->b64_img;
$val = $val . "\">";
return $val;
}
}
將照片轉為BASE64
📌 include "pic-b64.php";
$pic = new picture();
$pic -> put_path("temp.png");
$pic -> pic_base64();
$sql = new sql();
$sql->config("root","","pic","picture");
$data=['',$pic->path,$pic->b64_img];
$sql->put_data($data);
$sql->add();
temp.png 可以改為要加密的照片
📌 $data=["id","name","data"];
$sql->put_data($data);
$arr = $sql->sel();
$val = "<img src=\"data:image/png;base64,";
$val = $val . $arr[0]['data'];
$val = $val . "\">";
echo $val;
程式碼收錄:https://github.com/chyhhwen/pic-base64
相片存DB.通常壞處會大於好處。
好處是方便版本控制和分類
但效能和成本會提高
也有數據外洩的風險
確實,壞處會大於好處
這樣看起來,存取路徑會好點