PHP: Show JPG from Binary - php

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);

Related

Saving base64 image to server not working in PHP

Iam getting base64 image in a json like below format
{
"profilepic":"iVBORw0KGgoAAAANSUhEUgAAAPAAAABGCAYAAADyxhn6AAAMYml..."
}
I have a PHP code like below, where i decode this base64 image and save it in server, i tried to run the code and am not able to see the image in particular folder location.
Can anyone help me to identify the problem here?
<?php
header("Access-Control-Allow-Origin: *");
$str_json = file_get_contents('php://input'); //($_POST doesn't work here)
$response = json_decode($str_json, true); // decoding received JSON to array
$photo = $response["profilepic"];
// Obtain the original content (usually binary data)
$bin = base64_decode($photo);
// Load GD resource from binary data
$im = imageCreateFromString($bin);
// Make sure that the GD library was able to load the image
// This is important, because you should not miss corrupted or unsupported images
if (!$im) {
die('Base64 value is not a valid image');
}
// Specify the location where you want to save the image
$img_file = 'test/'.uniqid().'.jpg';
// Save the GD resource as PNG in the best possible quality (no compression)
// This will strip any metadata or invalid contents (including, the PHP backdoor)
// To block any possible exploits, consider increasing the compression level
imagejpeg($im, $img_file, 80);
$imgPath = 'http://serverip/'.$img_file;
?>
Please help me to identify the issue

Image to base64 encoding issue - PHP

<?php
header("Content-type: image/jpeg;charset=utf-8'");
$path = 'example/source.jpg';
$da = file_get_contents($path);
$base64 = base64_encode($da);
$src = 'data:image/jpeg;charset=utf-8;base64,'.$base64;
echo '<img src="'.$src.'">';
?>
php v5.6.2
I tired copying the $src value in debug and pasted in img src value. still its not showing up.
what did i missed here?.
thanks in advance
header("Content-type: image/jpeg;charset=utf-8");
here you say to the browser i will send you an jpeg image,
then:
echo '<img src="'.$src.'">';
here you send HTML.
because you said it was a jpeg image, the browser will try to render your html as jpeg. since the ascii text-based HTML format is completely incompatible with the binary based jpeg-format, the browser will fail horribly when trying to render your image, and fail with some error (probably image is corrupt or something like that.)
you can either fix your Content-Type header to specify that you're sending HTML, then the browser will (probably successfully!) try to render it as such, eg:
header("Content-type: text/html;charset=utf-8");
or you can modify your code to actually send the image as jpeg, eg:
<?php
header("Content-type: image/jpeg");
$path = 'example/source.jpg';
readfile($path);
(btw a base64 encoded jpeg image will be about 33% larger than just the raw jpeg image, so if you want a fast pageload, or you want to save up on bandwidth, or you want to save up on ram, using readfile() is faster, requires less bandwidth, and requires less ram, both for the server and the client, compared to your embedded base64 approach.)
So maybe your problem is in your mime type. then try this code two solve:
$path = 'domain.com/example/source.jpg';
$content = file_get_contents($path);
$file_info = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($path));
$base64 = base64_encode($content);
$src = 'data:'.$mime_type.';charset=utf-8;base64,'.$base64;
echo '<img src="'.$src.'">';
Note: its better to use path from full address domain, if you want to use from path use readfile()

PHP save PNG from base64 *rgba* string

I'm wondering if there is someone who can help me with this, i'm sending an image from my windows phone c# to php and i want to save the image.
The data connection works and i can receive the POST value
see: http://posttestserver.com/data/2014/01/23/19.41.40516059397 for the information the server receives.
i now want to save this base64 encoded RGBA string to a png or jpg (i would prefer png but i dont need the alpha value).
this is the code i have server side
//getimagecontents
$data = base64_decode(file_get_contents("php://input"));
//create an image from string
$im = imagecreatefromstring($data);
if ($im !== false) {
//add an header for the image
header('Content-Type: image/png');
//save the image
imagepng($im, 'upload/test.png');
imagedestroy($im);
echo "it worked!";
}
Hopefully someone can help me with this coz it's keeping me up all night :(
Thanks in advance :)
Make sure that you are receiving the data. If yes. then try once without base64_decode and upload directory is created in place that is where your image will be saved. If it is not created, you can not save image.
You should have received warnings in both cases indicating the problem. If this your dev environment, turn the warnings reporting on in php.ini or your script.
Another thing, why are you sending header for image when only you intend to save it to file system.
The image i was sending was too big for the server to handle...
As it was send as a RAW file...
converted it to a png and now works like a charm

Getting image from XML php

I am struggling with getting images from xml file
Let's say one image is stored in XML like this
<obrazok1>/9j/4AAQSkZJRgABAQAAAQABAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf ...</obrazok1>
My php code looks like this
dump($x->ponuka->obrazok1);
$data = base64_decode($x->ponuka->obrazok1);
dump($data);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
After this i get a typical icon of not known image ...
I think it might be a problem after using base64_decode.
It gives me:
Is it your dump? Try commenting your dumps
// dump($x->ponuka->obrazok1); // comment this
// dump($data); // comment this

PHP mySQL turning a string into a png is only working for the first image

So I am trying to get a bunch of photos to appear that are saved in a mysql database. With the code I am using, the page displays just the very first photo in the database with nothing else. There are 5 different photos and I don't know where they are going. If someone could help that would be great. My code is here:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
}
The headers will only display one image per page.
You could try:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
print '<img src="data:image/png;base64,'.$data.'" />';
}
That's untested, but something like that should work.
A better way would be to store the image file on the disk and store the location in the database.
You could either insert them into mysql as in blob type or simply upload the file to a folder and insert the file name into the database (I assume you just dont create random folders for each instance therefore you would know the destination path already)

Categories