{"record":{"id":"8a0d82daa5325ea8","repo":"phacility/phabricator","slug":"unable-to-load-image-data-with-imagecreatefromstri","errorCode":null,"errorMessage":"Unable to load image data with imagecreatefromstring(): %s","messagePattern":"Unable to load image data with imagecreatefromstring\\(\\): (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/applications/files/transform/PhabricatorFileImageTransform.php","lineNumber":354,"sourceCode":"    if ($img_pixels > $max_pixels) {\n      throw new Exception(\n        pht(\n          'This image (with dimensions %spx x %spx) is too large to '.\n          'transform. The image has %s pixels, but transforms are limited '.\n          'to images with %s or fewer pixels.',\n          new PhutilNumber($width),\n          new PhutilNumber($height),\n          new PhutilNumber($img_pixels),\n          new PhutilNumber($max_pixels)));\n    }\n\n    $trap = new PhutilErrorTrap();\n    $image = @imagecreatefromstring($data);\n    $errors = $trap->getErrorsAsString();\n    $trap->destroy();\n\n    if ($image === false) {\n      throw new Exception(\n        pht(\n          'Unable to load image data with imagecreatefromstring(): %s',\n          $errors));\n    }\n\n    $this->image = $image;\n    return $this->image;\n  }\n\n  private function shouldUseImagemagick() {\n    if (!PhabricatorEnv::getEnvConfig('files.enable-imagemagick')) {\n      return false;\n    }\n\n    if ($this->file->getMimeType() != 'image/gif') {\n      return false;\n    }\n","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/files/transform/PhabricatorFileImageTransform.php#L336-L372","documentation":"imagecreatefromstring() returned false after the earlier pre-flights (getimagesize OK, dimensions positive, pixel count under the cap), meaning GD recognized the container but could not decode the pixel data. Typical causes: the format is compiled out of this GD build (e.g. WebP/AVIF on older GD), the data is truncated/corrupt beyond the header, or decoding exhausted memory. The trapped PHP warnings are included in the message.","triggerScenarios":"Transforming an image whose header parses but whose body GD cannot decode — WebP on a GD without WebP support, AVIF/XL formats GD never had, files truncated mid-transfer, or progressive/animated variants a particular GD mishandles.","commonSituations":"PHP built with limited GD format support (common on minimal/container images); browsers happily producing WebP/AVIF uploads while the server GD predates them; interrupted uploads that keep a valid header but lose the tail.","solutions":["Check GD capabilities: php -r 'print_r(gd_info());' — confirm the format (WebP etc.) is enabled; if not, install a PHP/GD build with it and restart php-fpm/phd.","Verify the file decodes locally (GD or an image viewer) and re-upload a non-truncated, mainstream-format copy (JPEG/PNG).","Catch this exception at the transform call and fall back to the original file; optionally restrict 'image' uploads to formats your GD supports."],"exampleFix":"# before: WebP upload, GD without webp support -> transform throws\n# after: confirm support, else reject/skip\nphp -r 'var_dump(gd_info()[\"WebP Support\"]);'\n# false => install php-gd with webp (e.g. newer distro package), restart php-fpm + phd","handlingStrategy":"try-catch","validationCode":"$info = @getimagesize($file->getURI());\n$known_good = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);\nif ($info === false || !in_array($info[2], $known_good, true)) {\n  return $file; // format this GD build may not decode; skip transform\n}","typeGuard":"function gdCanDecodeFormat(int $imagetype): bool {\n  $gd = gd_info();\n  switch ($imagetype) {\n    case IMAGETYPE_WEBP: return !empty($gd['WebP Support']);\n    case IMAGETYPE_JPEG: return !empty($gd['JPEG Support']);\n    case IMAGETYPE_PNG: return !empty($gd['PNG Support']);\n    case IMAGETYPE_GIF: return !empty($gd['GIF Support']);\n    default: return false;\n  }\n}","tryCatchPattern":"try {\n  $transformed = $file->applyTransform($xform);\n} catch (Exception $ex) {\n  phlog($ex);\n  $transformed = $file; // decode failed; serve original\n}","preventionTips":["Check gd_info() for the formats you intend to accept (WebP, AVIF support varies by PHP/GD build).","Limit uploads to JPEG/PNG/GIF unless you have verified your GD supports more.","Always keep an original-file fallback so one undecodable image does not break rendering."],"tags":["phabricator","image-transform","gd","unsupported-format","php"],"backgroundTag":"invalid-image-data","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}