Storing Base64 PNG in MySQL

I use Sencha Touch to capture data from a user on an iPad. This includes the standard form (name, email address, etc.), as well as the client's signature (see Plugin here ).

In essence, the plugin takes the coordinates from the user signature and returns me Base64 PNG data.

As soon as I have the signature data, I want to save it. My two questions are:

  • Should I store Base64 data in my (MySQL) along with the rest of the user information or am I creating a static file and link as needed?

  • If saving to a database is the way to go, what data type should I use?

+4
source share
4 answers

No need to encode a base64 image. MySQL is very good at storing binary data. Just make sure you use the blob field type, not text. text fields are subject to a character set, which can ruin your .png data. Blob fields are not translatable.

In addition, base64 encoding increases text size by about 35%, so you're wasting a big chunk of space without any benefits.

However, it is usually a bad idea to store images in a database. You have the advantage that the image is always โ€œright thereโ€, but it makes absolutely huge dumps during backup and all kinds of entertainment that try to get the image and display on your website.

it is invariably better to store it externally in a file with a name after the primary record key for easy access / verification.

+6
source

Blob will store Base64. This will give you what you need. Storing it in a database gives you built-in relational capabilities that you have to code yourself if you saved it in a static file. Hope this helps. Good luck sir.

Edit: mark binary right v. base 64

+2
source

Just save the files in the BLOB field. Such a PNG file should not exceed 1 KB if you enable some optimizations (grayscale or black and white).

Storing files outside the database seems simple, but there are things to consider:

  • Reserve copy
  • additional replication if multiserver
  • security - access rights to dir files, but also to files,
  • no transactions - for example. DB insert ok, but file write failed,
  • it is necessary to distribute files in several directories to avoid large dir lists (depends on the capabilities of the file system)
+2
source

Set your field as a Blob data type, it stores base64EncodedString perfectly

0
source

Source: https://habr.com/ru/post/1345944/


All Articles