7

I have to send a picture to a webservice. The web service should receive the image as bytes (mayby bytearray) - not as a string... How do I convert the images to "byte" or bytearray?

I have tried this (without succes):

$image1 = file_get_contents("LINK TO IMAGE");
$image1BinaryData = "".base64_encode($image1)."";

Any help will be appreciated...

fletcher
  • 85
  • 1
  • 2
  • 5

3 Answers3

9

Have you tried to directly read the image as binary data?

<?php
$filename = "image.png";
$file = fopen($filename, "rb");
$contents = fread($file, filesize($filename));
fclose($file);
?>
sucotronic
  • 1,504
  • 9
  • 20
3

This is the actual byte array equivalent to what is generated in C# and Java.

$data = file_get_contents("test.jpg");

$array = array(); 
foreach(str_split($data) as $char){ 
    array_push($array, ord($char)); 
}
var_dump(implode(' ', $array));
Arvind Bhardwaj
  • 5,231
  • 5
  • 35
  • 49
0

a php string is binary, so it's already in bytes.

get rid of base64_encode() and use urlencode() or rawurlencode()

goat
  • 31,486
  • 7
  • 73
  • 96