1

The error Unable to decode input is occurred at the line $image = $manager->read($file);. Here is my code:

Controller

public function upload(Request $request)
    {
        try {
            $filePath = 'banners/';

            if (app()->environment('local')) {
                $filePath = 'local/' . $filePath;
            }

            $file = ImageService::processImage($request->file('file'), $filePath, 1920, 1080);

            return response()->json([
                'message' => 'Banner uploaded successfully',
                'url' => $file
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'message' => 'An error occurred',
                'detail' => $e->getMessage()
            ]);
        }
    }

ImageService

public static function processImage($file, $path, int $width = NULL, int $height = NULL)
    {
        try {
            $filePath = $path . auth()->id() . '_' . Str::random(40) . '_' . time() . '.jpg';
            $manager = new ImageManager(Driver::class);

            // $image = $manager->read('file url') -> this work normally

            // A problem here: Unable to decode input
            $image   = $manager->read($file);

            if ($file->getSize() > self::$maxSize || $width || $height) {
                if ($width && ! $height) {
                    $image->resize(width: $width);
                } elseif (! $width && $height) {
                    $image->resize(height: $height);
                } else {
                    $image->resize($width ?? self::$defaultWidth, $height ?? self::$defaultHeight);
                }
            }
            $encoded = $image->encode(new JpegEncoder(quality: 75));
            Storage::put($filePath, $encoded, 'public');

            return $filePath;
        } catch (Exception $e) {
            dd($e->getMessage());
            Log::info('PROCESS IMAGE ERROR: ' . $e->getMessage());
        }
    }

Someone can explain to me how to upload with Intervention Image (v3.7)

5
  • specify FilePathImageDecoder::class as second paramter as: $manager->read($file, FilePathImageDecoder::class); If still doesn't work try initializing the imagemanager class without driver class: new ImageManager();. Also, worth trying using $manager->make($file->getRealPath()); instead of $manager->make($file);
    – OMi Shah
    Commented Aug 12 at 3:37
  • It not work, still have the problem unable to decode input
    – elas
    Commented Aug 12 at 3:45
  • new ImageManager() expected 1 argument Driver. I was trying using $manager->make($file->getRealPath()); before and not work too
    – elas
    Commented Aug 12 at 7:11
  • can we see your form? or how you upload the 'file'?
    – Joe
    Commented Aug 30 at 9:52
  • 1
    Did you manage to solve this? I have the same problem and I'm struggling a lot with this Commented Oct 3 at 22:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.