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 Have Image String Like
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAE7CAYAAADXQrC8AAANvUlEQVR4nO3dLXoqWRuGUebDGBgBA0BjsZFxuDhcVFQMKiYmBoPBIDCImAgEAoGoCexPpM9PkirgSYo06W+JZbr7UPu8u0XdV8GuTlVVBQAAgNN1/u0FAAAA/DRCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAA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FAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQ+h9OaCdW3kXmdwAAAABJRU5ErkJggg==';
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // Decode image using base64_decode
$file = uniqid() . '.jpg';
//Now you can put this image data to your desired file using file_put_contents function like below:
$success = file_put_contents($file, $data);
I wanted To convert Into Image and Upload To the Server
The Upload Folder Like
www.abc.com/uploads
And My File is
Www.abc.com/webservices/index.php (In this file I will write the code)
Thanks In advance
One way of doing it,
function writePng($data, $filename){
$bin = substr($data, strlen('data:image/png;base64,'));
$bin=base64_decode($bin);
$fp = fopen($filename,'w+');
fwrite($fp, $bin);
fclose($fp);
}
writePng($data, '1.png');
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 :)
hi guys ive created a base64 encoded image captured with web cam now i convert the .png to .jpg all works fine but now i get two images on server both .png and .jpg how do i go about deleting the .png or is their a way to convert to jpg without saving .png image to disk thanx here my code
$rawData = $_POST['imgBase64'];
$filteredData = explode(',', $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(1000, 99999999999);
//Create the image
$fp = fopen('user/'.$randomName.'.png', 'w');
fwrite($fp, $unencoded);
//convert image from png to jpg
$image = imagecreatefrompng('user/'.$randomName.'.png');
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
unlink($fp);
ive tried it with
unlink($image);
unlink($_SERVER['DOCUMENT_ROOT'] . "/user/.$randomName.'.png'");
imagedestroy($fp);
imagedestroy($image);
Use the function unlink() but passing the file name to it instead of the file handler.
So from your example it would be:
EDIT: You might need to close the file first:
fclose( $fp );
unlink( 'user/'.$randomName.'.png' );
as far as i understand all you need is:
$data = base64_decode( $_POST['imgBase64']);
// image resource from your string
$image = imagecreatefromstring($data);
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
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/Q7P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xYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/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/DRCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAA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+/hsWk6YW09TGwaQyvhj1rc/+PzKxJ76+QanOOn1s/AHWEFECg+Ua04Tjt3bKMGm5475a70z578Ov3LrtyP6w/XKA/efsOp+3zvNyOR6V37IZ9cF1m62Mnx/3t31/DvDGkRmW5T/asX542Z97/f2Y2HTUcCtHgaro+yxw/t34A6ggpgP+0fdk8r8ti9lQeHx7KdDotD49PZbZYlpdtfvz3z13Df4E5AlwSIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQ+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+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==';
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.