-1

I have a database which contains images saved as BLOBs. I can successfully use the image on a page like so :

<img src="<?php echo 'data:image/jpeg;base64,'.base64_encode($image)?>" alt="Landing" width="150px">

However this requires setting the file extension manually in the actual statement at data:image/jpeg;. The problem is that I have lots of various images in various formats. I want to make sure that the filetype is set properly based on the actual file extension of the specific file for each image. I already have a nested array which contains all the file extensions for those files.

Nonetheless I am having trouble setting the extension dynamically. I've tried simply replacing the '' single quotes with "" to allow me to easily use a variable inside of the statement like so :

<img src="<?php echo "data:image/$images['monitor']['extension'];base64,".base64_encode($image)?>" alt="Landing" width="150px">

This doesn't work because the src tag itself contains double-quotes already I believe. My IDE tells me an error Cannot use '[]' for reading. I've also tried using concatinated single quotes instead :

<img src="<?php echo 'data:image/' . $images['monitor']['extension'] . ';base64,'.base64_encode($image)?>" alt="Monitor" width="150px">

Which also did not work. I was unable to find any solution to this online myself. Is there any way to dynamically set the file extension? Although setting jpeg for each image mostly works for instance doing so for the image/x-ico tab icon renders the image unable to load properly.

541daw35d
  • 141
  • 2
  • 12
  • 1
    The syntax of the latter code snippet should work fine. If you don't get the desired result - then your variables probably did not contain what you think they should. – CBroe Nov 18 '21 at 15:09
  • When I use I simply get "png" as an output so I don't belive that that's the case. – 541daw35d Nov 18 '21 at 15:12
  • 1
    No, with _that_ you would simply get an error message :-) And which type is the image in question now, jpeg or png? You are applying the base64 encoding to `$image`- what is the relation between that variable, and `$images['monitor']['extension']`? – CBroe Nov 18 '21 at 15:17
  • 1
    The specific image that I'm talking about is a PNG. The $image variable is a BLOB which contains a base64 string for the BLOB image. The $images variable is an array which holds the information for each image like array(monitor => array(date => '28.01.2021', extension => 'png'), phone => array(date => '29.01.2021', extension => 'jpg')); – 541daw35d Nov 18 '21 at 15:24
  • 1
    _"The $image variable is a BLOB which contains a base64 string for the BLOB image."_ - if that was really the case, then you would be base64 encoding it a _second_ time with `base64_encode($image)`. – CBroe Nov 18 '21 at 15:26
  • Then how come simply hardcoding the file extension like Landing work? If the $image variable did not contain the base64 string the image would not load no matter what no? – 541daw35d Nov 18 '21 at 15:28
  • 1
    And with `extension => 'jpg'` it is probably not going to work in general, because the _correct_ mime type for JPG images is `image/jpeg`, and not `image/jpg`, see https://stackoverflow.com/q/33692835/1427878 – CBroe Nov 18 '21 at 15:29
  • _"Then how come ..."_ - because your $image variable apparently does _not_ contain base64 encoded data then, but the _binary_ image data. – CBroe Nov 18 '21 at 15:30
  • You are right indeed I got confused. The BLOB does contain binary data not a base64 encoded string. – 541daw35d Nov 18 '21 at 15:37
  • Check the HTML code that gets generated. If it properly shows `data:image/jpeg` (or `data:image/png`) in there, then most likely your image does not actually match the specified type. Not sure why you would handle this data, that clearly belongs together logically, in separate variables to begin with - why is the actual image data contained in `$image`, and not `$images['monitor']['data']` or something. – CBroe Nov 18 '21 at 15:58

1 Answers1

1

Assuming that the BLOB contains the actual binary data of the image.

Just make sure that the extensions match with the required syntax

jpg file : <img src="data:image/jpeg;base64,[base64_encoded_data]

png file : <img src="data:image/png;base64,[base64_encoded_data]

ico file : <img src="data:image/icon;base64,[base64_encoded_data]

So a sample example is like the following:

<?php
$image=file_get_contents("http://www.createchhk.com/SO/sample1.png");

$file_ext = 'png';
?>

Test for PNG<br>
<img src="data:image/<?php echo $file_ext; ?>;base64,<?php echo base64_encode($image)?>" alt="Landing" width="50px"><br>


<?php
$image2=file_get_contents("http://www.createchhk.com/SO/sample1.jpg");
$file_ext2 = 'jpeg';
?>


Test for JPG<br>
<img src="data:image/<?php echo $file_ext2; ?>;base64,<?php echo base64_encode($image2)?>" alt="Landing2" width="50px"><br>

<?php
$image3=file_get_contents("http://www.createchhk.com/SO/sample1.ico");
$file_ext3 = 'icon';
?>


Test for JPG<br>
<img src="data:image/<?php echo $file_ext3; ?>;base64,<?php echo base64_encode($image3)?>" alt="Landing3" width="50px"><br>

The result can be seen here:

http://www.createchhk.com/SO/testSO_18Nov2021.php

Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • This worked! I've moved data:image/ and ;base64, to the HTML instead of the echo and the image did load! Thank you! – 541daw35d Nov 18 '21 at 16:28