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
What Do \R and \N Mean in PHP (And Other Languages)
Uncaught Error: Class 'Composerautoloaderinit' Not Found
The Ultimate Emoji Encoding Scheme
Is a Blob Converted Using the Current/Default Charset in MySQL
Extract Dom-Elements from String, in PHP
How to Upload Large File > 5Mb in Laravel 5
PHP Upload Progress in PHP 5.4 Is Not Working. Session Variables Not Set
Access an Array Returned by a Function
Using Pdo Prepared Statement and Incrementing a Column Value
PHP Pthreads: Fatal Error: Class 'Thread' Not Found
Regex for Dropping Http:// and Www. from Urls
How to Display PHP Code in HTML
Error: Class 'Facebook\Facebooksession' Not Found with the Facebook PHP Sdk
Laravel 5.1 Date_Format Validation Allow Two Formats
Symfony: How to Refresh the Authenticated User from the Database