Convert image to byte using php

I need to send an image to a web service. The web service should receive the image as bytes (maybe bytearray) - not as a string ... How to convert images to "byte" or bytearray?

I tried this (without success):

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

Any help would be appreciated ...

+6
source share
3 answers

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

 <?php $filename = "image.png"; $file = fopen($filename, "rb"); $contents = fread($file, filesize($filename)); fclose($file); ?> 
+6
source

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)); 
+1
source

php string is binary, so it is already in bytes.

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

0
source

Source: https://habr.com/ru/post/916267/


All Articles