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

  1. Raise PHP's memory_limit (php.ini / php-fpm pool) for web and daemon processes — GD needs several bytes per pixel for both buffers.
  2. 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.
  3. 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

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


AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21). Data as JSON: /api/errors/99db143f0c89451f. Report an issue: GitHub.