I am attempting to automatically resize images using the intervention package but I keep on getting the error when I go back to my browser page. I have already added Intervention\Image\ImageServiceProvider::class, to the providers section and, 'Image' => Intervention\Image\Facades\Image::class, to the aliases section of config/app.php file.
if($request->hasfile('image'))
{
$imagePath = $request->file('image');
$extension = $imagePath->getClientOriginalExtension();
$filename = time().'.'.$extension;
$imagePath->storeAs('uploads/public/', $filename);
}
$image = Image::make("uploads/public/($imagePath)")->fit(1200,1200);
$image->save();
Related
I'm dealing with a few issues regarding multiple images uploads. My project deals with a form the user inputs multiple images that save to a database, but first temporarily stores the images in a directory to show a preview of sorts. In my class, which deals with the uploading of the image I am trying to rename the images as seen below.
I uploaded seven images on the dropdown.
I see that all its images were stored in the database by one.
While it had to be stored under different names.
Controller
public function store(Product $product, Request $request)
{
$path = null;
if ($request->hasFile('file'))
{
$file = $request->file('file');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$path = $file->storeAs($this->path, $fileName, 'public');
}
$gallery = Gallery::create([
'product_id' => $product->id,
'image' => $path,
]);
return response()->json($gallery);
}
$file = $request->file('file');
$name = date('YmdHi');
$extension = $name.$file->getClientOriginalExtension();
In controller , just replace it.
I have a function in my Laravel application that allows users to upload profile pictures to their profile.
Here is the method:
/**
* Store a user's profile picture
*
* #param Request $request
* #return void
*/
public function storeProfilePicture(User $user = null, Request $request)
{
$user = $user ?? auth()->user();
//Retrieve all files
$file = $request->file('file');
//Retrieve the file paths where the files should be moved in to.
$file_path = "images/profile-pictures/" . $user->username;
//Get the file extension
$file_extension = $file->getClientOriginalExtension();
//Generate a random file name
$file_name = Str::random(16) . "." . $file_extension;
//Delete existing pictures from the user's profile picture folder
Storage::disk('public')->deleteDirectory($file_path);
//Move image to the correct path
$path = Storage::disk('public')->putFile($file_path, $file);
// Resize the profile picture
$thumbnail = Image::make($path)->resize(50, 50, function ($constraint) {
$constraint->aspectRatio();
});
$thumbnail->save();
$user->profile()->update([
'display_picture' => Storage::url($path)
]);
return response()->json(['success' => $file_name]);
}
I am trying to use the Intervention Image library to resize the uploaded picture from the storage folder, but I always get the same error.
"Image source not readable", exception: "Intervention\Image\Exception\NotReadableException"
I have also tried with Storage::url($path) as well as storage_path($path)
Here below code work very well with the storage path, you can directly make thumb image from request and put anywhere as a desire path as follows.
Note that here I am not deleting previse stored files or directory as I am assuming that was done already.
$file = $request->file('file');
$path = "public/images/profile-pictures/{$user->username}/";
$file_extension = $file->getClientOriginalExtension();
$file_name = time(). "." . $file_extension;
// Resize the profile picture
$thumbnail = Image::make($file)->resize(50, 50)->encode($file_ext);
$is_uploaded = Storage::put( $path .$file_name , (string)$thumbnail ); // returns true if file uploaded
if($is_uploaded){
$user->profile()->update([
'display_picture'=> $path .$file_name
]);
return response()->json(['success' => $file_name]);
}
Happy coding
I have difficulty understanding the file save and retrieval in Laravel. I managed to get the file saved into correct path
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
However not I want to create an a-tag to download the file, but cannot manage to get to download the file.
{{$filenameToStore}}
The file download but I get the error "Failed- Server Problem". I do not want to use a same link as these files are confidential and should not be able to be downloaded outside the app.
Something like that?
Step 1:
Create table files id | filename | user_id.
Step 2:
Create model File
Step 3:
Add file row in table.
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
File::create([
'filename' => $filenameToStore,
'user_id' => Auth::id()
]);
Step 4:
Create controller method download.
public function download(){
$filename = $request->input('filename');
$file = File::where('user_id', Auth::id())
->where('filename', $filename)
->firstOrFail();
$path = Storage::path('agreements/' . $filename);
if(Storage::exists($path)){
return Response::download($path, $filename);
}
}
Step 5:
Replace your link:
{{$filenameToStore}}
Or you can build it based on the file ID.
I am uploading an image on my form then resizing it using a library. I can upload the pic with no problem here is my code:
public function store()
{
$this->validate(request(),[
'title'=>'required',
'body'=>'required|min:5 ',
'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
$image = request()->file('photo');
$fileName = $image->getClientOriginalName();
$publicPath = 'images/';
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 800);
$image_resize->save($publicPath.$fileName);
auth()->user()->addPost(new Posts( [
'title'=>request('title'),
'body'=>request('body'),
'photo'=> $fileName
]));
return redirect('/');
}
my issue is that the image doesn't show up on my front view. I have tried to remove the resizing its still the same
Well the issue here is that when u are saving the post on database photo option u are saving only the filename, you should concatinate your public path with the image path like so: 'photo'=> $publicPath.$fileName
$image = request()->file('photo');
$fileName = $image->getClientOriginalName();
$publicPath = 'images/';
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 800);
$image_resize->save($publicPath.$fileName);
auth()->user()->addPost(new Posts( [
'title'=>request('title'),
'body'=>request('body'),
'photo'=> $publicPath.$fileName
]));
So try now it should work.
I've trouble getting the file upload working with Laravel on my Windows 7 system. There is no issue with uploading the files but when I see the destination directory the uploaded file is not present.
After searching in Google and forums I found that there may be a problem with the 'Temp' directory.
The output of dd(sys_get_temp_dir()) is C:\Users\RAGHAV~1\AppData\Local\Temp.
However there is no directory called RAGHAV~1(I've enabled to see the hidden folders). In php.ini the upload_tmp_dir is set to C:\xampp\tmp.
Is there any conflict between these settings? Can you please help me to get the file upload working?
The code in the controller that process the uploaded files:
$validator = $this->brandValidator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);
$brand_name = $request->input('brand_name');
$image = $image_directory . $request->file('image')->getClientOriginalName();
$id = Brand::create([
'brand_name' => $brand_name,
'image' => $image,
]);
You have not specified the path for the file. Simply replace this
$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);
with this
$file = $request->file('image');
$filename = $file->getClientOriginalName().'.' . $file->getClientOriginalExtension();
$file->move(public_path('Uploads/Products/'), $filename);