当我们使用Web服务时,我们需要传递一个参数作为base64二进制文件,但我必须将图像转换为字节数组。我使用了base64_encode,但它没有用。
该Web服务是基于.NET构建的,那个参数被视为字节数组..所以我想知道how we could create such a thing in PHP which is equivalent to byte array in .NET
现在我正在使用它,
$byte_array = file_get_contents('C:\xampp\htdocs\my_site\RGB.jpg');
$image = base64_encode($byte_array);
但不幸的是,这不起作用...... 提前谢谢..
答案 0 :(得分: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
演示: http://sandbox.onlinephpfunctions.com/code/29a6666e63dbd0f85bd51db59e469e7f88c48825