PHP Binary Image Data - php

I'm working with some binary image data given to me by another developer and trying to figure out how to display the images on the screen. The binary images all look like this:
0xFFD8FFE000104A46494...[TRUNCATED]...F6145145690D8E4ACFDE3FFFD9
I've tried the following PHP code to get the image to save:
$data = '0xFFD8FFE000104A46494...F6145145690D8E4ACFDE3FFFD9'; //full data entered here
$string = pack('H*', $data);
$result = imagecreatefromstring($string);
imagejpeg($result, 'test.jpg');
This saves an image called test.jpg, but it is a jumbled image and not the actual image it should be.
I also tried using base64 like this:
<img src="data:image/jpeg;base64,<?php echo base64_encode($data) ?>">
which also did not work. Any suggestions would be appreciated, thanks!

Related

How to solve binary image display problem PHP?

I have a image in binary format stored in my database (SQL Server). I export these images by a backend procedure in .PNG format. The image exported successfully but when I am displaying the image in my webpage, it does not work. But if I open the image in Paint and save as .PNG format again, it works. Thank you in advance for your cooperation. Here is my code to display:
$image = 'D:\\myImage.PNG';
$imageData = base64_encode(file_get_contents($image));
echo '<img src="data:image/PNG;base64,'.$imageData.'">';

Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension

I want to Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension (like imageMagick). I searched everywhere, but I couldn't find a perfect solution. I tried below code (got from link)
function get_dpi($filename){
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,0,4);
return array(hexdec($x),hexdec($y));
}
But I am not getting the correct Horizontal and vertical DPI. For example, I used an image with 96dpi and 96dpi, but I got (100,100).And this function is only for JPEG file formats.
The DPI of an image is usually a matter of fiction. Rarely is an image created where the physical dimensions of the final rendering actually matter (as far as the image itself is concerned). That said, the DPI information is stored in the EXIF data of a JPEG so you can read it from there with the built-in PHP function:
<?php
$filename = "/Users/quentin/Dropbox/Camera Uploads/2016-03-30 21.01.09.jpg";
$exif = exif_read_data($filename);
?>
DPI is <?php echo $exif["XResolution"] ?> by <?php echo $exif["YResolution"] ?>

PHP: Show JPG from Binary

I have a jpg blob thats been stored in an external DB and I'm looking to display that as an image via php. The issue is whenever I set the Content-Type to image/jpeg and echo out the blob I get the broken image icon when browsing to it.
I have tried making the file from scratch via sublime and that works when I save it as a hexadecimal file so I know the data is valid.
I have tried making the script create a file but it sets the charset=us-ascii so it won't get seen as a image file.
Does anyone have any experience with raw image binary files? anyone know how I can display the image or even save it out to a file?
Thanks in advance.
P.S. I would provide the binary but its just too big to put on here.
EDIT: (added some code)
<?php
header('Content-Type: image/jpeg;');
$data = 'some long string of hex';
// tried echoing it directly..
echo $data;
// and writing to a file...
file_put_contents('test.jpg', $data);
?>
After continuing research I found this post PHP: create file from an HEX string
With the following code I fixed the issue.
<?php
header('Content-Type: image/jpeg;');
$data = 'The hex data';
$data = pack('H*',$data);
$im = imagecreatefromstring($data);
imagejpeg($im);
?>
Try $im = imagecreatefromstring($data); The output it by imagejpeg($im);

PHP data URI to image file error - Windows cannot preview

I tried converting a data URI generated using
canvas.Canvas.toDataURL();
I am just trying to store the data URI as image using the following code
<?php $data = $_POST['image_designed'];
echo $data; //Data URI
echo '<img src="'.$data.'"/>'; //Getting the image perfectly
//removing the "data:image/png;base64," part
$uri = substr($data,strpos($data,",")+1);
header('Content-type: image/png');
echo base64_decode($uri);exit; //Just image place holder displaying instead of full image
I am getting the following data
$data = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAEsCAYAAADq/K67AAAgAElEQVR4Xky9WY8l6Xnn90acPffMqspauqqrit3sZrO5z2g0FATJIw0G8L0/gr
//Very longer data than this which cannot be pasted here
The final image result is wrong. Just displaying an error image placeholder. I guessed the reason is "I found long space in the middle of my $data". But it renders the image well in src tag.

data url image save as png

Lets say I have this data url from Apple:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6%2BR8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH%2BIO2BOJX0XwA2%2BQEL4pAhSX4aBQAAAABJRU5ErkJggg%3D%3D
That I want to save as a png file.How do I do That?
Rightclicking and 'Save image as' is not working(Why?)
So I thought I should use a PHP script with the 'base64_decode' function:
$img = "iVBORw0KGgoAAAANSUhEUgAAA9QAAAAkCAMAAABfcIIyAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHVQTFRFAAAA%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F5YtmQAAAACd0Uk5TAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmZiD6WAAAAdlJREFUeF7t3btuFEEQheFzunotGYkIERA4spn3fyeHiAgEEtsXB82s2eEJavR%2F0Wjyo75XWfHpw4MkSXNqCkA2lr2%2B%2Fvz63n15qpI0x5yTSAM52XaxJLVXf%2FkoaY6xQk2sgXy8Ql2KJf3wS5HG6HMMQg3kZNkuxVGKNLxJo48%2BxhxTpBrIx7JcXEqUKJI3zd77CjWLaiAle4U6Iixv6q33MfocTL%2BBlCy7OEqJqCFvs%2FV2m3%2BTaiAd6zb7rlHtrbc1VA%2Bm30BSa%2B%2B7RNSo4a21thbVTL%2BBnCy7rCV1rdXbtbXW%2B%2Bh%2Ft78BZLM2v6NE1Fov%2FrpC3Zl%2BA2l5H6lrrRe%2FXNu%2BqCbUQE57qGvsoW5t9D4H028gJculOKLUWuvFz%2B16O9NipAZSsm8nWpfq53ZlTQ3k9s%2BamlADZ0CogZMh1MDJEGrgZAg1cDL%2Fh5ojLSC1%2ByMtLp8A2R0un3BNFEjv%2FpooDzqA9O4fdPD0Esju8PSSIglAdsciCZQzArI7lDOat30yCg8CGe2FB1eovZcI7oMSwUBSq0TwWlQHxfyBEzgW86ftDpDase0ODfKA7A4N8mhlC2R318r2px%2BeQqLpPJDae9P5%2FmrVz491%2FWekBlKy7PXVfn9rb9xXsMD2wdOcAAAAAElFTkSuQmCC";
header("Content-type: image/png");
echo base64_decode($img);
That is giving me back the broken image icon :
How should I solve this problem and save a data url image to a file?
Your code is fine, but your base64 is urlencoded. I urldecoded it for you: iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH+IO2BOJX0XwA2+QEL4pAhSX4aBQAAAABJRU5ErkJggg==
or do it yourself:
base64_decode(urldecode($img));

Categories