Intervention \ Image \ Exception \ NotReadableException Image source not readable - php

I am trying to resize images using the intervention package, but I keep on getting the error, whenever I refresh the 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('public/uploads/', $filename);
}
$image = Image::make(public_path("public/uploads/{$imagePath}"))->fit(1200,1200);
$image->save();

You don't need to add "public" with the public_path() function (unless you actually have a "public" folder inside Laravel's public folder.
So your file path should probably be:
public_path("uploads/{$imagePath}")
If it still doesn't work, echo out the path to check if it's correct.

Related

How to save images in public_html instead of the default storage in laravel?

Laravel shared hosting saving images problem:
Situation:
Can't create symlink() "symlink() has been disabled for security
reasons"
no ssh support just ftp
How to save images to public_html lets say to a file storage/cover_image
This was the shortcut generated during development
if($request->hasFile('cover_image')){
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
Storage::disk('my_upload')->put($fileNameToStore, $request->file('cover_image'));
//$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
I made a custom disk in called my_upload i cant figure out where it saves the file but it doesn't save in public_html/storage/cover_images
//custom my_disk code
'my_upload' => [
'driver' => 'local',
'root' => public_path().'/storage/cover_images',
'visibility' => 'public',
],
if i use storeAs it always stores storage that is not in public_html its also not an issue with permissions since am still testing i have set the permissions to 777
Looks like you are on shared hosting, what I know most hosting providers has their rules to php.ini configurations cmiiw.
So, if you want put your image on public_html, you can do like this:
$path = '/home/{your_shared_hosting_username}/public_html/cover_image';
$request->file('cover_image')->move($path, $filename);
After hours of research and grinding solved my problem eventually... to help other me's.
Step 1:
Create a custom disk in config/filesystem.php
//In my case
'my_upload' => [
'driver' => 'local',
'root' => public_path().'/storage',
'visibility' => 'public',
],
Step 2:
Change public_path() to public_html
//What worked for me
//Changed file: public_html/index.php
//after bootstrap declaration-> this is important
$app->bind('path.public', function() {
return __DIR__;
});
Step 3:
Now save your file like this
// Handle File Upload
if($request->hasFile('cover_image')){
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('cover_image')->storeAs('cover_images', $fileNameToStore,['disk' => 'my_upload']);
if(!$path){
return false;
}
} else {
$fileNameToStore = 'noimage.jpg';
}
In my case my images will be saved in public_html/storage/cover_images
Resources:
How to upload files in Laravel directly into public folder?
However that doesn't cut it since images are stored in the laravel public so you have to change the default laravel public to your now public_html which is your public in shared hosting.
https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
Tested on laravel 5.8
NB: I also noted running laravel config:cache breaks the saving location..you should probably avoid this command.
Incase you already ran the command config:clear undo's

File is not moving in directory in lumen

I am trying to store a file in the following path "public/uploads/images" with lumen.
Here is my code
if($request->hasFile('photo')){
$file = $request->file('photo');
$fileName = time().$counter.'.'.$request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move('/uploads/images', $fileName);
return $fileName;
}
But no file is moving to the path. only the file name is returned. What is the problem and how to solve?
try to use the public_path function to upload it into public folder.
Example:
$request->file('photo')->move(public_path("/uploads"), $newfilename);
Visit https://laracasts.com/discuss/channels/laravel/image-upload-using-storage-function-to-public-folder

Laravel not storing files from request

I got form with file upload named "image". Form is working and i'm getting image but for some reason it's not stored. this is the code I use to store image:
$path = $request->image->store('storage/uploads');
before that I check
$request->hasFile('image')
and later I'm saving $path. Path is successfully saved and when i check it it's really storage/uploads/radnomid but there is no file
If your filesystems.php configs were default then
$path = $request->image->store('storage/uploads');
saved your files not in storage/uploads but in storage/app/storage/uploads.
And your current accepted answer is not really good because public_path() intended to be used for storing template files not user uploads. According to official docs uploads should be used in storage/ folder with a symbolic link from public/storage/ but not in public/.
Read more in https://laravel.com/docs/5.4/filesystem#the-public-disk
Sorry for posting my own answer but problem was that by default laravel public disk does not upload to public directory. Settings needed to be changed like this:
config/filesystems.php
'public' => [
'driver' => 'local',
'root' => public_path(),
'visibility' => 'public',
],
and then simply:
$path = $request->image->store('storage/uploads','public');
you can try with this code,
Image::make($request->file('image'))->save('upload_path/filename.jpg'));
here is complete doc of this Image package.
http://image.intervention.io/
Make sure that you are including enctype of multipart/form-data. You can either add the attribute directly to your form, or you can add a 'files' => true if you are using the FORM helper.
This is the conventional way to use the upload function that comes with Laravel 5.
public function processForm(Request $request)
{
if( $request->hasFile('image') ) {
$file = $request->file('image');
// Now you have your file in a variable that you can do things with
}
}
Try doing like this
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/comment/' . $filename);
Image::make($image)->resize(800, 600)->save($location);
}
I think you should try this:
Image::make($request->file('image'))->save(storage_path('uploads/image.jpg'));
Hope this works for you!

Laravel file upload NotFoundHttpException

I'm using Laravel to upload an image to my folder.
$file = Input::file('largeImage');
$filePath = '/uploads/'.date("Y/m").'/'.time().'/';
$path = $filePath;
$file->move($path, $file->getClientOriginalName());
The image is successfully uploaded.
Now when I try to access it:
http://localhost:8080/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg
I'm having the Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error.
I even tried http://localhost:8080/public/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg
But the same error. What might be the problem? I checked the uploads folder and the image is there.
You need to add a call to public_path helper:
$filePath = public_path() . '/uploads/'.date("Y/m").'/'.time().'/';
Otherwise it will place it in the app root, I think.
This worked for me:
$file = Input::file('largeImage');
$filePath = '/uploads/'.date("Y/m").'/'.time().'/';
$filename = $file->getClientOriginalName();
$path = public_path().$filePath;
$file->move($path, $file->getClientOriginalName());
Ok, so it IS a Routing Issue. To solve this particular one, define a Route::get that navigates to your file.
Route::get("/download/{year}/{month}/{time}/{filename}", "Controller#downloadFile");
Then you'll need a function in that controller that handles the file download:
public function downloadFile($year, $month, $time, $filename){
$path = public_path()."/uploads/".$year."/".$month."/".$time."/".$filename".jpg";
// Should equate to: "/uploads/2015/01/142064476110377625_673554946025652_6686347512849117388_n.jpg
return Response::download($path, 'image.jpg');
}
Which in theory should work for your needs. The path may need to be modified to fit your needs but this should be the general idea. Test that out and let me know if it works.
Note
This isn't the best way to handle downloads, as you need to know the exact filename of the file you want, but it points you in the right direction.

Cant write to public folder in laravel

I'm trying to manage my fileuploading but it seems I cant write to the folder public/uploads.
I have write permission so I'm not sure what it can be, does somebody see a typo?
I use the intervention/image library.
My code is:
$file = Input::file($fileName);
if ($file->isValid()) {
if ($file->getMimeType() == 'image/jpeg' || $file->getMimeType() == 'image/png') {
$path = public_path("uploads", $file->getClientOriginalName());
Image::make($file->getRealPath())->resize(180, null)->save($path);
} else {
throw new FileException;
}
}
The exception thrown is:
Intervention \ Image \ Exception \ NotWritableException
Can't write image data to path (/home/vagrant/Sites/cms/public/uploads)
I've found the typo...
I had:
$path = public_path("uploads", $file->getClientOriginalName());
Changed to:
$path = public_path("uploads/" . $file->getClientOriginalName());
Thanks for the quick answer though!
You need to check the permissions of the folder, and its parent folders. If you're on linux, it should be 644. The individual folder may have write access, but the parent might not; or it could be for the wrong user. Check the Ownership group too.

Categories