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.
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);
How to convert binary image data to image file and save it in folder in php
Try the imagecreatefromstring
method, which is documented here.
Convert GD image back to binary data
Use imagejpeg, imagepng, or similar. Use output buffering if you want to dump the result to a string, rather than a file:
ob_start();
imagejpeg($im);
$image_string = ob_get_contents();
ob_end_flush();
How to achieve the reverse effect of imagecreatefromstring in PHP?
There's no way to tell GD to return your image as a binary string, unfortunately. GD only supports writing to a file or to the screen. What we can do though is use output buffering to capture its output and then put it in a string.
ob_start();
imagepng($img);
$image = ob_get_clean();
echo 'data:image/png;base64,'.base64_encode($image);
PHP Get Image size when image is stored as byte array
You can use imagesx
and imagesy
on gd resources.
PHP : binary image data, checking the image type
The bits start with:
$JPEG = "\xFF\xD8\xFF"
$GIF = "GIF"
$PNG = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
$BMP = "BM"
$PSD = "8BPS"
$SWF = "FWS"
The other ones I wouldn't know right now, but the big 3 (jpeg,gif,png) usually cover 99%. So, compare the first bytes to those string, and you have your answer.
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)
Related Topics
PHP 7: Missing Vcruntime140.Dll
Symfony2 Collection of Entities - How to Add/Remove Association with Existing Entities
Dyld: Library Not Loaded: /Usr/Local/Lib/Libjpeg.8.Dylib - Homebrew PHP
How to Send JSON Response in Symfony2 Controller
JSON_Encode/JSON_Decode - Returns Stdclass Instead of Array in PHP
Youtube API V3: How to Find a List of Each 'Videocategoryid'
Oracle 11G Xe Greek Character Set Not Displaying
JSON_Encode or Remove Last Comma
PHP SQL Select Where Like Search Item with Multiple Words
Try/Catch Block in PHP Not Catching Exception
Header("Location") Causes [500] Internal Server Error
Learning Select from Where Prepared Statements
Send Email with a Template Using PHP
What Are the Differences of System(), Exec() and Shell_Exec() in PHP