Php:Convert a Blob into an Image File

Php : Convert a blob into an image file

If the BLOB contains the binary data of an image (in a recognizable format like e.g. tiff, png, jpeg, etc), take the content of the BLOB, write it to a file, and voilà... you got an image.

On some strange operation systems you have to give the output file a correspondig extension, so that the image file can be recognised as such.

converting blob image to file in php

The BLOB is the binary image. You can write that image to your filesystem once it's on your server. So if your image is in the variable $my_blob, you would do something like

file_put_contents('/path/to/new/file_name', $my_blob);

and there you go.

You might want to save the file to a tmp location first, then do some checks on it before you move it to a final location (with PHPs rename() function).

Btw: why not just save the BLOB to DB? That is a legitimate way of handling files these days, that's what the BLOB MySQL data type is for after all.

PHP : How to display image from blob

The src attribute of an image MUST point at a url. You cannot dump the raw binary contents of an image in there and expect it to work. The browser will take that raw binary data and try to hit the page's originating server and request that data as if it was a file url. i.e. you have this on a page loaded from http://example.com/foo/bar/baz.php:

<img src="blahblahblahblah" />

which will result in the browser requesting

http://example.com/foo/bar/blahblahblahblah

If you want to embed your image in the page, then you have to use a Data URI:

<img src="data:image/jpeg;base64,<?php echo $base64_encoded_image ?>" />

convert blob image and upload to storage/s3 folder

Use put function instead of putFile

Try this

$imageContent = 'ÿØÿàJFIFÿáExifII*......'; // LONGBLOB data

Storage::disk('my_disk')
->put('images/users/travel.jpg', $imageContent)

For more information Storage disk instances

How to convert BLOB Image to be displayed in Web page

Try to return image as an array. I think you do, So Try the following,

echo '<img src="data:image/jpeg;base64,'.base64_encode( $photo->image['image'] ).'" />';

Because of you are using addslashes in this statement,

$imagetmp = addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

You have to add stripslashes when displaying the image like this,

echo '<img src="data:image/jpeg;base64,'.base64_encode(stripslashes($photo->image)).'" />';

And also I don't see you are inserting $imagetmp anywhere. When I checked your base 64 string it shows a broken image. So I assume that your image doesn't upload correctly.

html FileReader : convert blob to image file?

First, there isn't anything in $_POST["data"] because the index (data) is named by the key in the JSON key : value pair, not the JavaScript var.

Secondly, you should change this:

$Handle = fopen($FileName, 'w');
fwrite($Handle, $data);
print "Data Written";
fclose($Handle);

to this:

file_put_contents($FileName, base64_decode($data));


Related Topics



Leave a reply



Submit