{"record":{"id":"99db143f0c89451f","repo":"phacility/phabricator","slug":"failed-to-imagecopyresampled-image-s","errorCode":null,"errorMessage":"Failed to imagecopyresampled() image: %s","messagePattern":"Failed to imagecopyresampled\\(\\) image: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/applications/files/transform/PhabricatorFileImageTransform.php","lineNumber":102,"sourceCode":"      return $this->applyImagemagick($argv);\n    }\n\n    $src = $this->getImage();\n    $dst = $this->newEmptyImage($dst_w, $dst_h);\n\n    $trap = new PhutilErrorTrap();\n    $ok = @imagecopyresampled(\n      $dst,\n      $src,\n      $off_x, $off_y,\n      $src_x, $src_y,\n      $cpy_w, $cpy_h,\n      $src_w, $src_h);\n    $errors = $trap->getErrorsAsString();\n    $trap->destroy();\n\n    if ($ok === false) {\n      throw new Exception(\n        pht(\n          'Failed to imagecopyresampled() image: %s',\n          $errors));\n    }\n\n    $data = PhabricatorImageTransformer::saveImageDataInAnyFormat(\n      $dst,\n      $this->file->getMimeType());\n\n    return $this->newFileFromData($data);\n  }\n\n  protected function applyImagemagick(array $argv) {\n    $tmp = new TempFile();\n    Filesystem::writeFile($tmp, $this->getData());\n\n    $out = new TempFile();\n","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/files/transform/PhabricatorFileImageTransform.php#L84-L120","documentation":"The GD function imagecopyresampled() returned false while PhabricatorFileImageTransform was copying/rescaling pixels into the destination canvas; the collected PHP warnings (via PhutilErrorTrap) are appended to the message. GD returns false when the operation cannot be performed — most often memory exhaustion while allocating the resampled rows, or an invalid source/destination resource.","triggerScenarios":"Executing any built-in image transform (thumbnails, profile-crops, the transform pipeline in PhabricatorFileImageTransform::applyTransform) on a file whose decoded bitmap is large relative to PHP's memory_limit, so the resample of e.g. 4000x3000 into a thumb exhausts memory mid-copy.","commonSituations":"Default PHP memory_limit (128M) with multi-megapixel JPEGs; a transform applied to a file that passed getimagesize() but decodes into a huge bitmap; GD built without proper support; concurrent transforms on a memory-constrained host.","solutions":["Raise PHP's memory_limit (php.ini / php-fpm pool) for web and daemon processes — GD needs several bytes per pixel for both buffers.","Verify the source file is a valid raster image (re-encode it or strip bogus metadata) and that its pixel count is under the 4096x4096 transform cap.","Catch the exception at the transform call site and fall back to serving/storing the original, untransformed file."],"exampleFix":"// before\n$transformed = $transform->execute($file);\n\n// after\ntry {\n  $transformed = $transform->execute($file);\n} catch (Exception $ex) {\n  phlog($ex);\n  $transformed = $file; // degrade gracefully to the original\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  $transformed = $transform->execute($file);\n} catch (Exception $ex) {\n  phlog(pht('Transform failed for %s: %s', $file->getPHID(), $ex->getMessage()));\n  $transformed = $file; // fall back to the original\n}","preventionTips":["Pre-check byte size (16MB cap) and pixel count (4096x4096 cap) before transforming.","Set memory_limit generously (512M+) on hosts that transform user images.","Treat transforms as best-effort: always have an original-file fallback path."],"tags":["phabricator","image-transform","gd","memory-limit","php"],"backgroundTag":"gd-image-operation-failed","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}