Friday, 19 September 2014

PHP - Decode XML Embedded File to Image



So i'm currently trying to wrap my head around this, i've tried every method i could find online but to no avail.


In my XML file i have a section named , by standard if i echo this out like so:-



echo "<p><b>Attached:</b> ".$xml->SearchOrder->SubjectProperty->PlanAttachment->EmbeddedFileBinaryObject. "</p>";


It displays it as it is in the XML, which makes sense, so then i tried to decode it by doing this:-



$imagedata = ($xml->SearchOrder->SubjectProperty->PlanAttachment->EmbeddedFileBinaryObject);
$binary = base64_decode($imagedata);
echo $binary;


After doing so i get a mixed array of characters, so this may be the point of the problem but either way i tried placing code to present this image like:-



<img src='data:image/jpeg;base64,".$imagedata."'/>
<img src='data:image/jpeg;base64,".$binary."'/>


The first one shows an error image and the second one just shows all the messed up characters like when i echo'd $binary, so i tried using file_put_contents like so in hopes that would work to:-



file_put_contents("picture.jpg", $binary);


But that did not work either, are there any suggestions for this? It would be greatly appreciated, also here's the full thing to make more sense of the structure:-



<html>
<head>
<title>Title</title>
</head>

<style type="text/css">
body{font-family: sans-serif;font-size: 14px;line-height: 18px;}
b{display:inline-block;}
p {border-top: 1px solid #ccc;padding-top: 10px;}
</style>

<body>
<?php

$xml = simplexml_load_file("ForwardSearchOrderSend_Example.XML");
$imagedata = ($xml->Form->SubjectProperty->PlanAttachment->EmbeddedFileBinaryObject);
$binary = base64_decode($imagedata);
/* print_r($xml);*/

echo"<br><br><h2>Embedded File</h2>";;
echo "<p><b>Binary Object:</b></p>"

echo "<img src='data:image/jpeg;base64,".$binary."'/>";
/*echo $binary;*/
?>


</body>
</html>

No comments:

Post a Comment