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)
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);
unable to decode input
new ImageManager()
expected 1 argument Driver. I was trying using$manager->make($file->getRealPath());
before and not work too