How to convert image data in NSDATA's format( Hexadecimal ) coming through XML-RPC to actual Image in PHP
I am facing a very confusing issue where my Iphone app send Image data as a string in NSDATA format using XML-RPC to webserver. I am also maintaining XML-RPC log to save every request coming from iPhone to web server. My problem is when i received Image Data from xml-rpc and save it to actual image file, the image get corrupted.I am receiving following XML-RPC request from iphone.
<?xml version="1.0"?><methodCall><methodName>ipad.dataSync</methodName>
<params><param>
<value><string><ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966
00004d4d 002a0000 00080002 01120003 00000001 00010000 >.....continued data of image>
</string></value>
</param><params>
</methodCall>
When i save the image data to actual file using following code, the image get corrupted.
$image_name = "my_image_name.png";
$image_bits_data = "<ffd8ffe0 00104a46 49460001.....>"; //long hexadecimal formatted string of image from iphone
$fp = @fopen( $image_name, 'w+' );
if($fp)
{
if (fwrite($fp, $image_bits_data) === FALSE)
{
echo "Cannot write to开发者_如何学JAVA file ($image_name)";
exit;
}
else
{
fclose( $fp );
clearstatcache();
echo "File is successfully uploaded.";
}
}
else
{
echo "File can not be created.Please check the path and directory
permission";
}
The image gives its appropriate size like 50KB etc but when i open image it get corrupted and image is not showing in Picture Viewer or browser. Please if anybody got a clue or have solution about this issue then please share it. THanks
It looks like youre saving an string of space separated hex numbers into PNG file - that obviously can't be right. At least try to convert it to binary, something like
// get rid of the "<" and ">" in the begginig/end of the str so it looks like
$image_bits_data = "ffd8ffe0 00104a46 49460001....."; //long hexadecimal formatted string of image from iphone
$fp = fopen( $image_name, 'w+b');
foreach(explode(" ", $image_bits_data) as $hex){
fwrite($fp, pack('H', $hex));
}
You might have to fiddle with byte order too (ie use "h" in pack()
). But it doesn't look too promising - there is two ff
bytes in the beginning of the data and AFAIK no PNG file starts with such a header. It might be that there is some additional header which you must strip...
精彩评论