The problem Unable to decode input
at $image = $manager->read($file);
.
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)