1

I am using Laravel Image Intervention to resize an image upload field on my form.

This is the error I receive on upload - I am running on Valet.

Command (Extension) is not available for driver (Gd).

The following works fine without Image::make

      use Image;
      ...

      $authorID = Auth::user()->id;
      $file = request()->file('ts_image');
      if($file) {
      $file = Image::make($file)->resize(300, 300);

      $fileExtension = $file->extension();
      $unique_name = md5($file. time()).'.'.$fileExtension;
      //
      $fileImg = $file->storeAs('/public/images/' . $authorID, $unique_name);

Any ideas? Thanks!

Edit:

When dd($file) this is what is returned:

Image {#667 ▼
  #driver: Driver {#668 ▼
    +decoder: Decoder {#669 ▼
      -data: null
    }
    +encoder: Encoder {#670 ▼
      +result: null
      +image: null
      +format: null
      +quality: null
    }
  }
  #core: gd resource @16 ▼
    size: "300x300"
    trueColor: true
  }
  #backups: []
  +encoded: ""
  +mime: "image/jpeg"
  +dirname: "/private/var/tmp"
  +basename: "phpBPRGuD"
  +extension: null
  +filename: "phpBPRGuD"
}
scopeak
  • 545
  • 1
  • 5
  • 22
  • As its uploaded file it wont have an extension, from what im seeing by googling that error [this](https://stackoverflow.com/questions/30205613/generate-image-extension-from-mimetype) and [this](https://stackoverflow.com/questions/23199468/laravel-intervention-image-not-returning-an-extension) you may need to use `$file->mime()` and then match it. – Lawrence Cherone Nov 11 '18 at 20:26
  • @LawrenceCherone hmm interesting, when dd($image) I do seem to get the mime type back. See updated question. – scopeak Nov 11 '18 at 21:01
  • Yeah as said above, `extension` is null so its not available, the `extension` method is only a getter. So you prob need to just match out the mime, though im not sure how safe it is just to trust it. – Lawrence Cherone Nov 11 '18 at 21:05
  • @LawrenceCherone you have put me on the right track, just realised what I need to do :) will report back once worked it out! – scopeak Nov 11 '18 at 21:08
  • 1
    np, cryptic error though as its got nothing to do with it being gd, good luck – Lawrence Cherone Nov 11 '18 at 21:10
  • @LawrenceCherone right, think I've got it sorted. However as I'm using `$file->save()` rather than `$file->storeAs()` the file won't save. This is because the directory folder doesn't exist yet for that user. Is there a way around this? – scopeak Nov 11 '18 at 21:17
  • 1
    You can just create it if it does not exist i.e https://3v4l.org/ERu0p – Lawrence Cherone Nov 11 '18 at 21:21
  • Sorted! I had to change the `$file` when using `->storeAs()` as it expected `->save()` instead. Thanks again! – scopeak Nov 11 '18 at 21:37

1 Answers1

2

I used intervention to save my images into the database. I was working with laravel and i had to save images of different size, large, medium and small. this how it works for me

      if ($request->hasFile('image')) {
        $image_tmp = Input::file('image');
        if ($image_tmp->isValid()) {

            $extension = $image_tmp->getClientOriginalExtension();
            $filename = rand(111, 99999) . '.' . $extension;
            $large_image_path = 'images/backend_images/products/large/' . $filename;
            $medium_image_path = 'images/backend_images/products/medium/' . $filename;
            $small_image_path = 'images/backend_images/products/small/' . $filename;
            //resize image
            Image::make($image_tmp)->save($large_image_path);
            Image::make($image_tmp)->resize(600, 600)->save($medium_image_path);
            Image::make($image_tmp)->resize(300, 300)->save($small_image_path);
            $product->image = $filename;

        }
    }
Silah Kosgei
  • 61
  • 1
  • 5