Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Base64 PNG in MySQL

I am using Sencha Touch to capture data from a user on an iPad. This includes a standard form (name, email, etc.) as well as the customer's signature (see the plugin here).

Essentially, the plugin takes the coordinates from the user's signature and gives me back Base64 PNG data.

Once I have the signature data, I want to store it. My two questions are:

  1. Should I store the Base64 data in my (MySQL) database along with the rest of the user's information, or should I create a static file and link as necessary?

  2. If storing in the database is the way to go, what data type should I use?

like image 691
timmyc Avatar asked Mar 30 '11 20:03

timmyc


People also ask

Is it okay to save Base64 image in database?

While base64 is fine for transport, do not store your images base64 encoded. Base64 provides no checksum or anything of any value for storage. Base64 encoding increases the storage requirement by 33% over a raw binary format.

Can a PNG be Base64?

To convert PNG to base64 data: Click on "Choose file" and upload the PNG image from your system. Click on the "Convert" button to convert the PNG image into base 64 data. You can "Download or Copy" the base 64 code for further use.

How do I encode a Base64 PNG?

Paste the URL or select a PNG image from your computer. If necessary, select the desired output format. Press the “Encode PNG to Base64” button. Download or copy the result from the “Base64” field.


2 Answers

There's no need to base64 encode the image. MySQL's perfectly capable of storing binary data. Just make sure you use a 'blob' field type, and not 'text'. text fields are subject to character set translation, which could trash your .png data. blob fields are not translated.

As well, base64 encoding increases the size of text by around 35%, so you'd be wasting a large chunk of space for no benefit whatsoever.

However, it's generally a bad idea to store images in the database. You do have the advantage of the image being "right there" always, but makes for absolutely huge dumps at backup time and all kinds of fun trying to get the image out and displayed in your app/web page.

it's invariably better to store it externally in a file named after the record's primary key for ease of access/verfication.

like image 172
Marc B Avatar answered Sep 28 '22 05:09

Marc B


Just save files in BLOB field. Such PNG file shouldn't be larger than 1KB if you turn some optimizations (grayscale or B/W).

Storing files outside DB seems easy but there are things to consider:

  • backup,
  • additional replication if multi-server
  • security - access rights to files dir, but also to files,
  • no transactions - e.g. DB insert ok but file write fails,
  • need to distribute files within multiple directories to avoid large dir listings (depends on filesystem capabilities)
like image 44
gertas Avatar answered Sep 28 '22 07:09

gertas