-
-
Save fazlurr/9802071 to your computer and use it in GitHub Desktop.
<?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.'; | |
?> |
i use this code but problems arise to blank image in my uploaded folder not show image
its because your image is too big, try smaller images
my code is not not workin, i am not seeing actual image,Please give a best solution
define('UPLOAD_DIR', 'uploads/');
foreach ($_REQUEST['image'] as $value) {
$img = $value;
$img = str_replace('data:image/jpeg;base64,', '', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
$data1[] = $file;
}
echo json_encode($data1);
this code is working as per my requirements.
thanks
thanks this code is woking perfectly, but the image file was damaged and corrupte, any one give the best solution !!
$base64 = "data:image/png;base64,gAAAQ8AAAC6CAMAAACHgTh+AA=";
if(preg_match("^data:image\/(?<extension>(?:png|gif|jpg|jpeg));base64,(?<image>.+)$", $base64, $matchings))
{
$imageData = base64_decode($matchings['image']);
$extension = $matchings['extension'];
$filename = sprintf("image.%s", $extension);
if(file_put_contents($filename, $imageData))
{
// image saved
}
}
define('UPLOAD_DIR', 'images/');
$img = $_POST['img']; //base64 string
$data = file_get_contents($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
`<?php
function create_image($name,$directory){
define('UPLOAD_DIR', $directory);
$img = base64_encode(file_get_contents( $_FILES[$name]["tmp_name"] ));
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$basename=uniqid();
$file = UPLOAD_DIR . $basename . '.png';
$success=file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
return $basename.'.png';
}
`
Just what I was looking for! Thanks so much!!
Showw, thank you, you're excellent.
Thank so much! kaled-hoshme
if(isset($_POST['submit'])){
define('UPLOAD_DIR', 'img/');
$img = $_POST['image'];
$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.';
}
<form method="post" name="change" enctype="multipart-form/data">
<!-- <img src="" id="img" name="image" > -->
<input type="text" name="image" id="img" hidden="hidden" value="base64Code">
<input type="submit" name="submit">
</form>
what is UPLOAD_DIR is it constant we should use or is it path?
define('UPLOAD_DIR', 'img/');
It worked!
Thank you. Working fine
it works ! thank you
Dude, you just saved my life (I'm on the edge of a project deadline haha). Thanks bro!
define ('UPLOAD_DIR', 'imagens /');
$ img = $ _POST ['img']; // base64 string
$ data = file_get_contents ($ img);
$ arquivo = UPLOAD_DIR. uniqid (). '.png';
$ success = file_put_contents ($ arquivo, $ dados);
print $ sucesso? $ file: 'Não foi possível salvar o arquivo.';
Esse funcionou muito obrigado
$base64 = $_POST['yourImgSrc'];
$image_parts = explode(";base64,", $base64);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1]; //image extension
$image_base64 = base64_decode($image_parts[1]);
$file = ''New Name of your image'.'.$image_type;
$uploadFile = file_put_contents("where to save your file directory/$file", $image_base64); //upload your file
$base64 = "data:image/png;base64,gAAAQ8AAAC6CAMAAACHgTh+AA="; if(preg_match("^data:image\/(?<extension>(?:png|gif|jpg|jpeg));base64,(?<image>.+)$", $base64, $matchings)) { $imageData = base64_decode($matchings['image']); $extension = $matchings['extension']; $filename = sprintf("image.%s", $extension); if(file_put_contents($filename, $imageData)) { // image saved } }
You need to add delimiters to the preg_match()
preg_match("/^data:image\/(?<extension>(?:png|gif|jpg|jpeg));base64,(?<image>.+)$/", $base64, $matchings))
Genial, me funcionó
`
$data = $_POST["image"];
$rand = uniqid();
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = "../firmas/" . $rand . '.png';
$movefile = file_put_contents($file, $data);
$response = array(
'movefile' => $movefile,
'rand' => $rand,
'file' => $file
);
echo json_encode($response);
`
Thanks @fazlurr , it worked perfectly for me.
copy pasted code, but blank image problem.