2

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.

GotExx
  • 117
  • 4
  • 17
user2519032
  • 819
  • 1
  • 18
  • 34

2 Answers2

0

Visibly, Intervention can't read your image, check the path you give it.

dd($path);

In my case, this is how I proceed:

$img = Image::make('storage/'.$banner)->resize(800, 250);

Then to resize your image before uploading it, you can do like this:

//$image is the temporary path of your image (/tmp/something)
$image = $request->image;

//Create a Image object with the tmp path
$resized_img = Image::make($image);

//Do what you want and save your modified image on the same temporary path as the original image.
$resized_img->fit(300)->save($image);

//Upload your image on your bucket and get the final path of your image
$path = $image->storeAs('public/news', $name)
GotExx
  • 117
  • 4
  • 17
  • Hi. My configuration is default (without any changed directories). I'm still getting the same error even when I use your example. I've added manually the full path but it still has the same error. – user2519032 Oct 15 '20 at 11:26
  • Thank you for your suggesting, I've found a solution for the issue and you can see my answer. – user2519032 Oct 19 '20 at 10:28
0

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.

user2519032
  • 819
  • 1
  • 18
  • 34