Intervention/image · error · InvalidArgumentException

Base64-encoded data must be either of type string or instanc

Error message

Base64-encoded data must be either of type string or instance of Stringable

What it means

decodeBase64Data() in AbstractDecoder accepts only a string or a Stringable object carrying Base64-encoded image data. The guard runs before any decoding, so arrays, null, integers, resources, and objects without __toString() are rejected immediately with this InvalidArgumentException.

Source

Thrown at src/Drivers/AbstractDecoder.php:81

        } finally {
            if (is_resource($source)) {
                fclose($source);
            }
        }

        return new Collection(is_array($data) ? $data : []);
    }

    /**
     * Decodes given base64 encoded data.
     *
     * @throws InvalidArgumentException
     * @throws DecoderException
     */
    protected function decodeBase64Data(mixed $input): string
    {
        if (!is_string($input) && !$input instanceof Stringable) {
            throw new InvalidArgumentException(
                'Base64-encoded data must be either of type string or instance of Stringable',
            );
        }

        $decoded = base64_decode((string) $input, true);

        if ($decoded === false) {
            throw new DecoderException('Input is not valid Base64-encoded data');
        }

        if (base64_encode($decoded) !== str_replace(["\n", "\r"], '', (string) $input)) {
            throw new DecoderException('Input is not valid Base64-encoded data');
        }

        return $decoded;
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Verify the value is a string (or cast it) before passing: is_string($v) || $v instanceof Stringable
  2. Null-check optional fields and skip the read when empty
  3. Extract the body first when the source is a stream object: (string) $stream->getContents()

Example fix

// before
$image = $manager->read($request->input('image'));

// after
$data = $request->input('image');
$image = is_string($data) || $data instanceof \Stringable
    ? $manager->read($data)
    : throw new \RuntimeException('Missing image data');
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_string($data) && !$data instanceof \Stringable) {
    throw new \InvalidArgumentException('Image data must be stringable');
}

Type guard

function isStringable(mixed $value): bool
{
    return is_string($value) || $value instanceof \Stringable;
}

Prevention

When it happens

Trigger: Calling the base64 decoding path (read()/decode with base64 input) with a value that came out of json_decode as an array, a null from a missing request field, or a stdClass without __toString().

Common situations: Untyped request payloads fed straight to ImageManager::read(); optional upload fields that are null when omitted; passing a PSR-7 stream object where a string is expected.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/d76ce77d9baf67dd. Report an issue: GitHub.