{"record":{"id":"2e9970ffa5da3838","repo":"phacility/phabricator","slug":"unable-to-imagecreatetruecolor-a-new-empty-image","errorCode":null,"errorMessage":"Unable to imagecreatetruecolor() a new empty image: %s","messagePattern":"Unable to imagecreatetruecolor\\(\\) a new empty image: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/applications/files/transform/PhabricatorFileImageTransform.php","lineNumber":176,"sourceCode":"   * @param int Desired image width.\n   * @param int Desired image height.\n   * @return resource New image resource.\n   */\n  protected function newEmptyImage($w, $h) {\n    $w = (int)$w;\n    $h = (int)$h;\n\n    if (($w <= 0) || ($h <= 0)) {\n      throw new Exception(\n        pht('Can not create an image with nonpositive dimensions.'));\n    }\n\n    $trap = new PhutilErrorTrap();\n    $img = @imagecreatetruecolor($w, $h);\n    $errors = $trap->getErrorsAsString();\n    $trap->destroy();\n    if ($img === false) {\n      throw new Exception(\n        pht(\n          'Unable to imagecreatetruecolor() a new empty image: %s',\n          $errors));\n    }\n\n    $trap = new PhutilErrorTrap();\n    $ok = @imagesavealpha($img, true);\n    $errors = $trap->getErrorsAsString();\n    $trap->destroy();\n    if ($ok === false) {\n      throw new Exception(\n        pht(\n          'Unable to imagesavealpha() a new empty image: %s',\n          $errors));\n    }\n\n    $trap = new PhutilErrorTrap();\n    $color = @imagecolorallocatealpha($img, 255, 255, 255, 127);","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/files/transform/PhabricatorFileImageTransform.php#L158-L194","documentation":"The GD function imagecreatetruecolor($w, $h) returned false after newEmptyImage() had already validated that both dimensions are positive. GD fails this allocation almost exclusively when PHP cannot allocate the w*h truecolor buffer (memory_limit exhausted); the trapped PHP warnings are included in the message. Unlike the nonpositive-dimension error, this means the size was legal but the allocation was refused.","triggerScenarios":"Creating a transform canvas for large-but-legal dimensions (e.g. a 4096x4096-max source producing a large intermediate canvas) on a PHP process whose memory_limit is already mostly consumed by the decoded source bitmap.","commonSituations":"Shared-hosting or container PHP with a low memory_limit; transforms on 16MB files near the size cap where the source bitmap plus destination canvas exceed the limit; other extensions or loaded data leaving little headroom in the request.","solutions":["Raise memory_limit for the PHP web and daemon processes (GD needs ~4-5 bytes per pixel per buffer, source and destination).","Reduce the pixel budget: skip transforms for images near the 4096x4096 cap, or transform to a smaller target size first.","Wrap transform execution in try/catch and fall back to the original file so a single oversized image does not fail the whole request."],"exampleFix":"// php.ini or php-fpm pool\n; before\nmemory_limit = 128M\n; after\nmemory_limit = 512M","handlingStrategy":"fallback","validationCode":"$needed = $w * $h * 5 * 2; // rough bytes for src+dst buffers\nif ($needed > ini_get('memory_limit') converted-to-bytes - memory_get_usage()) {\n  // skip transform or reduce dimensions\n}","typeGuard":null,"tryCatchPattern":"try {\n  $transformed = $transform->execute($file);\n} catch (Exception $ex) {\n  $transformed = $file; // allocation failed; degrade to original\n}","preventionTips":["Raise memory_limit on transform-capable hosts; GD needs several bytes per pixel per buffer.","Bound transform sizes: cap source at 4096x4096 and target dimensions modestly.","Always provide an original-file fallback when a transform fails."],"tags":["phabricator","image-transform","gd","memory-limit","php"],"backgroundTag":"image-allocation-failed","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}