PHP GD: How to get imagedata as binary string?
One way is to tell GD to output the image, then use PHP buffering to capture it to a string:
$imagedata = imagecreatefrompng($imagefile);
ob_start();
imagepng($imagedata);
$stringdata = ob_get_contents(); // read from buffer
ob_end_clean(); // delete buffer
$zdata = gzdeflate($stringdata);
Have GD get image from binary string
imagecreatefromstring() should do the trick. I think the function example in the manual is almost exactly what you need:
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
Where $data
is your binary data string from the database. Use PHP / GD to Save Binary Data as a JPEG Without Losing Metadata
Why don't you just save $binary_data
instead of passing it through GDfile_put_contents($directory, $binary_data)
How to convert binary image data to image file and save it in folder in php
Try the imagecreatefromstring
method, which is documented here.
Get image mimetype from resource in PHP/GD?
I know this is pretty old, but just in case someone come across this post like I did...
A better option it's been released from PHP 5.4.0:
getimagesizefromstring
This new function is exactly the same of getimagesize but allows you to retreive the information from a stream.
convert binary data to image using php
Given the string displayed as text (extactly this sequence), it's a lineup of hexadecimal numbers.
In Jpeg, the magic numbers of such a file are FF D8
as the first two bytes and FF D9
as the last two bytes (compare with How to identify contents of a byte[] is a jpeg?).
These hexadecimal (hex in short) numbers can be found at the beginning and end of your sequence as well:
FF00D800FF00 ... 1F00FF00D9000
## ## ## ##
Looking for these magic numbers also reveals that the hex values are append with 00
always. It also shows that at the very end an extra single 0
is found.So four characters always form one value. Hex-encoded this looks like 16 bit values however the value never goes over the 8 bit range (0-255), therefore there is always 00
visible.
With this information alone one then can try to turn this into a binary string that PHP's imagecreatefromstring
can deal with:
$string = implode('', // map array of binary strings to binary string
array_map('chr', // map ord integer value to character
unpack('v*', // unsigned short (always 16 bit, little endian byte order)
pack("H*", $data) // hex to binary (high nibble first)
)));
Using such a string then in $result = imagecreatefromstring($string);
imagejpeg($result, 'test.jpg');
reveals the following PHP error notice:and the following image:imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: bad Huffman code
This looks desperately broken. So you are probably facing an additional encoding problem here. The last NUL
byte suggests that more has been done and there also must be a reason why the data has been stored as 16 bit hex values instead of just binary data (blob) as databases have support for that.
But don't waste too much time, because of the flaw in the software and configuration that was used in the past, this might just be data-loss so all you can do is to restore from backups.
PHP View Picture From Base64 String
Your problem is that imagecreatefromstring()
doesn't return a file, but rather an image in memory that should be output with the correct headers.
$data = base64_decode($data);
// Create image resource from your data string
$imgdata = imagecreatefromstring($data);
if ($imgdata) {
// Send JPEG headers
header("Content-type: image/jpeg");
// Output the image data
imagejpeg($imgdata);
// Clean up the resource
imagedestroy($imgdata);
exit();
}
Related Topics
Pdo_Sqlite Driver Not Present.. What to Do
Replace Any Url's Within a String of Text, to Clickable Links with PHP
PHP Character Encoding Problems
Login Event Handling in Laravel 5
In a PHP5 Class, When Does a Private Constructor Get Called
How to Pass an Array of Checked/Unchecked Checkbox Values to PHP Email Generator
What's the Difference Between Post and Raw Post in PHP at All
How to Implement Ws-Security 1.1 in PHP5
(When) Should I Use Type Hinting in PHP
Php, How to Get Current Date in Certain Format
Remove Namespace from Xml Using PHP
Does Die() End Your Session in PHP
Zend Framework - Multiplate Navigation Blocks