I am getting error 'Can't write image data to path', and i have folder permission too.
$image = $request->file('image');
$name = time()."_". $image->getClientOriginalName();
$img = Image::make($image->getRealPath());
$img->save('http://test.server.com/upload/' . $name);
first Install intervention image to your project
composer require intervention/image
after that
then inside your controller
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
then inside your method to store file
$file = Input::file('image');
$fileName = $file->hashName();
Image::make( $file->getRealPath() )->fit(70, 70)->save(public_path('path/to/dir/').$fileName)->destroy();
$object->imageName = $fileName;
this link was taken as reference
Related
I am building a site using laravel, and I got this error when trying to upload an image. I'm a beginner so, can I get some help?
$user = Auth::user();
$user->name = $request('name');
$user->update();
$file = $request->file('image');
$filename = $request->name . '-' . $user->id . '.jpg';
$user->name = $request('name');
$request is variable not func because of $
Because you didn't follow the naming rules in PHP : $request('name') .
I guess you were going to use $request->get('name')
For Uploading image you can use following code, Although I prefer using Intervention Image
//Auth::loginUsingId(1); #You can test your code without a real login
$data = $request->only(['name']);
if (request()->hasFile('image')) {
$file = request()->file('image');
$fileName = md5($file->getClientOriginalName() . time()) . "." . $file->getClientOriginalExtension();
$destinationPath = public_path('/images/'); //// I assume you have a folder named images in public.
$file->move($destinationPath, $fileName);
$data['avatar'] = $destinationPath.$fileName; // I assume you have a field named avatar in users table.
}
return $data; // Try this line for a better understanding then comment it
Auth::user()->update($data);
I'm using Image Intervention Library for image resizing, i've done the following steps:
1- Install Library: composer require intervention/image
2- Usage in code:
$file = $request->file('logo');
$destinationPath = 'db_images/public/';
$filename = $file->getClientOriginalName();
$extension = explode(".",$filename)[1];
$name = md5(microtime()).".".$extension;
$image_path = $destinationPath.$name;
$img = Image::make($filename)->resize(254,
179)->save($image_path);
$file->move($destinationPath,$img);
Issue Is:
When i try to upload the file using the above code this will return me 'Image Source is not readable'.
Pleas help me in resolving this issue. Thanks
Your are passing only the filename to the make method, you have to pass either the file object or the filepath:
file:
$img = Image::make($file)->resize(254, 179)->save($image_path);
path:
$img = Image::make($file->getRealPath())->resize(254, 179)->save($image_path);
I am currently using this packages for amazon file uploads and it works, the only problem is i don't know how to specify a folder in my chosen bucket.
Package Used - "aws/aws-sdk-php": "~3.0",
This is how i currently upload to the bucket
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image = $request->file('image');
$t = Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');
$imageName = Storage::disk('s3')->url($imageName);
You need to create a path and use in put method instead of $imageName to store in that particular path of bucket. It'll create the folder itself according to path.
For e.g. if you set the path as $path = "folder_1/folder_2/file.pdf", s3 driver will store file.pdf in folder_2 which is inside folder_1.
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image = $request->file('image');
//image stored in folder name image_folder
$path = "image_folder/".$imageName;
$t = Storage::disk('s3')->put($path, file_get_contents($image));
I want to ask, how can I make that somebody selects an image, then he clicks upload and the image will save into my project (example: project/public/images). Thank you :)
You can just do:
$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");
Storage::put($path, File::get($file->getRealPath()));
However. I would recommend to use laravel medialibrary package:
https://docs.spatie.be/laravel-medialibrary/v4/introduction
This way you can just do:
$post->addMediaFromRequest('image')->toCollection('images');
Or else you can do like this
$destinationPath = '';
$filename = '';
if (Input::hasFile('file')) {
$file = Input::file('file');
$destinationPath = public_path().'/images/';
$filename = time() . '_' . $file->getClientOriginalName();
$filename = str_replace(' ','_',$filename);
$uploadSuccess = $file->move($destinationPath, $filename);
}
I use Laravel 5 and have a form to upload a image. When saving the file I have in my controller methods to get the image and put it in a directory:
if ($request->hasFile('picture')) {
$destinationPath = 'uploads';
$filename = $image->getClientOriginalName();
$extension = $image->getClientOriginalExtension(); // add
$picture = sha1($filename . time()) . '.' . $extension; //add
$offer->image = $picture;
$image->move($destinationPath, $picture);
}
$offer->save();
Before I save the file I would like to resize the file with a max. width of 800px. Is there a image resize or compression function in Laravel available?
What would be the best idea to do this?
You can install and use package, similar to intervention.
Example from official website:
$img = Image::make('public/foo.jpg');
$img->resize(320, 240);
$img->insert('public/watermark.png');
$img->save('public/bar.jpg');
Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
try this code