I tried to resize my image before uploading but is not working but I am able to upload the image, Here is my Controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:20480',
]);
$imageFile = $request->file('image');
$name = $imageFile->getClientOriginalName();
$path = $request->file('image')->store('images', 'public');
$resize = Image::make($imageFile)->resize(50, 50)->stream();
$save = new Gallery;
$save->image = $path;
$save->status = 1;
$save->user_id = Auth::id();
$save->save();
return redirect('gallery')->with('success', 'Image has been uploaded')->with('image',$name);
}
try below example source code. I have done image resize functionality in one of my project with Laravel version 8
<?php
if($request->hasFile('dealimg')){
$file = $request->File('dealimg');
$original_name = $file->getClientOriginalName();
$file_ext = $file->getClientOriginalExtension();
$destinationPath = 'uploads/deals';
$file_name = "deal".time().uniqid().".".$file_ext;
// $path = $request->deal_img->store('uploads');
$resize_image = Image::make($file->getRealPath()); //for Resize the Image
$resize_image->resize(150, 150, function($constraint){ //resize with 150 x 150 ratio
$constraint->aspectRatio();
})->save(public_path($destinationPath) . '/' . $file_name);
$deal_img = $destinationPath."/".$file_name;
$request->request->add(['deal_img' => $deal_img]);
}
?>
I hope this one helps to you... see this
I'm Using Laravel 8 and I'm Facing this error.
Can't write image data to path (/home/u520518518/domains/dev.peacockindia.in/public_html/public/Backend/Image/Brand/2003012866_Brand-Image_Mobile_jpg)
This is My Code
if($request->logo){
$image = $request->file('logo');
$img = rand().'_'.'Brand-Image'.'_'.$request->title.'_'.$image->getClientOriginalExtension();
$loc = public_path('Backend/Image/Brand/'.$img);
Image::make($image)->save($loc);
$brand->logo = $img;
}
$image = $request->file('image');
$slug = Str::slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
if (!file_exists('storage/uploads/employee-images'))
{
mkdir('storage/uploads/employee-images',0777,true);
}
$image->move('storage/uploads/employee-images',$imagename);
}else{
$imagename = "default.png";
}
I tried this
$loc = 'Backend/Image/Brand/'.$img;
and its worked
I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //
I am trying to convert and store base64 encoding to an image in laravel 5.4. Here $ifphoto has base64 value. I also checked it using return. I have visitor_photo folder in public folder. How can I store that. My controller function is here. Thanks in advance
public function store(Request $request)
{
$visitor = new visitor() ;
$ifphoto = $request->v_photo;
if (isset($ifphoto)) {
define('UPLOAD_DIR', 'public/visitor_photo/');
$encoded_data = $ifphoto;
$img = str_replace('data:image/jpeg;base64,', '', $encoded_data );
$data = base64_decode($img);
$file_name = 'image_'.date('Y-m-d-H-i-s', time()); // You can change it to anything
$file = $file_name . '.png';
$request->v_photo->move(base_path('public/visitor_photo'), $file);
// $file = UPLOAD_DIR . $file_name . '.png';
// $success = file_put_contents($file, $data);
$visitor->v_photo = $file_name;
}
$visitor->save();
return redirect('/home');
}
Below code will store your base64 string as image in app/public folder. You can change the path as per your requirement.
Code :
if (isset($ifphoto)) {
$file_name = 'image_'.date('Y-m-d-H-i-s', time()).'.png';
if($ifphoto!=""){
\Storage::disk('public')->put($file_name,base64_decode($ifphoto));
}
}
I send a png image file to controller in base64 via Ajax. I've already test and sure that controller has received id but still can't save it to public folder.
Here is my controller
public function postTest() {
$data = Input::all();
//get the base-64 from data
$base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);
//decode base64 string
$image = base64_decode($base64_str);
$png_url = "product-".time().".png";
$path = public_path('img/designs/' . $png_url);
Image::make($image->getRealPath())->save($path);
// I've tried using
// $result = file_put_contents($path, $image);
// too but still not working
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
Intervention Image gets binary data using file_get_content function:
Reference : Image::make
Your controller should be look like this:
public function postTest() {
$data = Input::all();
$png_url = "product-".time().".png";
$path = public_path().'img/designs/' . $png_url;
Image::make(file_get_contents($data->base64_image))->save($path);
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
$data = Input::all();
$png_url = "perfil-".time().".jpg";
$path = public_path() . "/img/designs/" . $png_url;
$img = $data['fileo'];
$img = substr($img, strpos($img, ",")+1);
$data = base64_decode($img);
$success = file_put_contents($path, $data);
print $success ? $png_url : 'Unable to save the file.';
$file = base64_decode($request['image']);
$safeName = str_random(10).'.'.'png';
$success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
print $success;
This is an easy mistake.
You are using public_path incorrectly. It should be:
$path = public_path() . "/img/designs/" . $png_url;
Also, I would avoid your method of sending the image. Look at a proper upload in a form and use Laravel's Input::file method.
My solution is:
public function postTest() {
$data = Input::all();
//get the base-64 from data
$base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);
//decode base64 string
$image = base64_decode($base64_str);
Storage::disk('local')->put('imgage.png', $image);
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
echo $storagePath.'imgage.png';
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
what am i doing is using basic way
$file = base64_decode($request['profile_pic']);
$folderName = '/uploads/users/';
$safeName = str_random(10).'.'.'png';
$destinationPath = public_path() . $folderName;
file_put_contents(public_path().'/uploads/users/'.$safeName, $file);
//save new file path into db
$userObj->profile_pic = $safeName;
}
Store or save base64 images in the public folder image and return file path.
$folderPath = public_path() . '/' . 'images/';
$image_parts = explode(";base64,", $image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$uniqid = uniqid();
$file = $folderPath . $uniqid . '.' . $image_type;
file_put_contents($file, $image_base64);
return $file;
Actually, Input::all() returns an array of inputs so you have following:
$data = Input::all();
Now your $data is an array not an object so you are trying to access the image as an object like:
$data->base64_image
So, it's not working. You should try using:
$image = $data['base64_image'];
Since it's (base64_image) accessible from $_POST then Input::file('base64_image') won't work because Input::file('base64_image') checks the $_FILES array and it's not there in your case.
Here is my solution for the file upload from base_64.
public static function uploadBase64File(Request $request, $requestName = 'imageData', $fileName = null, $uploadPath = 'uploads/images/')
{
try {
$requestFileData = $request->$requestName;
// decode the base64 file
$file = base64_decode(preg_replace(
'#^data:([^;]+);base64,#',
'',
$request->input($requestName)
));
if (in_array($file, ["", null, ' '])) {
return null;
}
//handle base64 encoded images here
if ($fileName == null) {
$fileName = Str::random(10);
}
$extension = '.' . explode('/', explode(':', substr($requestFileData, 0, strpos($requestFileData, ';')))[1])[1];
$filePath = $uploadPath . '' . $fileName . '' . $extension;
// dd($extension);
if (!File::exists(public_path($uploadPath))) {
File::makeDirectory(public_path($uploadPath), 0777, true);
}
// dd($filePath);
$ifImageUploadSuccessful = File::put(public_path($filePath), $file);
if (!$ifImageUploadSuccessful) {
return null;
}
return '/' . $filePath;
// throw new Exception("Unable To upload Image");
} catch (Exception $e) {
// dd($e);
throw new Exception($e->getMessage());
}
}
I'v done it!!
I replaced
$data->base64_image to $_POST['base64_image'] and then use
$result = file_put_contents($path, $image);
instead of Image::make($image->getRealPath())->save($path);
But this doesn't look like a laravel ways. I you have another way that look more elegant please tell me!