Store image in MySQL BLOB

早过忘川 提交于 2019-12-02 11:19:38

问题


I'm storing small thumbs in a MySQL database. With PHP i create a thumbnail from an image and i simply store that into a BLOB column.

It seems that MySQL saves the binary image as a Base64 string in the database. The problem is that MySQL seems to store it as a application/octet-stream instead of jpg file.

I know that because i tested the Base64 string with this code:

<?php
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);

$f = finfo_open();

$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);

echo $mime_type;
?>

Is MySQL automatically converting my image to a Base64 string? If so, is there a way to make sure it saves it as a JPG?


I know saving thumbs to the filesystem is better, but unfortunately this is not the case and i am NOT looking for answers to save it to the file system. I want to know WHY it is saved as octed stream.

I'm using WideImage to create the thumb, like so:

        $thumb = \WideImage::load ( $filepath )
                            ->resize(117, 88)
                            ->crop ( 'center', 'center', 117, 88 )
                            ->asString('jpg');

The variable $thumb contains valid binary data. Because i can print it and it looks good, but also fwrite writes a good JPG.

I then save it to the database with an INSERT query. But when i then SELECT it again it gives me back an octed stream format.


回答1:


The best approach is to store the image on your filesystem and then reference its location via MySQL. Storing images in MySQL is not recommended. Why put an extra unnecessary strain on your database? All of the large websites (Facebook etc) store their images on a filesystem.



来源:https://stackoverflow.com/questions/17751865/store-image-in-mysql-blob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!