I have this code to save my post images and it returns error of:
Intervention \ Image \ Exception \ NotReadableException
Unable to find file ().
My code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
}
I've got this code from Intervention \ Image \ Exception \ NotReadableException using laravel 4 but before that i had this code
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
$request->file('image')->move($location, $filename);
$food->image = $filename;
}
And it worked just fine, the reason I changed my code was to be able to resize images that's all.
Thanks.
You have to change the permissions on Windows/Temp file. Enable read permisson to the users group.
I have tested your code and fixed error. its working fine now.
$file = $request->file('image');
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image->getRealPath())->resize(800, 400)->save($location);
}
In my case, i've managed to get it work by adding this entry in php.ini
upload_tmp_dir = "C:\Users\<NAME>\AppData\Local\Temp"
and reload the server
try this
$image = $request->file('image');
$location = public_path('images/');
$filename = $location. ''.'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize('800','400')->save($filename);
UPDATE
$image = $request->file('image');
$location = public_path('images/');
Image::make($image)->resize('800','400')->save($location.$filename);
Also check this so post here it
$originalImage = 'images/1.jpg';
Image::make($originalImage);
I try many more, finally i got it.I find out if index.php file use in outside public folder then this problem show. Following this way to solve the problem.
$image = $request->file('image');
$input['imagename'] = hexdec(uniqid()).$image->getClientOriginalName();
$location = public_path("upload/image");
$imgs = Image::make($image->getPathname());
$imgs->resize(300 , 300, function ($constraint) { $constraint->aspectRatio(); })->save($location.'/'.$input['imagename']);
I try many more, finally I got it. Following this code I think solved your problem
$location = "assets/images/brand";`
$file = $request->image;
$filename =uniqid().time().'.'.$file->getClientOriginalExtension();
$image = Image::make(file_get_contents($file))->resize('600','600')->save($location.'/'.$filename);
Related
I am having issues getting my image uploads to move to the proper folders. When I upload them they just dump into the main img folder instead of going to the specific subfolder.
Here is the code I have:
$rental = new Rental;
$rental->title = $request->title;
$rental->name = $request->name;
$rental->description = $request->description;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/rentals' . $filename);
Image::make($image)->save($path);
$rental->image = $filename;
}
$rental->save();
You're missing a / in your $path. The following line...
$filename = time() . '.' . $image->getClientOriginalExtension();
will generate a string, something like 123456789.jpg. Then in the next line you're doing...
$path = public_path('img/rentals' . $filename);
which joins together img/rentals and 123456789.jpg, resulting in img/rentals123456789.jpg. Notice that . which concatenates two strings. You're then passing the resulting string to public_path which will refer to a folder in your public directory named img and a filename rentals123456789.jpg.
To solve your problem you just need to stick a forward slash in between:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/rentals/' . $filename);
which will result in a folder img/rentals inside your public path and a filename of 123456789.jpg.
I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.
I am trying to upload image in laravel. But when i upload it gives error.
Call to a member function getClientOriginalExtension() on null
Here is my Blade file form
{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
'method' => 'post','class'=>'form-row','files'=>true,
"enctype"=>"multipart/form-data"))}}
And here is controller
$imagename_bg = time() . '.' . $photo->getClientOriginalExtension();
$destinationPath = public_path('/uploads/blog');
$thumb_img = Image::make($photo->getRealPath())->resize(750, 450);
$thumb_img->save($destinationPath . '/' . $imagename_bg, 80);
$photo->move($destinationPath, $imagename_bg);
Please help me how to resolve this problem.
I am not able to understand your code. if you are looking for uploading image and resize using intervention package try this:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('image');
$img = Image::make($file);
$img->resize(400,270);
$name = pathinfo($_FILES['image']['name']);
$destination = public_path('/uploads/blog');
$ext = $name['extension'];
$rand= time().str_random(6).date('h-i-s').'.'.$ext;
$img->save($destination.$rand);
}
}
Or Without intervention pacakge:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('blogimage');
$destination = public_path('/uploads/blog');
$ext= Input::file('blogimage')->getClientOriginalExtension();
$mainFilename =time().str_random(5).date('h-i-s');
$file->move($destination, $mainFilename.".".$ext);
}
}
Hope it helps!
$photo = $request->file('file_name');
You only need to use this:
$photo = $request->file("blogimage");
instead of:
$photo = $request->input("blogimage")
Hope this will fixed your problem!
I'm working with laravel 5.4 I have a form which I can upload my logoimage and in update function I have this code:
//Save logo
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$general_Settings->logo = $filename;
Storage::delete($oldFilename);
}
$general_Settings->save();
for updating my image which is work but as you see I have Storage::delete($oldFilename); this part doesn't work and just keep the old image.
what do you think is issue of that?
Solved:
The issue was Filesystem.php I made my local root set to 'root' => public_path('avatars/'), and changed all my functions in my app because no way to save images in sub-folders and delete them just can save in sub-folders.
then my update function become like this:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = 'sitelogo' . '-' . time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/');
$request->file('logo')->move($location, $filename);
$general_Settings->logo = $filename;
}
$general_Settings->save();
I hope this help someone.
Since, You have stored logo file name only not full path of file,
You need to give full path from public folder to delete image. Change delete line as:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$file_path = "avatars/logos/".$oldFilename;
$general_Settings->logo = $filename;
Storage::delete($file_path);
}
$general_Settings->save();
This might work.
I am trying to create image thumbnails using intervention here is my controller
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
$image->fit(240, 157)->save(public_path('images/blog/' . $filename . '-thumbs.jpg'));
$add->image = $filename;
}
Got
Method fit does not exist.
What I am doing wrong here?
You have to update this line:
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
to
$image = Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
because fit is a method of InterventionImage object.