1

I have some jpeg images as base64 encoded

I checked the mime type this way

$str = "..."; // base64 code

$imgdata = base64_decode($str);

$f = finfo_open();

$mime = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);

echo $mime . '<br><br>';

result - image/jpeg

Trying to display the image

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

result - there is no any image

echo $str;

result - base64 code is echoed

is there any way to check what is wrong with the base64 code

here is a live example - https://abuena.net/base.php

qadenza
  • 9,025
  • 18
  • 73
  • 126
  • If abuena is your site I suggest you to remove the link and just post a picture of it, as it can hurt your server's CPU if it keeps loading with no ending. People might abuse it when they see the link – Mootje Oct 19 '20 at 19:38
  • Why use base64 in the first place? Why not serve your image from PHP code directly? It's much more efficient. – Brad Oct 19 '20 at 19:43
  • @Brad, I have not original image saved, only the code – qadenza Oct 19 '20 at 19:45
  • @Mootje, thanks a lot but on the page image the code would not be visible – qadenza Oct 19 '20 at 19:48
  • make sure your base64 encoded image is valid by creating the image . this post might help you on how to validate image of base64 - https://stackoverflow.com/questions/12658661/validating-base64-encoded-images – Al-Amin Oct 19 '20 at 20:02
  • @Al-Amin - yes, the code is not valid. Trying to open in GIMP - and getting the error - `Corrupt JPEG data: 298 extraneous bytes before marker 0x36` – qadenza Oct 19 '20 at 20:22

1 Answers1

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

There should be no whitespace after "base64," - if that's not only a typo, it's likely the reason for the image not being shown.

Honk der Hase
  • 2,459
  • 1
  • 14
  • 26