Thursday, 15 August 2013

javascript - How do I echo a a base64 string as an image to the browser using php? -



javascript - How do I echo a a base64 string as an image to the browser using php? -

i've got database base64 strings of images. i've built api phone call display these images uuid (example.com/image/5e7edbe0-a765-4863-9d75-9f89ccc532e0). value database, decode base64 binary, , echo browser follows:

$document = documentmodel::getfromdatabase($uuid); $type = $document->gettype(); // image/png $valuebase64 = $document->getvalue(); // data:image/png;base64,ivborw0kggoaaaansuheugaaaaiaaaaccaaaaabx3vl4aaaacxbiwxmaaastaaaleweampwyaaaab3rjtuuh3gysdcugsze0aaaaaa5jrefucndjrgjgymaaaaj0ah4sdhviaaaaaelftksuqmcc $value = base64_decode($valuebase64); header('content-type: ' . $type); echo $value;

unfortunately, 1 of these broken images icons saying image can't load. if take raw base64 value database , seek render in browser using javascript console below indeed shows image (a 2x2 pixel gray image).

var img = new image(); img.src = "data:image/png;base64,ivborw0kggoaaaansuheugaaaaiaaaaccaaaaabx3vl4aaaacxbiwxmaaastaaaleweampwyaaaab3rjtuuh3gysdcugsze0aaaaaa5jrefucndjrgjgymaaaaj0ah4sdhviaaaaaelftksuqmcc"; document.body.appendchild(img);

so don't understand why echo'ing binary value doesn't work. know i'm doing wrong here? tips welcome!

the string in $valuebase64 contains data uri, unacceptable php's base64_decode() function, since accepts pure base64 encoded data.

in order programme work, you'll have strip header (the data:image/png;base64, part - including comma) input data.

javascript php html image base64

No comments:

Post a Comment