Laravel 5.6 Image Intervention Library: 'Image Source Not Readable' - laravel

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);

Related

Storing image in local drive and saving path in db

I would like to save an image in my local drive, and insert its path to a DB, and then preview the image through the website. I have no idea how to do it and also I have not found anything similar anywhere.
Try this one
$image = $request->file('image');
$filename = time() . $image->getClientOriginalName();
$destination_path = public_path('/image/');
$image->save($destination_path . $filename);
$image->image_path= $destination_path;
Hope this helps :)
You can try to do this:
// To save image on local drive and insert path to db.
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$path = $request->image->store('public/images');
$path = basename($path);
$image = new Images();
$image->photo = $path;
$image->save();
}
I hope it would be helpful.

save an avatar in octobercms database after i modified it

after resizing the user avatar with intervention image ,i try to store it in the octobercms database like this code:
if (Input::hasFile('avatar')) {
$file= Input::file('avatar');
$filenamewithextension = $file->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $file->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
Storage::put('public/profile_images/'. $filenametostore, fopen($file, 'r+'));
Storage::put('public/profile_images/thumbnail/'. $filenametostore, fopen($file, 'r+'));
//Resize image here
$thumbnailpath ='storage/app/public/profile_images/thumbnail/'.$filenametostore;
$img = Image::make($file->getRealPath());
$img->crop(request('w'), request('h'), request('x1'), request('y1'));
$img->save($thumbnailpath);
$user->avatar= $filenametostore;
}
i get this error:
The avatar must be an image.
C:\wamp643\www\october3\vendor\october\rain\src\Database\Traits\Validation.php line 340
i really don't know what to do ,i'm a beginner.
please help me!!
you are just setting file name you should set full path of image there.
Assuming you have already avatar attachment relation in user model
public $attachOne = [
'avatar' => ['System\Models\File']
];
replace
$user->avatar = $filenametostore;
with
$user->avatar = storage_path($thumbnailpath);
may be it should work.
if doubt please comment.

Laravel resize and upload to storage

I am trying to resize, rename and upload the image using laravel Storage, intervention on laravel5.
Here is my upload code:
if( $request->file('logo_image') ){
$logo_path = $request->file('logo_image')->store('university/'.Auth::User()->id);
if ( $logo_path ){
Storage::delete($university->logo_image);
}
$university->logo_image = $logo_path;
}
$university->update();
It is storing the image on folder like: storage/app/public/university/1/fjkdhjkfdh.jpg
Now I want to rename image name like university-name.jpg and also resize and upload to sam directory.
So I should have two images one is storage/app/public/university/1/university-name.jpg.jpg
and other is storage/app/public/university/1/thumbnail/university-name.jpg
I play around with this for the whole day but no success.
if(Input::file())
{
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('profilepics/' . $filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$user->image = $filename;
$user->save();
}
Please someone can help me?
Thanks in advance.

how to upload image from localhost to server path in laravel?

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

Laravel 5 image resize

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

Resources