Scale/resize image stored MySQL database - mysql

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.

Related

How to convert MySQL blob to base64?

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.

Best practice on storing Node.js Buffer in MySQL

How to store Buffer in MySQL with Node.js?
One way I know is to convert Buffer to hex string and save it as CHAR type in MySQL. But is it the best practice to transform before and after saving in MySQL?
Is there a way that can directly save and get the Buffer (bytes array) in MySQL with Node.js, for example, using BLOB in MySQL?
Or actually it doesn't matter what kind of way I use, they don't differ so much?
I am not aware of a best practice. But I can share what works for me.
If I'm working with objects, I prefer storing them as base64 (to cut down on character length over base16) in a longtext column and gzipping them .
It's more helpful to store them as strings, I think, because if you store them as buffers, then I don't think there is a way to copy them out of the database (it's really helpful troubleshooting sometimes), and translate them back to your object - at least, I haven't found a way to do that.
For example (assuming Nodejs 6+ for Buffer.from() syntax, otherwise use new Buffer()):
const obj = {};
const zip = zlib.gzipSync(JSON.stringify(obj)).toString('base64');
And, undoing:
const originalObj = JSON.parse(zlib.unzipSync(Buffer.from(zip, 'base64')));
Since longtext equals ~4GiB, you shouldn't ever hit the limit, unless you have massive objects. But then I suppose you have an efficiency problem elsewhere.
Note: There is obviously a lot packed into one line there (i.e. zip, parse, buffer, etc.).
A lot can fail in that one line.
So make sure you handle it appropriately.

How to convert multiple images to csv?

I want to run some images through a neural network, and I want to create a .csv file for the data. How can I create a csv that will represent the images and keep each image separate?
One way to approach is to use numpy to convert image to array, which can then be converted to a CSV file or simply a comma separated list.
The csv data can be manipulated or original image can be retrieved when needed.
Here is a basic code that demonstrates above concept.
import Image
import numpy as np
#Function to convert image to array or list
def loadImage (inFileName, outType ) :
img = Image.open( inFileName )
img.load()
data = np.asarray( img, dtype="int32" )
if outType == "anArray":
return data
if outType == "aList":
return list(data)
#Load image to array
myArray1 = loadImage("bug.png", "anArray")
#Load image to a list
myList1 = loadImage("bug.png", "aList")
You can encode your image in Base64 and still use CSV, since commas are not part of characters in Base64.
See: Best way to separate two base64 strings
If possible, create a storage location just for images. If your images have unique filenames, then all you need to track is the filename. If they do not have a unique filename, you can assign one using a timestamp+randomizer function to name the photo. Once named, it must be stored in the proper location so that all you need is the filename in order to reference the appropriate image.
Due to size constraints, I would not recommend storing the actual images in the csv.
Cheers!
I guess that depends a lot on what algorithm and what implementation you select. It is not even clear that CSV is the correct choice.
For your stated requirements, Netpbm format comes to mind; if you want to have one line per image, just squish all the numbers into one line. Note that the naive neural network will ignore the topology of the image, you'd need a bit advanced setup to include it.

Saving a canvas drawn image to a mysql database

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

Convert blob to text in a mysql export

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.