I know there are so many solutions for decode base64 image. But any of them didn't work for me.
I have the Base64 image data and i need to convert it to a png file and save it to the local directory.
I tried the following code which i got from here. But it didnt work for me. Please help me to get this done..
define('UPLOAD_DIR', 'images/');
$base64string = "data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBB...";
$img = $base64string;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . 'txtimg.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
When i execute the script its getting the error message Unable to save. Then i gave full permission for the users. But still no luck.
I tried to save a simple text file through file_put_contents() ; it worked.
I'm using IIS 8 on Windows Azure Server.
I am sending base64 encoded image to PHP from my android app. Sometime it stores full image (4KB) and sometime (3KB) (Same Image). when I use URL in picasso, image with 4KB size works fine but image with 3KB size does not load it shows decode error.
This is my PHP code (which sometime works)
$encodedImage = str_replace(' ','+',$_POST['encodedProfileImage']);
$data = base64_decode($encodedImage);
$file = 'Pics/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
$BASE_URL = 'http://domain.com/TestApp/';
I then do SQL operation in PHP to store image path. Is there any chance that next code operation is done on half decoded image(which is corrupt).
You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.
Use below function:-
function base64_to_png($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
If you want to use str_replace function then may be below way work. I am not sure :)
$fname = filter_input(INPUT_POST, "name");
$encodedImage = filter_input(INPUT_POST, "image");
$encodedImage = str_replace('data:image/png;base64,', '', $encodedImage);
$encodedImage = str_replace(' ', '+', $encodedImage);
$encodedImage = base64_decode($encodedImage);
file_put_contents($fname, $encodedImage);
print "Image has been saved!";
Hope it will help you :)
I'm trying to get an image from canvas and save it with my php script. But when the script proceedes, I got a simple black rectangle instead of my canvas image (web-cam snapshot).
Here is my code:
$img = $base64Img;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = "photo/" . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
What is the purpose of this line:
$img = str_replace(' ', '+', $img);
Don't think it is necessary.
Otherwise all seems fine.
I usually use php explode to isolate the data:
$exploded = explode(',', $img);
$base64 = $exploded[1];
$data = base64_decode($base64);
But str_replace should also do the job.
Maybe the error is in the code that loads the image?
UPDATE
Purpose of that line is to encode the white space in the base64 data.
This is also mentioned in this comment on php.net.
Probably it would be better to use the PHP function urlencode in such cases.
$data = base64_decode($data);
This line is probably causing the issues in this, since the data is not used as a data url but saved to a file directly.
When i post a base64 stream to a php page using post, i never received the totality of the stream. I increase the max post size, but it doesn't affect anything.
Have you already seen a similar problem ?
Here is the server code :
$value = $_POST['file'];
echo "-------";
echo $value;
echo "-------";
$img = $value;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
I'm not PHP developer, you will probably find some bad practices.
Thanks in advance for any kind of help / suggestions.
If you have suhosin check for the suhosin.post.max_value_length
I am trying to take a base64 encoded string and return it as an image in php using $_POST. On line one if I use $_POST['imgdata'] it returns error from the preg_match if i were hard code the base64 string instead of using $_POST it all works and returns the image. how can i make this work by using the $_POST
works
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAoCAYAAAC7HLUcAAADtUlEQVR4Xu2aLZYqMRCFMxuAFaARg8WwAnaABQEWgwUBFoMFARaDRqAxWBBoVgAbmPcu7/Q7PX3SP0k16TTcnDNqOpXKrXykKsmXUurn7x+bvQJf9l3Z03cFEFwCIosSAZHp53VvAiIPDwGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBTEg7XZb1Wo11Wg0VKVS+T/Rx+Ohzuezut1uar/feyVAp9NR8Dvaer2ejZ8ExEa1kvSxBgQLbDQaPeFIa4BkPp97AQr8XiwWWpfr9XraVHT/JyA2qpWkjxUg+AWeTqfGU5xMJmq73Rr3y6vD9/e32mw2v3a6sG0CkpfS72PHGBAsst1uZ60A0pjj8Wjd37Yj0j/AAf/jGgGxVfd9+xkDgp0DO0i0oeZYLpfqcrk8F+FgMND+UqMeGQ6HzhVdr9eq1WoljktAnIfF+wGNATmdTtqFH02fsBixKHUgNZtNp8LEQR11goA4DUspBjMCJC69wu6hW/TX6zXPYthKUF29BH/DJ26BYQJiJfFbdzICJFAinKoAGiy4aPGN063D4VDoDqIDGr7iRE13yEBA3nqtW03OCpAsI+EoVXfX4KoGAaA4TIjezXS7XVWtVrXpHwHJEtnP+uYlgCTl/C5OseJOrII6Ka4+IiCftfizzDZXQLAwcXmoO+WCM652D93uhbRqtVo9NSEgWZYGv4ECuQGClAYLM+6eAce/SG9QA7yy9fv9J6ThhvoIu0e4htKdsHEHeWVkymk7F0BQa8xms9gbalwM4u7j1XDoTqx0YHIHKediLcJrMSC6X+zwRJDWIL1x0XR3NBj/fr//Gh67nS4NDPzUncol+M+3WC6CW9AYIkCSinEssvF47PSBYty9i6m22PEMXvYSEFOBS/S9NSBJcLhKqaI6E5ASrbySuGoFSFJaFS2IXepAQFyq/RljGQOS9po3y0td5PoonvNuBCRvRWnPGJAsr2LTZH3VZWHaa93AL0AePQrG/4K6A0W9AcCsQdICXuL/GwES977KdP6vAiSrHzzmzaoUvzMCJO1IN6ucBCSrUvyuaAWMAIl7gGg6CQJiqhi/L0oBI0CKctLzcVmDeB4giXsERKLev74ERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/gDZOWY4lzJl1QAAAABJRU5ErkJggg==';
does not work
$imgstr = $_POST['imgdata'];
full code
$imgstr = $_POST['imgdata'];
// Grab the MIME type and the data with a regex for convenience
if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) {
die("error");
}
// Decode the data
$content = base64_decode($matches[2]);
// Output the correct HTTP headers (may add more if you require them)
header('Content-Type: '.$matches[1]);
header('Content-Length: '.strlen($content));
// Output the actual image data
echo $content;
As was noted the "+" should not be missed, the rest is straight forward. Use $_REQUEST if you are not sure is it post or get.
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_REQUEST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
This is the One., Would you like the following i think.,
<?php
$imgstr = 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAoCAYAAAC7HLUcAAADtUlEQVR4Xu2aLZYqMRCFMxuAFaARg8WwAnaABQEWgwUBFoMFARaDRqAxWBBoVgAbmPcu7/Q7PX3SP0k16TTcnDNqOpXKrXykKsmXUurn7x+bvQJf9l3Z03cFEFwCIosSAZHp53VvAiIPDwGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBTEg7XZb1Wo11Wg0VKVS+T/Rx+Ohzuezut1uar/feyVAp9NR8Dvaer2ejZ8ExEa1kvSxBgQLbDQaPeFIa4BkPp97AQr8XiwWWpfr9XraVHT/JyA2qpWkjxUg+AWeTqfGU5xMJmq73Rr3y6vD9/e32mw2v3a6sG0CkpfS72PHGBAsst1uZ60A0pjj8Wjd37Yj0j/AAf/jGgGxVfd9+xkDgp0DO0i0oeZYLpfqcrk8F+FgMND+UqMeGQ6HzhVdr9eq1WoljktAnIfF+wGNATmdTtqFH02fsBixKHUgNZtNp8LEQR11goA4DUspBjMCJC69wu6hW/TX6zXPYthKUF29BH/DJ26BYQJiJfFbdzICJFAinKoAGiy4aPGN063D4VDoDqIDGr7iRE13yEBA3nqtW03OCpAsI+EoVXfX4KoGAaA4TIjezXS7XVWtVrXpHwHJEtnP+uYlgCTl/C5OseJOrII6Ka4+IiCftfizzDZXQLAwcXmoO+WCM652D93uhbRqtVo9NSEgWZYGv4ECuQGClAYLM+6eAce/SG9QA7yy9fv9J6ThhvoIu0e4htKdsHEHeWVkymk7F0BQa8xms9gbalwM4u7j1XDoTqx0YHIHKediLcJrMSC6X+zwRJDWIL1x0XR3NBj/fr//Gh67nS4NDPzUncol+M+3WC6CW9AYIkCSinEssvF47PSBYty9i6m22PEMXvYSEFOBS/S9NSBJcLhKqaI6E5ASrbySuGoFSFJaFS2IXepAQFyq/RljGQOS9po3y0td5PoonvNuBCRvRWnPGJAsr2LTZH3VZWHaa93AL0AePQrG/4K6A0W9AcCsQdICXuL/GwES977KdP6vAiSrHzzmzaoUvzMCJO1IN6ucBCSrUvyuaAWMAIl7gGg6CQJiqhi/L0oBI0CKctLzcVmDeB4giXsERKLev74ERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/gDZOWY4lzJl1QAAAABJRU5ErkJggg==';
$new_data=explode(";",$imgstr);
$type=$new_data[0];
$data=explode(",",$new_data[1]);
header("Content-type:".$type);
echo base64_decode($data[1]);
?>
The above code will generates the output as per your wish.,Try this..This may help you.
Sorry For The Delay Reply.,Now Only I Recover My StackOverFlow Account After a long time.
Note : Ignore the empty space's in before the starting '' tag on the PHP file you are using for image decode.,If any empty is on that file means it throws Error message and it doesn't create or show the original image after the decodes.
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
source: documentation.
check out imagecreatefromstring.
Not sure why the regex isn't working for you, I copied the base64 post data and your code and it worked fine. You can try this instead which doesn't use regex and may be a little faster and use less memory.
$imgstr = $_GET['imgdata'];
list($type, $imgstr) = explode(';', $imgstr);
list(, $type) = explode(':', $type);
list(, $imgstr) = explode(',', $imgstr);
$content = base64_decode($imgstr);
Please check below code it is working fine for me,
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAE7CAYAAADXQrC8AAANvUlEQVR4nO3dLXoqWRuGUebDGBgBA0BjsZFxuDhcVFQMKiYmBoPBIDCImAgEAoGoCexPpM9PkirgSYo06W+JZbr7UPu8u0XdV8GuTlVVBQAAgNN1/u0FAAAA/DRCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAIC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BJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQ+h9OaCdW3kXmdwAAAABJRU5ErkJggg==';
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // Decode image using base64_decode
$file = uniqid() . '.png'; //Now you can put this image data to your desired file using file_put_contents function like below:
$success = file_put_contents($file, $data);
You can capture parts by this regex :
^data:([a-zA-Z]+/[a-zA-Z]+);base64\,([a-zA-Z0-9+\=/]+)$
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAoCAYAAAC7HLUcAAADtUlEQVR4Xu2aLZYqMRCFMxuAFaARg8WwAnaABQEWgwUBFoMFARaDRqAxWBBoVgAbmPcu7/Q7PX3SP0k16TTcnDNqOpXKrXykKsmXUurn7x+bvQJf9l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Dw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/gDZOWY4lzJl1QAAAABJRU5ErkJggg==';
preg_match("/data\:image\/(.*)\;base64/",$imgstr, $match);
echo $match[1];
^^^^ Return file extension.
$data = str_replace('data:image/png;base64,', '', $imgstr);
data = str_replace(' ', '+', $data);
$data = base64_decode($data);
file_put_contents("/files/youfilemane.".$match[1],$data);
^^^^ Create file from base64.