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);
Related
I want to resize image beofre upload it into database. I store image in this way
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$success = $image->move($upload_path1, $image_full_name);
}
Now, I want to resize image before uploading it. I try to use intervation package, I try this
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$img = Image::make($image)->resize(300, 200);
$img->save($upload_path1, 60, $image_full_name);
}
and I got this error
"Encoding format (1691942233153059.jpg) is not supported."
What is the best way to resize image before upload and also set image with unique name?
1- use getRealPath() inside Image::make()
2- save image in particular path.
if($request->hasFile('image')) {
$image = $request->file('image');
$file_name = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$file_name));
}
I'm using this code to upload files or images. It's working but can't upload a large file and I want to upload a file when selecting it from the local computer like the below image.
I use below PHP code in the controller.
$image = $request->file('file_upload');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
echo $new_name;
$image->move(public_path('images'), $new_name);
Try to use this code.
Change the $request->sharing_file with your field name.
You can increase the $size for image what you want, currently it is 16mb and working fine.
$myimage = $request->image;
$size = getClientSize();
if($sizes < 16777216){
$fileMimeType = explode('/', $myimage->getClientMimeType());
$fileType = $fileMimeType[0];
$originalFileName = substr($myimage->getClientOriginalName(), 0, strpos($myimage->getClientOriginalName(), "."));
$originalFileName = substr(str_replace(' ', '-', $originalFileName),0,10);
$rand = rand(9,1000);
$fileName = $rand.'-'.$originalFileName.'.'.$myimage->getClientOriginalExtension();
$upload = $values->move(public_path('images'), $fileName);
if($upload) {
$message = 'File Uploaded';
} else {
$message = "Failed to upload file";
}
}
else {
$message = 'Files size should be less than 16 MB.';
}
Try
if ($request->hasFile('file_upload')) {
$destinationPath = public_path().'/images/';
$file = $request->file_upload;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
$input['your_databse_table_field_name'] = $fileName;
}
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;
}
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);
}
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.