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.
Related
I'm trying to decode a base64 string and write it to a file:
<?php
// requires php5
define('UPLOAD_DIR', '/images/');
$img = $_POST['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.';
?>
But the server gives this error message:
403 Forbidden
You don't have permission to access testupload.php on this server
I have set all permissons for the php file and the images directory, so I don't understand why I get this error.
When I put the decoded string of image directly inside the $img variable, it works and it creates the file under the images folder... But when I try to post the decoded string it doesn't work.
How can I solve this?
NOTE :
when we try to send decoded string of image it may contain some virus or something like that from hackers ... i contact with my host providers and they say if you want we can turn of the modsecurity but its really unsafe way to do ... so what i have to do with this situation?
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.
I'm trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..
I got this code:
define('UPLOAD_DIR', '../image/');
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$uniqueNumber = uniqid();
$namefile = $uniqueNumber.'.png';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
$namefile = $uniqueNumber.'.pdf';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!
EDIT:
I'm trying this code and getting the same issue.
$data = base64_decode ($img);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$data);
//close output file
fclose ($pdf);
echo 'Done';
Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.
EDIT 2:
FOUND A SOLUTION.
http://www.fpdf.org/en/script/script45.php
I followed these steps and i can get that, thank you all!
Check out DOMPDF: https://github.com/dompdf/dompdf
You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.
<?php
require_once("dompdf_config.inc.php");
$img = $_POST['image'];
$html =
'<html><body>'.
'<img src="'.$img.
'"></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
I am working on the project which includes uploading image and video of user from iphone device. I am handling the backend with PHP. For uploading images I am getting the base64encode string and I have use following code to upload the image -
$str = $data['pictureurl']; // base64encode string
$newVar = base64_decode($str);
define('UPLOAD_DIR', 'userimages/');
$img = $str;
$date = strtotime(date("m-d-y H:i:s"));
$img = str_replace('data1:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data1 = base64_decode($img);
$file = UPLOAD_DIR . strtolower($data['firstName']).strtolower($data['lastName']). "_".$userId . '.png';
$totalpath = $path.$file;
#chmod('userimages/', 0777);
$success = file_put_contents($file, $data1);
From this code, image gets uploaded to the server in specified directory properly with .png extension. May I use the same code for uploading video making the file type .flv or .mov? I have tried it, but file gets placed in the folder with 0 bytes of data. What could be the problem? or is there any another solution to upload such file ?