crwork Posted October 10, 2012 Share Posted October 10, 2012 Question regarding the decoding and display of base64 images. I have a site that will display a user avatar at the top of the content page. There is a web service I'm calling that receives a user ID as input and retrieves a base64 encoded image. I'm getting the base64 image back successfully with this code: //Get base64 image from XML return in cURL resource $userImageData = curl_exec($ch); //Extract the base64 image $userImageXML = new SimpleXMLElement($userImageData); $userImage64 = $userImageXML->return; //Decode the base64 image $imgDecoded = base64_decode($userImage64); What isn't working is when I display the variable for the decoded image, $imgDecoded, in an IMG tag. It just shows a long string of odd characters. However, what DOES work is if I display the encoded base64 image that has not been decoded, variable $userImage64: echo "<img src=data:image/png;base64,$userImage64 title='User Avatar' >"; I'm confused, and this is probably a simple explanation, as to why the decoded base64 image, variable $imgDecoded won't display in the IMG tag. I feel like I'm missing something basic, but I'm a bit stuck on this one... Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/ Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Keep it as base 64, don't decode it. You're telling the browser "this image tag is base64 encoded data" and sending it DECODED data. Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384247 Share on other sites More sharing options...
kicken Posted October 10, 2012 Share Posted October 10, 2012 I'm confused, and this is probably a simple explanation, as to why the decoded base64 image, variable $imgDecoded won't display in the IMG tag. You can't just stick raw image data into an <img> tag. You would have to write the decoded data ($imgDecoded) to a file, then reference that file in the <img> tag. Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384248 Share on other sites More sharing options...
crwork Posted October 10, 2012 Author Share Posted October 10, 2012 Ok, got it. Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384250 Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 You can't just stick raw image data into an tag. You would have to write the decoded data ($imgDecoded) to a file, then reference that file in the tag. Some browsers support the data protocol. But I wouldn't rely on it. Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384259 Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Did you really just use an MSDN link to justify yourself? Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384261 Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 Did you really just use an MSDN link to justify yourself? Sure did: MSDN is pretty great, actually. There's also Wikipedia and the RFC if you'd rather read those. Also, I retract my statement about not relying on it. Seems browser support has increased to nearly everyone. Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384262 Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Much better! Quote Link to comment https://forums.phpfreaks.com/topic/269315-displaying-base64-images/#findComment-1384265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.