{"record":{"id":"64152ae8694043d8","repo":"phacility/phabricator","slug":"can-not-create-an-image-with-nonpositive-dimension","errorCode":null,"errorMessage":"Can not create an image with nonpositive dimensions.","messagePattern":"Can not create an image with nonpositive dimensions\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/applications/files/transform/PhabricatorFileImageTransform.php","lineNumber":167,"sourceCode":"    $properties = $this->getFileProperties() + $defaults;\n\n    return PhabricatorFile::newFromFileData($data, $properties);\n  }\n\n\n  /**\n   * Create a new image filled with transparent pixels.\n   *\n   * @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();","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/files/transform/PhabricatorFileImageTransform.php#L149-L185","documentation":"PhabricatorFileImageTransform::newEmptyImage($w, $h) casts both arguments to int and refuses to allocate a GD canvas when either is <= 0. A nonpositive dimension means the transform computed a degenerate output size (typically scaling a tiny image down so far that a side rounds to zero), and GD cannot create a 0-pixel image, so this fails fast with a clear message instead of a raw GD error.","triggerScenarios":"A transform whose computed destination width or height is 0 or negative — e.g. scaling a 1px-wide image to 1% scale, or a custom subclass passing user-controlled dimensions into newEmptyImage(); also any code path that passes null (casts to 0) or a negative offset.","commonSituations":"Custom PhabricatorFileImageTransform subclasses with arithmetic like floor($w * $scale) that yields 0 for small inputs; transforms configured with a dimension of 0 in config; division by a large scaling factor producing values < 1 that truncate to 0.","solutions":["Clamp computed dimensions to at least 1 before creating the canvas: $w = max(1, (int)$w); $h = max(1, (int)$h);","In custom transforms, reject or skip images whose source dimensions are smaller than the target scale factor rather than producing a 0-sized output.","Validate transform configuration so width/height/percent parameters cannot be zero or negative."],"exampleFix":"// before\n$dst = $this->newEmptyImage($s * $this->w, $s * $this->h); // can be < 1\n\n// after\n$w = max(1, (int)round($s * $this->w));\n$h = max(1, (int)round($s * $this->h));\n$dst = $this->newEmptyImage($w, $h);","handlingStrategy":"validation","validationCode":"$w = max(1, (int)round($w));\n$h = max(1, (int)round($h));\n// now safe to call newEmptyImage($w, $h)","typeGuard":"function isPositiveDimensions($w, $h): bool {\n  return ((int)$w > 0) && ((int)$h > 0);\n}","tryCatchPattern":null,"preventionTips":["In custom transforms, clamp every computed dimension to >= 1 before allocation.","Skip transforms when the source image is smaller than the target scale implies.","Validate transform configuration (width/height/percent) to reject zero or negative values."],"tags":["phabricator","image-transform","gd","dimensions","php"],"backgroundTag":"nonpositive-image-dimensions","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}