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 ?>" />
Related
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'm displaying my BLOB image like this and it is working.
<img src="<?php echo 'data:image;base64,'.$row_img['image']; ?>" alt"" />
But when i try to copy image link and open it in url, it show me encrypted code like this :
ÿØÿà�JFIF�����ÿÛ�C�ÿÛ�CÿÀ�6î"�ÿÄ�����������
ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ��������
ÿÄ�µ��w�!1AQaq"2B‘¡±Á #3RðbrÑ.....
That's the data url displayed as text, the browser doesn't know its a jpeg.
Add the correct mime type to the url and it should work
echo 'data:image/jpeg;base64,'.$row_img['image'];
Your content type for your data is wrong. image is not valid. It should be image/png or image/jpeg or something similar depending on the type of image.
From what you posted, it looks like your image is a JPEG.
Because the browser has no way to know the mime type with inline image. Open them with img tag is Ok because you tell the browser it's an image. But without context in a new tab it s just a random string
EDIT : use a full correct mime
<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" alt"" />
I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.
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