PHP View Picture From Base64 String - php

I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.
I have stored the file in the DB using Base64 encoding which makes the result look like:
/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...
(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)
Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:
How do I achieve this through PHP - I have tried using:
<?php
$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver
$data = base64_decode($data);
$fileTmp = imagecreatefromstring($data);
$newImage = imagecreatefromjpeg($fileTmp);
if (!$newImage) {
echo("<img src=".$newImage."/>");
}
?>
I'm just getting a blank page!

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

Save Google Chart image to file

I'm using the Google Charts API to generate some charts and am using an ajax request to send that data to an endpoint that will save the image to disk for use in a PDF. However, I'm not sure how to get the image string into a PHP image resource.
It looks something like this (I'll snip a bunch of the code to save some room):
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAEsCAYAAABwoFP+AAAgAElEQVR4XuzdCZcDuXXe/XGcfU/sLI41Glv5/l9JGmskZ99XJ7H9nl/bf71QqUgWyeKOOqdPd5NVKODiAvfBXX/rz//8z//8m3lNCkwKTArsTAFby//7f//v6+c//If/8M3f+Bt/45v//b//9zd/+qd/+s3f//t//5vf+q3f+nrjn/3Zn33d8z/+x//45vd+7/e+Pv+bf/Nv7tyb2dykwKTApMCkwC0o8FsTSN6CrLPNSYHPpsD//b//9TJwxYw7z0NGWIDRtXeSQyxAOi1trkg3pCSmx2MUn75CgHbRTcKMlT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9JMHWbQuBQqAQeCEEiki+kDBrKK+FgBrbAm5oHnMggRKUSzye49OnT7MDl2OSBpKGU+WbMiXNQlYnFAKFQCFQCHQgUESyA6Q6pRB4FgLqbTNFC7iJRpJW0d9+0lR+/Pixu3sCdMZSCHU3UCcWAoVAIVAIFAINAkUkazoUAgdGgOZRmcQcSKD/SU4u6IZ5W37J8nc8sBCra4VAIVAIvDACRSRfWLg1tNdAgK/kv//97//WomXeRiSZvZm5aSY/fPjwGoOtURQChUAhUAicCoEikqcSV3X2igggi3wcmaV9HMijgBkJyQXcIJvv378vs/UVJ0iNuRAoBAqBJyJQRPKJ4NetC4GlCPzwww//vQSZRDJFX/sZM7eoblHaf/rTn5Y2X+cXAoVAIbAJAqpy9ea53eSG1cjTECgi+TTo68aFwHIE/vOf//zmX//61xtxTACOVqQF4j/p57t3795M4Xwn6ygECoFCoBAoBPZEoIjknuhW24XATggIuGHORiwRxt/97ndv+SHrKAQKgUKgECgEHolAEclHol33KgQ2RoBWkp8kc3YdhUAhUAgUAoXAoxH4P1ANKGa/fh2tAAAAAElFTkSuQmCC"}';
$decoded = json_decode($data, true);
$resource = imagecreatefromstring($decoded['image_data']);
var_dump($resource);
This throws a warning at the imagecreatefromstring step: Warning: imagecreatefromstring(): Data is not in a recognized format.
What's the right handler for that image format?
This is because your need to get rid of leading data:image/png;base64,
Also your base64 string looks corrupted . I did the following
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAQhUlEQVR4Xu2dTWxcVxWAz3kzEwKqiCtoG0eCWiCx6IK6Euwq6u5aqB0HCVGJnzoggWji1lmAxApbLGCBVAc7QnQBjlS1YlXHToVAIBwJEAsEk+5QF7ggOa666ARQ0mRm3kH3zZvxeDJjz3jsmTn2N1IWyby577zvnHy697777lPhAwEIQMAJAXUSJ2FCAAIQEIRFEUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEE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ESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAv8H5S4HFJE4mRUAAAAASUVORK5CYII="}';
$decoded = json_decode($data, true);
$final = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $decoded['image_data']));
$im = imagecreatefromstring($final);
if ($im !== false) {
ob_clean();
header('Content-type: image/png');
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
}
?>
working fiddle
http://phpfiddle.org/main/code/7u1t-yfbe

Imagecreatefromwebp(): WebP decode: fail to decode input data

I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.
Here's my code
$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';
$im = imagecreatefromwebp($filename);
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
Please help.
i am using this code, it works fine for me. Here $data contains the base64encoded data
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php

How to get file information before saving it

I have file contents which I am receiving from my form in string form (base64), and now before using file_put_contents() function to save the file, I wanna know the information related to it, like it's size and mimetypes and so on.
So the question is how to retrieve a file's information just by it's data(base 64 string)
For ex, let's say I have this string:-
Object {files: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvwA…Fb+kScAm4BFwC6yGB/w9uNEwUj3EmdQAAAABJRU5ErkJggg==", text: "hi"}files: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvwAAACQCAYAAAB08b+HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7H0HeF7FsbZTbirNYNy7LTfZsiVZbrJkWc2ybLlLslzk3nvv4ALGYNN7h9ANofcOSSCETgiBJJCem0rKTSO5987/vjM755zvswy2gf/eG/Q9zz7nfHt29+zO2Z19Z3Z2tsmkqbOkbsZ8mTl/qUydtUAm18/D/3kydeYCqZ+3RKbg2YJla2TuopUyZ+EKmbt4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_data = 'iVBORw0KGgoAAAANSUhEUgAAAvwA…Fb+kScAm4BFwC6yGB/w9uNEwUj3EmdQAAAABJRU ....';
$size = strlen(base64_decode($encoded_data));
remember base64 encoding increases the original data in size by about 33%
Now we can write a function -
function getFileSize($base64string){
$bytes = strlen(base64_decode($base64string));
$roughsize = (((int)$bytes) / 1024.0)* 0.67;
return round($roughsize,2);
}
this function will return filesize in Kb.
Now to know the Mime Type you can use Fileinfo
$imgdata = base64_decode($encoded_data);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);

Laravel 4 - unserialize(): Error at offset while save base64 image

I'm trying to upload base64 image to server.
I've already try many answers, but i always get the same errors.
Links that i tried
How to save a PNG image server-side, from a base64 data string
https://www.reddit.com/r/laravel/comments/39wclp/how_to_convert_an_base64_image_string_to_an_image/
Convert Base64 string to an image file?
This code below should work.
$image = base64_decode(Input::get('image_cropped'));
$image_name= 'file_name.png';
$path = public_path() . "/images/" . $image_name;
file_put_contents($path, $image);
If you inspect the form sent from request the image encoded starts with data:image/png;base64, so try with:
$img = explode(',', Input::get('image_cropped'));
$img_name = 'file_name.png';
file_put_contents(public_path().'/images/'.$img_name, base64_decode($img[1]));
This worked for me.

how to create a base64encoded string from image resource

I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.
Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.
Taken from http://www.php.net/manual/en/book.image.php#93393
$image = imagecreatefromstring($file);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_clean();
echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
imagedestroy($image);

Categories