If you really need to store it in blob data, you can do this:
For PHP >=5.2.0, you can use the data://
stream wrapper to retrieve file from a data uri:
$file_in_blob = file_get_contents("data://".$var_containing_the_data_uri);
where $var_containing_the_data_uri
should be replaced by the variable containing the data URI, for example $_POST['image']
. Then you can insert the $file_in_blob
to the database.
When you need to display it in a webpage again, you can:
- write a PHP script that retrieves the blob content according to a GET parameter, and use it as an
src
for the image tag, or
- directly use the original data URI obtained from
toDataUrl()
as the src
of the image tag.
If you use method 2, you don't actually need to decode the data URI into blob, but store the data URI directly. (Of course you may be concerned of the data size...)
Method 1 example:
Your HTML:
<img src="img_from_db.php?img=1" />
Your img_from_db.php
:
<?php
header('Content-Type: image/png'); // make the browser recognize it as PNG
echo get_image_blob_from_db($_GET['img']); // you may wish to add some checks
Method 2 example:
<img src="<?=get_image_dataURI()?>" />