{"record":{"id":"216121d2dc266519","repo":"phalcon/cphalcon","slug":"failed-to-create-image-from-file-file","errorCode":null,"errorMessage":"Failed to create image from file {file}","messagePattern":"Failed to create image from file (.+?)","errorType":"exception","errorClass":"Phalcon\\Image\\Exceptions\\ImageLoadFailed","httpStatus":null,"severity":"error","filePath":"phalcon/Image/Adapter/Gd.zep","lineNumber":89,"sourceCode":"    ) {\n        var image, imageInfo;\n\n        this->check();\n\n        let this->file = file;\n        let this->type = 0;\n\n        if (true === this->phpFileExists(this->file)) {\n            let this->realpath = (string) realpath(this->file);\n            let imageInfo      = getimagesize(this->file);\n\n            if (false !== imageInfo) {\n                let this->width  = imageInfo[0];\n                let this->height = imageInfo[1];\n                let this->type   = imageInfo[2];\n                let this->mime   = imageInfo[\"mime\"];\n            } else {\n                throw new ImageLoadFailed(this->file);\n            }\n\n            switch (this->type) {\n                case IMAGETYPE_GIF:\n                    let image = imagecreatefromgif(this->file);\n                    break;\n\n                case IMAGETYPE_JPEG:\n                case IMAGETYPE_JPEG2000:\n                    let image = imagecreatefromjpeg(this->file);\n                    break;\n\n                case IMAGETYPE_PNG:\n                    let image = imagecreatefrompng(this->file);\n                    break;\n\n                case IMAGETYPE_WEBP:\n                    let image = imagecreatefromwebp(this->file);","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Image/Adapter/Gd.zep#L71-L107","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$image = new \\Phalcon\\Image\\Adapter\\Gd($tmpName); // corrupt upload -> throws\n\n// after\nif (!is_file($tmpName) || false === @getimagesize($tmpName)) {\n    throw new \\RuntimeException('Uploaded file is not a readable image');\n}\n$image = new \\Phalcon\\Image\\Adapter\\Gd($tmpName);","handlingStrategy":"validation","validationCode":"$path = '/uploads/' . $name;\nif (!is_file($path) || false === @getimagesize($path)) {\n    throw new \\RuntimeException('Not a readable image: ' . $path);\n}\n$adapter = new \\Phalcon\\Image\\Adapter\\Gd($path);","typeGuard":"function isReadableImage(string $path): bool\n{\n    return is_file($path) && false !== @getimagesize($path);\n}","tryCatchPattern":"try { $image = new \\Phalcon\\Image\\Adapter\\Gd($path); } catch (\\Phalcon\\Image\\Exceptions\\ImageLoadFailed $e) { http_response_code(415); exit('Corrupt or unreadable image file');\n}","preventionTips":["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"],"tags":["php","phalcon","image","gd","file-upload"],"backgroundTag":"image-load-failed","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}