0

When we are using a web service, we need to pass one argument as base64 binary,but I have to convert an image into byte array. I used base64_encode,but it didn't worked.

That web service is built on .NET,there that argument is treated as byte array.. So I would like to know how we could create such a thing in PHP which is equivalent to byte array in .NET Presently I'm using this,

   $byte_array = file_get_contents('C:\xampp\htdocs\my_site\RGB.jpg');
     $image = base64_encode($byte_array);

But unfortunately,this is not working... thanks in advance..

Vaisakh Pc
  • 714
  • 8
  • 21

1 Answers1

0
$string = file_get_contents('C:\xampp\htdocs\my_site\RGB.jpg');

# 011000010110001001100011

# https://stackoverflow.com/questions/6382738/convert-string-to-binary-then-back-again-using-php
$value = unpack('H*', $string);
$binary = str_pad(base_convert($value[1], 16, 2),strlen($string)*8,'0',STR_PAD_LEFT);

var_dump($binary);

$bytes = str_split($binary,8);

var_dump($bytes);

[1] Convert string to binary then back again using PHP

[2] http://www.php.net/str_pad

[3] http://www.php.net/str_split

Demo: http://sandbox.onlinephpfunctions.com/code/29a6666e63dbd0f85bd51db59e469e7f88c48825

Community
  • 1
  • 1
albertdiones
  • 735
  • 5
  • 10