Description of the issue:
I have an Insert script contains the binary data (image file) which used to be stored in Mysql DB as a blob, and the requirement is to convert the blob data to base64 and store it in SQL Server. I tried inserting the blob data directly to SQL Server as Image and varbinary data type and converted them to base64, however, both methods failed to display the image on the web.
I tried exporting the image file from Mysql db and try to view it, but the format looks invalid.
I'm not sure what format the image file stored in Mysql DB and its hard to decode. Can anyone help me?
Sample file:
0xa15b8a9e56c2a7f9b56c4015a0eaf6abaf9f39575c028b39ffea32c35dad471522babf9f5abfeaea7eafaebafadf2d596
Where is the problem? MariaDB BLOB? SQL Server? Displaying Base64 as an image? Something else?
Here are some tips:
In MariaDB (at least) you can use UNHEX(some 8-bit data) as the expression to insert into a BLOB column.
In MariaDB (at least) you can use BASE64(some 8-bit data) to produce a text string that could be put into either TEXT or BLOB.
To display Base64 as an image in a <img> in HTML, you need to say that it is encoded that way. (The details are a bit obscure, but it works, and the string is part of the html.)
To display an image (from any of many locations), you need a separate program that runs in the web server, has the suitable "Header" and writes the binary (not Base64) bytes of the image. In this case, use, or example, <img src=foo.php?...> where foo.php uses its paramters ($_GET) to fetch the image (and, if neccesary, convert from Base64), then echo the Header the the image type and the binary of the image.
Related
I'm working on a database import/export process in VB.NET which writes data from a MySQL (5.5) database to a plain text file. The application reads the data to a DataTable, then goes through the rows/columns to actually write the data to the OutputFile (System.IO.StreamWriter object). The encoding on the tables in this database is Latin1. There is a MediumBlob field in one of the tables I've been using for testing which contains image files stored as a byte array.
In my attempts to validate the output from my application, I've exported the data directly from the database using the MySQL Workbench, then compared that with the results I get when I write the same data from my application. In the direct export from MySQL Workbench, I see some of these bytes are exported with the backslash. When I read the data through my application, however, this escape character does not appear. Viewed through Notepad++, it clearly shows some distinct differences between the two output results (see screenshot).
Obviously, while apparently very similar, the two are not completely identical. My application is not including the backslashes for escaped characters, and some characters such as NULL are coming out differently altogether. My code for writing this field to the file is:
OutputFile.Write("'" & System.Text.Encoding.GetEncoding(28591).GetString(CType(COPYRow(ColumnIndex), Byte())) & "'")
There doesn't appear to be an overload for the GetString method that allows me to specify an escape character, so I'm wondering if there's another way that, using this method, I can ensure the characters are correctly encoded, including escape characters.
I'm "assuming" that this method should also work in general when I start working with my PostgreSQL database, but with possibly a different encoding. I'm trying to build things as "generic" as possible, but I'll have to worry about specifying encodings at run-time instead of hard-coding them later.
EDIT
I just ran across another SO question, which might point me in the right direction: Convert a Unicode string to an escaped ASCII string. Obviously, it might take a bit more work to get it right, but this looks like the closest thing to what I'm trying to accomplish.
I need to store HTML data in a MySQL database. I read about this and found that the best method is to use NVARCHAR or VARCHAR. Furthermore I'd like to compress the input to make it less space consuming. I use PHP's gzdeflate() function for deflating the HTML input, but in this case what MySQL data type should I use?
EDIT: Since I need to store quite big HTML sources I decided to go with the TEXT data type but the question: Is MySQL's TEXT field compatible with a deflated HTML string?
Use BLOB type instead of TEXT to use it for binary data.
I searched a lot, perhaps inefficiently, but I can not find information on how to scale the image stored in the database?
I'm inserting my image as
"0xffd8ffe110e94578696..."
after ussing unpack:
$data = file_get_contents($_FILES['image']['tmp_name']);
$data = unpack('H*',$data);
$data = '0x'.$data[1];
And then I can preview my image using base64_encode:
<img src="data:image/png;base64,/9j/4RDpRXhpZgAATU0AKgAAAAg...e6/9k=">
Probably informations above are unnecessary, becasue it's obvious how to send img to DB and how to preview that image. But maybe it helps you to understand me.
So is there any way to achieve that?
Scaling your image is not a function of your database system. Instead, it's a function of your host language.
You need to retrieve the image from the database in which you've stored its byte stream as a binary large object (blob). You then need to use some software or other to rescale it. This most likely requires decoding the byte stream, resampling the image, and encoding a byte stream for a new image.
You then can store it back into the database. Depending on what your application needs, you might choose to UPDATE the row with the original blob, or INSERT another row with the new blob.
I was wondering about the best way to tackle this. I'm trying to save a user-drawn image on a HTML5 canvas to my database so I can retrieve it later.
I got as far as creating the base64 data string for the image with the following code, hooked to a simple button clickhandler:
var image_data = $("#drawing_canvas").get(0).toDataURL('image/png');
I was planning on saving that data to my database and then retrieving it later on with something like this:
var myImage = new Image();
myImage.src = imgData;
ctx.drawImage(myImage, 0, 0);
However, these base64 'strings' seem to contain a lot of data. I was wondering if there's a better way to save these images to my database? I have yet to figure out a way to save the actual image as a .png. I could get it to open as a png in a new browser tab, but that's where I'm stuck at the moment.
Or would it be fine to store these base64 data strings in my database (in, I suppose, a 'text' column)?
Thanks in advance.
You want to use a BLOB type. Here's what the MySQL docs say about it:
BLOB values are treated as binary strings (byte strings). They have no character set, and sorting and comparison are based on the numeric values of the bytes in column values. TEXT values are treated as nonbinary strings (character strings). They have a character set, and values are sorted and compared based on the collation of the character set.
http://dev.mysql.com/doc/refman/5.0/en/blob.html
I'd have some blob data such as:
0x3333332c2044e963617269652c20356520e9746167650d0a53742d4c617572656e7420285175e9626563292048344e20334d390d0a
that I'd like to convert to text because the new database has text field instead of blobs and now it makes trouble with some accentuated characters.
Is there somekind of blob to string converter somewhere?
Thanks a lot!
Try:
CONVERT(blobname USING latin1)
It depends on what the blob is. For example, I've dealt with some blobs that could be represented as basic XML files. Those would have been relatively easy to convert. However, I dealt with other blobs that were image files. If you tried to represent them as text you'd lose data.
What are in your blobs?
Create your new database with your export, once done create your text column on the table, update that using a CONVERT drop the old column, renaming the old one if required.
However if the data contains simple byte stream (that is, unstructured data, files, audio, video, whatever) and you need to represent them as pure ASCII you could change into a Base64 string.
If using phpmyadmin, tick the box that says "Dump binary columns in hexadecimal notation (for example, "abc" becomes 0x616263)" at the bottom of the export page.