i want upload image from base64 png and rename it with different image type.
if ($img) {
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $name . = str_replace("image/", ".", $type);
$success = file_put_contents($file, $data);
}
Try with file_get_contents it has built in base64 data uri protocol wrapper:
if ($img) {
$contents = file_get_contents($img); // $img = 'data:image/png;base64,....'
// setting $file name etc.
$success = file_put_contents($file, $contents);
}
Related
I have a PHP script that allows me to send a pdf file to it and it saves the PDF file locally.
<?php
define('UPLOAD_DIR', 'upload/');
$img = $_POST['image'];
$img = str_replace('data:image/pdf;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . 'image.pdf';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
How do I run a function once the file has been saved?
how can i save image server side i have this code but for some reason the image that i upload to the server is saved as text/x-generic. what can i do to fix this?
<?php
function base64_to_image($base64_string) {
$data = explode(',', $base64_string);
$ext = "";
switch ($data[0]) {
case "data:image/png;base64";
$ext = "png";
break;
case "data:image/jpg;base64";
$ext = "jpg";
break;
case "data:image/jpeg;base64";
$ext = "jpg";
break;
case "data:image/gif;base64";
$ext = "gif";
break;
}
$milli = round(microtime(true) * 1000);
$output_file = "img/" . date('Y-m-d_H:i:s') . "." . $milli . "." . $ext;
$ifp = fopen($output_file, "wb");
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $ifp;
}
$file = base64_to_image($_POST['file']);
var_dump($file);
?>
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.
I use move_uploaded_file to upload my image to server. But when I check on my server, I found out that my image have move another file. I didn't spot any error on my code with online example. Can anyone guide me to solve this problem? TQ.
$file_path = realpath("../media/rewardreceipt/");
$img=$_POST['img'];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$img_name = uniqid();
$file_name = $img_name . '.jpeg';
$file = $file_path. '/' . $file_name;
$success = file_put_contents($file_path , $data);
if($success) {
$arr = array('result' =>$file_name);
echo json_encode($arr);
} else{
$arr = array('result' =>'false');
echo json_encode($arr);
}
i have a function which can write image on server folder with PHP. i follow instruction on this link Can't save a HTML5 Canvas as Image on a server , my problem is, i want to show success message when image save. on my code, it just show message only when write image failed. here is my function :
$loc = $_POST['lokasi'];
define('UPLOAD_DIR', $loc);
$filename = $_POST['nama-file'];
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR ;
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
thanks for your response. :)
$loc = $_POST['lokasi'];
define('UPLOAD_DIR', $loc);
$filename = $_POST['nama-file'];
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR ;
if (file_put_contents($file, $data)) {
echo "Image saved";
} else {
echo "Unable to save the file.";
}
Note: With file-uploads its better to use move_uploaded_file
Im trying to convert base64 image to png.This is my code
$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.';
The file is created successfully. But it could not be open.Its not in a readable format.
Sorry for reviving your question, but take a look, it worked like charm:
<?php
$img = $_POST['Base64Variable'];
define('UPLOAD_DIR', 'YourFolder/');
$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 : 'Could not save the file!';
?>