I have this code
echo ' <img class="postedImage" src="data:image/png;base64,'.base64_encode($Img).'"/ type="image/png"></img><br/>';
and I keep getting this display https://gyazo.com/ad1f8096798099d96c0d305a24176182
My database has the images stored in as longblob.
When echoing my base64 encode img, I get this which is basically just the images data. The image being uploaded is a png and I still get same results.
iVBORw0KGgpcMFwwXDANSUhEUlwwXDBcMJVcMFwwXDCNCAZcMFwwXDC+XCc4uVwwXDBcMAlwSFlzXDBcMA7DXDBcMA7DAcdvqGRcMFwwA+xJREFUeF7t3euRozAQRWHHRUCKh2hIhmBY2mtPeTyMB9TXRq0+X1X/2mcVZyWBH3tZXDAxooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyBEV5IgKckQFOaKCHFFBjqggR1SQIyrIERXkiApyRAU5ooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyOWLap6XaRqXUsoyDMM6l+Vy2Zjrjw3XnzeOk/0y7JQjqnlaxjJsx3NwhjIuE4W91HVUs61Iv61E3rFVbFwiri19RrWuTG+L6XmGsq5cXLc/F1fdRTWPmm3u2LBqPeooqvVcMF62LvjnZhgJy3QS1flB3YewOomqlaDuU6bbXyyp+FFNZfPCnjvDknnBCh7Vepe3eVEbmGFcXDflnEJHdc6d3v7Jug0GjqrhVeo+SVeruFG5z1LD10suzxd+ttcHR3ttcOvXHZmcZ6uwUXnu+IZ1X9p7rec13mHj99g7GR8xBI2qfuurusjzWB9Wwi0wZlS1W5/j5Fxcf1NQ1n8CuYSMqu4Ce8838zJWnbHynasCRlV5cQX39+fEHE/AqOrOU5JnRlXbLlG1r+rQLLqwZ/7ZgcSM6vD2pzos16ySRBWPPby0maZlshnL9cMKpfz/4MI1QNltPVHtET+qT6ra/nikgFdqDuo8/MQrVS8NJXyrAlHtduKjjGCIaq+qZ1T5zlOGqHY57yl+RES1Ay/PHENUf6l920vSVcoQ1Uu179vKu0oZovrVGlTVW13WSbxKGaLaVPveKZucd3yPiOoHT1B8OtkQ1Te+oLJve3dE9cUZFNveF6K68gaV+27vGVF57vJuw673Xe6o7GscN1wiOTJ8H9VPeaOqfVL+MAS1LWdUBPVW+aISBMWXxr6WKyrnl23YXR6H8r/liUoQFDvePjmiqnrX5uMUvoD/gP6j8gZl/6PD7bfCPn1H5Q4q75fBevQblfcMtZ7ICapOn1E5HxvY1zeiXn9ReYPiFs+ts6h8r+URlEZHUXnfsUlQKt1E5foKbFYoqT6i8jw64FAuFz8qz8Gc51BvETwqzzmK1/LeJXRUrf8vWvfJtsMGjsr/VuBPDVEFEWWVsiGqEOKsUjZEFUCkVcqGqJrn/eDn54eoWlwn+ODCp4eoGhdt67MhqqbF2/psiKppse767kNULQt4nrIhqoZFPE/ZEFXDiCqGYGcqREBUkCMqyBEV5IgKckQFOaKCHFFBjqggR1SQIyrIERXkiApyRAU5ooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyBEV5IgKckQFOaKCHFFBjqggR1QQW5Z/8Y/csUo9rX1cMFwwXDBcMElFTkSuQmCC
Normally this should work
echo '<img src="data:image/jpeg;base64,'.base64_encode( $data['image'] ).'"/>';
you can also try with data:$mime
where $mime:
mime can be an image of any kind, text, word document, text document,
pdf document, e.t.c... data is blob's column content
Please let know if this works for you, thanks.
Related
Is it possible to create a temp file server side for viewing an image?
I've some images & they're displayed from server but I want to have them watermarked... Found out about the Imagick Class, used composite... It can be viewed when sent as header but I need it somehow visible for the tag.
I don't want to uses 'write image' & create a new image from the composited image. I'm trying to use it as client Side thing (I'm not familiar with JS... Yes, I'll look into it but need an alternative till then).
Another alternative that I'm aware of is I should control that WHILE uploading the images but I've already uploaded quite a few so...
Any help appreciated!
Have you tried using the function that generates your image as a src attribute for the <img /> tag?
I don't know exactly how your code is, but I am thinking the end result could be something like:
<img src="data:image/jpeg;base64,<?php echo base64_encode($image_headers); ?>">
You basically output the headers inside the image src as a base64 encoded string.
What you should keep in mind is that you should adjust the image/jpeg type to the corresponding type of your image.
For example:
For JPG, JPEG files it would be image/jpeg
For PNG files it would be image/png
For WEBP files it would be image/webp
And of course the encoded string should be an image of the corresponding format.
Good day
I'm trying to show an image using DATA URIs and encoding a string in base64 as shown below:
<img src="data:image/jpeg;base64,<?php base64_encode($FOTO) ?>" />
The image (JPG) is stored in the database in this way:
The problem is that using that DATA URI, it does not show the image on the screen. I don't know the error or if I have to do something different to be able to show that stored image.
Is there any other way to display these images stored in SQL Server?
Thank you very much for your time.
The data in db is not base64, you need to convert this hexadecimal data to base64. Php, as I know doesn't have a direct function to do that but it can be achieved with pack(). I leave it to you as homework to study this function.
Convert the data from db to base64 like:
$fFoto = base64_encode(pack('H*', $foto));
Then pass this $fFoto to image src like you are already doing.
<IMG src="data:image/jpeg; base64, <?php echo $fFoto; ?>" />
I have a little problem here.
I try to convert an image into string base64, after that I want to save the string into blob in MySQL.
So, the blob can be displayed on the mobile apps.
this is my code :
$data = file_get_contents($_FILES["picture"]["tmp_name"]);
$image = base64_encode($data);
I already successfully save the blob into MySQL, but I can not displayed the image in website.
<td> <img src="<?php echo base64_decode($user->getPicture()); ?>"></td>
because the result is : ������� and many more
Am I wrong ?
Please correct me :)
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 ?>" />
I have some data stored in a MySQL database .. i would like to show the stored image data along with other data in a .php page..
if i fetch data from the database and use header("Content-type: image/jpeg"); its not possible to show the image with other php data.. is there a a other way ?
Read this: displaying an image stored in a mysql blob
If you set the header to image/jpeg that treats your entire page as an image file.. You want the data to be insert into the image holder only.
Try something like this
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
Where you will next echo the blob data into the image src
<img alt="Embedded Image" src="data:image/png;base64,<?php echo $image->blob_data; ?> "/>
It depends how you stored you data, but sometimes you have to convert the data to base64. Try this
echo '<img src="data:image/png;base64,' . base64_encode($blob_data) . '"/>
All,
I am executing a PHP script through CURL which returns a PNG file as an output. How can I show the png file in a php web page?
echo '<img src="urltotheimage.php" alt="Alt text" />';
First of all, why do you need to fetch a PNG through CURL? There's better ways to get a image from server, for example using the <img /> tag..
Anyways, I assume you are getting the binary data in a variable, you can output the image by setting appropriate headers and echoing the data:
header('Content-type: image/png');
echo $image;
as you are receiving the string representing the png and if that is base64 encoded then you can embed directly on the image tag as follows:
<img src="data:image/png;base64,aAbBcCdDeEfFgGhH..." />
where aAbBcCdDeEfFgGhH... would be the image string.
see more on data uri's here: http://en.wikipedia.org/wiki/Data_URI_scheme#Inclusion_in_HTML_or_CSS_using_PHP