介绍
PHP+mysql存储照片有两种方式,
1是通过数据库中存储图片的路径来实现图片的存储
2是把图片数据存储在数据库中
本文介绍就是第2种方法,将图片数据存储在数据库中。
方法封装
//图片生成Base64数据流
public function base64EncodeImage ($image_file) {
$base64_image = '';
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return $base64_image;
}
使用方法
//图片真实物理路径
$img=ROOT.DS.'public/images.png';
//调用图片生成base64数据流方法
$base64_img = $this->base64EncodeImage($img);
//将数据流存储在数据库中
$i=Db::table('lr_wx_info')->where('name','是滑稽啊')->update(['head'=>$base64_img]);
数据表的字段类型
字段名 | 类型 | 长度 | 小数点 | 允许空值 | 主键 |
---|---|---|---|---|---|
head | blob | 0 | 0 | √ |
评论