Intervention/image · error · Intervention\Image\Exceptions\InvalidArgumentException
Unable to decode binary data from empty string
Error message
Unable to decode binary data from empty string
What it means
Thrown by BinaryImageDecoder::decode() when the input string is empty (''). After the Stringable/string cast, a zero-length payload cannot contain an image, so it is rejected before Imagick is invoked. Distinct from decode failures: it means there was no data at all.
Source
Thrown at src/Drivers/Imagick/Decoders/BinaryImageDecoder.php:53
* @see DecoderInterface::decode()
*
* @throws InvalidArgumentException
* @throws ImageDecoderException
* @throws DriverException
* @throws StateException
*/
public function decode(mixed $input): ImageInterface
{
if (!is_string($input) && !$input instanceof Stringable) {
throw new InvalidArgumentException(
'Image source must be binary data of type string or instance of ' . Stringable::class,
);
}
$input = (string) $input;
if ($input === '') {
throw new InvalidArgumentException('Unable to decode binary data from empty string');
}
try {
$imagick = new Imagick();
$imagick->readImageBlob($input);
} catch (ImagickException) {
throw new ImageDecoderException('Failed to decode unsupported image format from binary data');
}
// decode image
$image = parent::decode($imagick);
// get media type enum from string media type
$format = Format::tryCreate($image->origin()->mediaType());
// extract exif data for appropriate formats
if (in_array($format, [Format::JPEG, Format::TIFF])) {
$image->setExif($this->extractExifData($input));View on GitHub (pinned to 5598b9e397)
Solutions
- Reject empty input before reading: if ($input === '') fail with a validation message
- For uploads, check $_FILES['file']['error'] === UPLOAD_ERR_OK and size > 0
- Treat empty fetch bodies as an error rather than passing them on
Example fix
// before
$image = $manager->read($request->file('avatar')->getContent()); // '' when upload empty
// after
$file = $request->file('avatar');
abort_if($file === null || $file->getSize() === 0, 422, 'Image required');
$image = $manager->read($file->getContent()); Defensive patterns
Strategy: validation
Validate before calling
if ($input === null || trim((string) $input) === '') {
throw new InvalidArgumentException('image data is empty');
}
$image = $manager->read((string) $input); Type guard
function isNonEmptyString(mixed $value): bool
{
return is_string($value) && $value !== '';
} Try / catch
use Intervention\Image\Exceptions\InvalidArgumentException;
try {
$image = $manager->read($input);
} catch (InvalidArgumentException $e) {
return back()->withErrors(['image' => 'empty upload']);
} Prevention
- Require non-empty uploads at the validation layer (size > 0, UPLOAD_ERR_OK)
- Treat empty fetch bodies as transport errors, not image input
- Never pass optional request fields to read() without a presence check
When it happens
Trigger: $manager->read(''), or reading a variable that resolves to '' — an empty uploaded file (0-byte $_FILES entry), file_get_contents on an empty/failed fetch returning false-cast-to-string, trim()ed user input, or a Stringable whose __toString returns ''.
Common situations: Upload forms submitted without a chosen file; upstream fetch returning an empty body that is passed through; queue payloads where the image field was never set; tests feeding '' as a quick placeholder.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Input is not valid Base64-encoded data
- Unable to Base64-decode image from string
- Image source must be binary data of type string or instance
- Image source must be data uri scheme of type string or
- Image source must be of type
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/f951f747050f6c2c.
Report an issue: GitHub.