Converting a Byte Array into an Image Using PHP and HTML

Convert binary byte array to image in PHP

If all you want to do is show the image to the user, you simply need to serve the right content header with the response:

if      (substr($string, 0, 4) == "\x89PNG")  header('Content-Type: image/png');
else if (substr($string, 0, 2) == "\xFF\xD8") header('Content-Type: image/jpeg');
else if (substr($string, 0, 4) == "GIF8") header('Content-Type: image/gif');

echo $string;

An image is really just a blob of data (like the 'string' you have) plus an indication that it's an image (in HTTP, that would be the MIME header). Since you already have the blob, all you need is the header to make the browser interpret it as an image.

How to convert byte array to image in php?

Use pack to convert data into binary string, es:

$data = implode('', array_map(function($e) {
return pack("C*", $e);
}, $MemberImage));

// header here
// ...

// body
echo $data;

How to send byte array with image from AS3 to PHP?

import com.sociodox.utils.Base64;
.....
//BA1 is ByteArray with an image encoded
var enc_image=Base64.encode(BA1);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
var request:URLRequest = new URLRequest("some.php");
var variables:URLVariables = new URLVariables();
variables.decode("image="+enc_image);
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);

of course, set your listeners, too...

in "some.php":

$imageData = base64_decode(str_replace(" ", "+", $_POST['image']));
$fh = fopen("path/to/image/somename.jpg", "wb");
fwrite($fh, $imageData);
fclose($fh);

This works like a charm :)



Related Topics



Leave a reply



Submit