How to save a PNG image server-side, from a base64 data URI
You need to extract the base64 image data from that string, decode it and then you can save it to disk, you don't need GD since it already is a png.
$data = 'data:image/png;base64,AAAFBfj42Pj4';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.png', $data);
And as a one-liner:
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
An efficient method for extracting, decoding, and checking for errors is:
if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
$data = substr($data, strpos($data, ',') + 1);
$type = strtolower($type[1]); // jpg, png, gif
if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) {
throw new \Exception('invalid image type');
}
$data = str_replace( ' ', '+', $data );
$data = base64_decode($data);
if ($data === false) {
throw new \Exception('base64_decode failed');
}
} else {
throw new \Exception('did not match data URI with image data');
}
file_put_contents("img.{$type}", $data);
how to save base64 image server side
I used this function to save base64 image and it's working fine. Try this-
function saveBase64ImagePng($base64Image, $imageDir)
{
//set name of the image file
$fileName = 'test.png';
$base64Image = trim($base64Image);
$base64Image = str_replace('data:image/png;base64,', '', $base64Image);
$base64Image = str_replace('data:image/jpg;base64,', '', $base64Image);
$base64Image = str_replace('data:image/jpeg;base64,', '', $base64Image);
$base64Image = str_replace('data:image/gif;base64,', '', $base64Image);
$base64Image = str_replace(' ', '+', $base64Image);
$imageData = base64_decode($base64Image);
//Set image whole path here
$filePath = $imageDir . $fileName;
file_put_contents($filePath, $imageData);
}
Hope this will help you.
How to save a PNG image server-side, from a base64 data string javascript
changed the php to -------->
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() . 'txtimg.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
which i got from ----->http://j-query.blogspot.com/2011/02/save-base64-encoded-canvas-image-to-png.html
-cheers works awesome :)
Laravel how to save the base64 data image?
This should work:
json_decode($request->input('avatar'))->output->image
How to save base64 string as image server-side
try this
$data = $_POST['form_new_data'];
file_put_contents('img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)));
How to save base64 data as png image to server?
You can use the filesystem API.
var fs = require('fs');
fs.writeFile("/tmp/test.png", $data, "binary", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
Also see here
Convert Base64 string to an image file?
The problem is that data:image/png;base64,
is included in the encoded contents. This will result in invalid image data when the base64 function decodes it. Remove that data in the function before decoding the string, like so.
function base64_to_jpeg($base64_string, $output_file) {
// open the output file for writing
$ifp = fopen( $output_file, 'wb' );
// split the string on commas
// $data[ 0 ] == "data:image/png;base64"
// $data[ 1 ] == <actual base64 string>
$data = explode( ',', $base64_string );
// we could add validation here with ensuring count( $data ) > 1
fwrite( $ifp, base64_decode( $data[ 1 ] ) );
// clean up the file resource
fclose( $ifp );
return $output_file;
}
when saving canvas image server-side, from a base64 data string,it produce blank image
I suspect you haven't configured your development box to display PHP error messages. You are defining a constant that is not a valid identifier:
define('localhost/samp/sample2/uploads', 'images/');
That means that you cannot use it directly:
$file = localhost/samp/sample2/uploads . uniqid() . '.png';
... should be triggering:
Notice: Use of undefined constant localhost - assumed 'localhost'
Notice: Use of undefined constant samp - assumed 'samp'
Warning: Division by zero
Notice: Use of undefined constant sample2
Warning: Division by zero
Notice: Use of undefined constant uploads - assumed 'uploads'
Warning: Division by zero
... and file
will only contain the base file name (e.g. 53676a01cdb59.png
) but not path component. You need to use this syntax:
$file = constant('localhost/samp/sample2/uploads') . uniqid() . '.png';
... or, even better, give the constant a sensible name:
define('DIR_UPLOADS', 'images/');
Related Topics
How to Run a PHP Script in the Background After a Form Is Submitted
How to Get Last Insert Id in Sqlite
Regex - Exclude "-" Between Numbers
How to Get Part of Url Before Last Slash With PHP
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses
How to Get Filename from a Variable (Url) in PHP
Getting Date Format M-D-Y H:I:S.U from Milliseconds
How to Deal With MySQLi Problems - MySQLi_Fetch_Array(): Argument #1 Must Be of Type MySQLi_Result
Send Email Using the Gmail Smtp Server from a PHP Page
How to Catch a PHP Fatal ('E_Error') Error
Convert Svg Image to Png With PHP
How to Send Data Using Redirect With Laravel
Checking Dates If Between Range of Dates in Laravel 5
How to Remove Query Part from $Request->All() in Laravel Framework
Php Adding 15 Minutes to Time Value
How to Stop My PHP Form Resubmitting Input After Page Refresh