how to save base64 image server side - php

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.

Related

Laravel upload image base 64

I have an api that sends to me a form-data base-64 image string
I found this code
public function image(Request $request){
$request = json_decode($request->getContent());
$image = $request->image; // your base64 encoded
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = str_random(10).'.'.'png';
\File::put(base_path(). '\public\users\\' . $imageName, base64_decode($image));
}
I works fine but all image has error and can't open.
Use this Code to Save image
$imageData will be your requested image $request->image
$imgdata = base64_decode($imageData);
$f = finfo_open();
$mime_type = "." . explode("/", finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE))[1];
$img = $imageData;
$image_parts = explode(";base64,", $img);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = public_path() . '/uploads/' . $folder_name . $img_perfix . $id . "." . $image_type;
file_put_contents($file, $image_base64);
if (!empty($file)) {
$profile = $table_name::find($id);
$profile->image = $img_perfix . $id . "." . $image_type;
}

How to replace string and upload with php

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);
}

Decoding base64 image and uploading it to server with php

I'm passing image as a base64 string from a form. Then I would like to decode it, rename it and upload it to my server folder. I can't get this to work so obviously I'm doing something wrong here. Any help would be appreciated.
My code:
$image = $_POST['image-data'];//base64 string of a .jpg image passed from form:
$image = str_replace('data:image/png;base64,', '', $image);//Getting rid of the start of the string:
$image = str_replace(' ', '+', $image);
$image = base64_decode($image);
$ext = strtolower(substr(strrchr($image, '.'), 1)); //Get extension of image
$lenght = 10;
$image_name = substr(str_shuffle("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ"), 0, $lenght);
$image_name = $image_name . '.' . $ext; //New image name
$uploaddir = '/var/www/vhosts/mydomain.com/httpdocs/img/'; //Upload image to server
$uploadfile = $uploaddir . $image_name;
move_uploaded_file('$image', $uploadfile);
Using file put content you can move files to folder
$file = $_POST['image-data'];
$pos = strpos($file, ';');
$type = explode(':', substr($file, 0, $pos))[1];
$mime = explode('/', $type);
$pathImage = "path to move file/".time().$mime[1];
$file = substr($file, strpos($file, ',') + 1, strlen($file));
$dataBase64 = base64_decode($file);
file_put_contents($pathImage, $dataBase64);

how to show message success after write file on php?

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

base64 image to png not working php

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!';
?>

Categories