So far I'm uploading an image with the following code bellow:
if($request->hasFile('image')) {
$path = $request->image->getClientOriginalName();
$name = time() . '-' . $path;
$news->image = $request->file('image')->storeAs('public/news', $name);
}
It checks for file image, revert to it original name, creating a time format attached to the image filename and uploading into storage in the desired folder.
How can I resize that image before uploading into the file directory?
I've read about the Intervention, which is included in Laravel 7 but can someone help me to achieve this using Intervention and combine with my logic of uploading an image?
I've tried like this:
use Intervention\Image\ImageManagerStatic as Image; // use Intervention
if($request->hasFile('image')) {
$path = $request->image->getClientOriginalName();
$resize = Image::make($path)->fit(300);
$name = time() . '-' . $resize;
$news->image = $request->file('image')->storeAs('public/news', $name);
}
but I'm getting Image source not readable
error.
I've found a solution after a long testing. This is my code bellow:
if($request->hasFile('image')) {
$image = $request->file('image');
$imageName = $image->getClientOriginalName();
$fileName = 'public/news/' . time() . '-' . $imageName;
Image::make($image)->resize(600,300)->save(storage_path('app/' . $fileName));
$news->image = $fileName;
}
The main issue was the path and also I don't need to use storeAs() function, simply just using the Intervention functions...that's all. This approach is lot more flexible and it's very easy to implement additional features from the wonderful Intervention library.