phacility/phabricator · error · Exception
Failed to imagecopyresampled() image: %s
Error message
Failed to imagecopyresampled() image: %s
What it means
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.
Source
Thrown at src/applications/files/transform/PhabricatorFileImageTransform.php:102
return $this->applyImagemagick($argv);
}
$src = $this->getImage();
$dst = $this->newEmptyImage($dst_w, $dst_h);
$trap = new PhutilErrorTrap();
$ok = @imagecopyresampled(
$dst,
$src,
$off_x, $off_y,
$src_x, $src_y,
$cpy_w, $cpy_h,
$src_w, $src_h);
$errors = $trap->getErrorsAsString();
$trap->destroy();
if ($ok === false) {
throw new Exception(
pht(
'Failed to imagecopyresampled() image: %s',
$errors));
}
$data = PhabricatorImageTransformer::saveImageDataInAnyFormat(
$dst,
$this->file->getMimeType());
return $this->newFileFromData($data);
}
protected function applyImagemagick(array $argv) {
$tmp = new TempFile();
Filesystem::writeFile($tmp, $this->getData());
$out = new TempFile();
View on GitHub (pinned to 5720a38cfe)
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.
Example fix
// before
$transformed = $transform->execute($file);
// after
try {
$transformed = $transform->execute($file);
} catch (Exception $ex) {
phlog($ex);
$transformed = $file; // degrade gracefully to the original
} Defensive patterns
Strategy: try-catch
Try / catch
try {
$transformed = $transform->execute($file);
} catch (Exception $ex) {
phlog(pht('Transform failed for %s: %s', $file->getPHID(), $ex->getMessage()));
$transformed = $file; // fall back to the original
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to imagecreatetruecolor() a new empty image: %s
- Can not create an image with nonpositive dimensions.
- Unable to imagesavealpha() a new empty image: %s
- Unable to imagecolorallocatealpha() a new empty image: %s
- Unable to imagefill() a new empty image: %s
AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21).
Data as JSON: /api/errors/99db143f0c89451f.
Report an issue: GitHub.