How to resolve Imagick error when handling large image

When I'm trying to resolve a large image in PHP intervention/image package, sometimes I will receive a "Unable to decode input" error message, this message is too simple and not helpful.

To finding the cause, I found the matched Decoder class, and print the Imagick real error message:

class FilePathImageDecoder extends NativeObjectDecoder
{
    public function decode(mixed $input): ImageInterface|ColorInterface
    {
        // ...
        try {
            $imagick = new Imagick();
            $imagick->readImage($input);
        } catch (ImagickException $e) {
            var_dump($e->getMessage()); // <-- print here
            throw new DecoderException('Unable to decode input');
        }

        // ...

And the message shows: width or height exceeds limit

Now I noticed that this is a limit from Imagick config, there is the solution that we can change this limit.

I'm using Ubuntu server, the Imagik config file is located at /etc/ImageMagick-6/, if you are using other platform, you must find the Imagick config position. Now, open /etc/ImageMagick-6/policy.xml and look at about line 62 and 63, this is the origin settings, the max width and height is 16000px.

...
  <policy domain="resource" name="width" value="16KP"/>
  <policy domain="resource" name="height" value="16KP"/>
...

In my case, the image I upload is 25000px height, I change the value to:

...
  <policy domain="resource" name="width" value="32KP"/>
  <policy domain="resource" name="height" value="32KP"/>
...

Save and leave this file, then remember to restart Apache:

# Ubuntu
service apahe2 restart

Now handling of the large image file is works.