phalcon/cphalcon · error · Phalcon\Image\Exceptions\ImageLoadFailed
Failed to create image from file {file}
Error message
Failed to create image from file {file} What it means
The GD adapter constructor throws ImageLoadFailed($file) in two cases: the file exists but getimagesize() returns false (corrupt, truncated or not a real image), or the file does not exist AND no width+height were passed (those are required to create a blank canvas instead of loading a file).
Source
Thrown at phalcon/Image/Adapter/Gd.zep:89
) {
var image, imageInfo;
this->check();
let this->file = file;
let this->type = 0;
if (true === this->phpFileExists(this->file)) {
let this->realpath = (string) realpath(this->file);
let imageInfo = getimagesize(this->file);
if (false !== imageInfo) {
let this->width = imageInfo[0];
let this->height = imageInfo[1];
let this->type = imageInfo[2];
let this->mime = imageInfo["mime"];
} else {
throw new ImageLoadFailed(this->file);
}
switch (this->type) {
case IMAGETYPE_GIF:
let image = imagecreatefromgif(this->file);
break;
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
let image = imagecreatefromjpeg(this->file);
break;
case IMAGETYPE_PNG:
let image = imagecreatefrompng(this->file);
break;
case IMAGETYPE_WEBP:
let image = imagecreatefromwebp(this->file);View on GitHub (pinned to b7419de9cd)
Solutions
- Verify before constructing: if (false === @getimagesize($path)) reject the upload with 400/415
- For a blank canvas pass dimensions: new Gd('canvas', 800, 600); - width/height skip file loading
- Catch ImageLoadFailed around adapter construction and surface a friendly error or request a re-upload
Example fix
// before
$image = new \Phalcon\Image\Adapter\Gd($tmpName); // corrupt upload -> throws
// after
if (!is_file($tmpName) || false === @getimagesize($tmpName)) {
throw new \RuntimeException('Uploaded file is not a readable image');
}
$image = new \Phalcon\Image\Adapter\Gd($tmpName); Defensive patterns
Strategy: validation
Validate before calling
$path = '/uploads/' . $name;
if (!is_file($path) || false === @getimagesize($path)) {
throw new \RuntimeException('Not a readable image: ' . $path);
}
$adapter = new \Phalcon\Image\Adapter\Gd($path); Type guard
function isReadableImage(string $path): bool
{
return is_file($path) && false !== @getimagesize($path);
} Try / catch
try { $image = new \Phalcon\Image\Adapter\Gd($path); } catch (\Phalcon\Image\Exceptions\ImageLoadFailed $e) { http_response_code(415); exit('Corrupt or unreadable image file');
} Prevention
- Verify uploads with getimagesize() before any processing (also serves as content sniffing)
- Check is_uploaded_file() and file size during upload handling
- For blank canvases always pass width and height to the Gd constructor
When it happens
Trigger: new Gd('/tmp/upload123') on a zero-byte or corrupted upload; an SVG renamed to .jpg; a nonexistent path passed without the $width/$height constructor arguments.
Common situations: Processing user uploads without verification; temp files moved or deleted concurrently; files truncated by interrupted transfers or full disks; SVG uploads sneaking through extension checks.
Related errors
- Width and height must be specified
- Width must be specified
- Height must be specified
- Installed GD does not support {mime} images
- The color '{color}' is not a valid hex color
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/216121d2dc266519.
Report an issue: GitHub.