Convert image to byte with php

爱⌒轻易说出口 提交于 2019-12-10 13:22:46

问题


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...


回答1:


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);
?>



回答2:


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));



回答3:


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

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



来源:https://stackoverflow.com/questions/10687270/convert-image-to-byte-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!