3 abstract class PhabricatorFileImageTransform
extends PhabricatorFileTransform
{
12 * Get an estimate of the transformed dimensions of a file.
14 * @param PhabricatorFile File to transform.
15 * @return list<int, int>|null Width and height, if available.
17 public function getTransformedDimensions(PhabricatorFile
$file) {
21 public function canApplyTransform(PhabricatorFile
$file) {
22 if (!$file->isViewableImage()) {
26 if (!$file->isTransformableImage()) {
33 protected function willTransformFile(PhabricatorFile
$file) {
41 protected function getFileProperties() {
45 protected function applyCropAndScale(
52 // Figure out the effective destination width, height, and offsets.
53 $cpy_w = min($dst_w, $use_w);
54 $cpy_h = min($dst_h, $use_h);
56 // If we aren't scaling up, and are copying a very small source image,
57 // we're just going to center it in the destination image.
59 $cpy_w = min($cpy_w, $src_w);
60 $cpy_h = min($cpy_h, $src_h);
63 $off_x = ($dst_w - $cpy_w) / 2;
64 $off_y = ($dst_h - $cpy_h) / 2;
66 if ($this->shouldUseImagemagick()) {
68 $argv[] = '-coalesce';
70 $argv[] = $src_x.'x'.$src_y;
74 $argv[] = $dst_w.'x'.$dst_h;
76 $argv[] = $dst_w.'x'.$dst_h.'>';
79 $argv[] = '-bordercolor';
80 $argv[] = 'rgba(255, 255, 255, 0)';
82 $argv[] = $off_x.'x'.$off_y;
84 return $this->applyImagemagick($argv);
87 $src = $this->getImage();
88 $dst = $this->newEmptyImage($dst_w, $dst_h);
90 $trap = new PhutilErrorTrap();
91 $ok = @imagecopyresampled
(
98 $errors = $trap->getErrorsAsString();
104 'Failed to imagecopyresampled() image: %s',
108 $data = PhabricatorImageTransformer
::saveImageDataInAnyFormat(
110 $this->file
->getMimeType());
112 return $this->newFileFromData($data);
115 protected function applyImagemagick(array $argv) {
116 $tmp = new TempFile();
117 Filesystem
::writeFile($tmp, $this->getData());
119 $out = new TempFile();
121 $future = new ExecFuture('convert %s %Ls %s', $tmp, $argv, $out);
122 // Don't spend more than 60 seconds resizing; just fail if it takes longer
124 $future->setTimeout(60)->resolvex();
126 $data = Filesystem
::readFile($out);
128 return $this->newFileFromData($data);
133 * Create a new @{class:PhabricatorFile} from raw data.
135 * @param string Raw file data.
137 protected function newFileFromData($data) {
139 $name = $this->file
->getName();
141 $name = 'default.png';
146 'name' => $this->getTransformKey().'-'.$name,
149 $properties = $this->getFileProperties() +
$defaults;
151 return PhabricatorFile
::newFromFileData($data, $properties);
156 * Create a new image filled with transparent pixels.
158 * @param int Desired image width.
159 * @param int Desired image height.
160 * @return resource New image resource.
162 protected function newEmptyImage($w, $h) {
166 if (($w <= 0) ||
($h <= 0)) {
168 pht('Can not create an image with nonpositive dimensions.'));
171 $trap = new PhutilErrorTrap();
172 $img = @imagecreatetruecolor
($w, $h);
173 $errors = $trap->getErrorsAsString();
175 if ($img === false) {
178 'Unable to imagecreatetruecolor() a new empty image: %s',
182 $trap = new PhutilErrorTrap();
183 $ok = @imagesavealpha
($img, true);
184 $errors = $trap->getErrorsAsString();
189 'Unable to imagesavealpha() a new empty image: %s',
193 $trap = new PhutilErrorTrap();
194 $color = @imagecolorallocatealpha
($img, 255, 255, 255, 127);
195 $errors = $trap->getErrorsAsString();
197 if ($color === false) {
200 'Unable to imagecolorallocatealpha() a new empty image: %s',
204 $trap = new PhutilErrorTrap();
205 $ok = @imagefill
($img, 0, 0, $color);
206 $errors = $trap->getErrorsAsString();
211 'Unable to imagefill() a new empty image: %s',
220 * Get the pixel dimensions of the image being transformed.
222 * @return list<int, int> Width and height of the image.
224 protected function getImageDimensions() {
225 if ($this->imageX
=== null) {
226 $image = $this->getImage();
228 $trap = new PhutilErrorTrap();
229 $x = @imagesx
($image);
230 $y = @imagesy
($image);
231 $errors = $trap->getErrorsAsString();
234 if (($x === false) ||
($y === false) ||
($x <= 0) ||
($y <= 0)) {
237 'Unable to determine image dimensions with '.
238 'imagesx()/imagesy(): %s',
246 return array($this->imageX
, $this->imageY
);
251 * Get the raw file data for the image being transformed.
253 * @return string Raw file data.
255 protected function getData() {
256 if ($this->data
!== null) {
262 $max_size = (1024 * 1024 * 16);
263 $img_size = $file->getByteSize();
264 if ($img_size > $max_size) {
267 'This image is too large to transform. The transform limit is %s '.
268 'bytes, but the image size is %s bytes.',
269 new PhutilNumber($max_size),
270 new PhutilNumber($img_size)));
273 $data = $file->loadFileData();
280 * Get the GD image resource for the image being transformed.
282 * @return resource GD image resource.
284 protected function getImage() {
285 if ($this->image
!== null) {
289 if (!function_exists('imagecreatefromstring')) {
292 'Unable to transform image: the imagecreatefromstring() function '.
293 'is not available. Install or enable the "gd" extension for PHP.'));
296 $data = $this->getData();
297 $data = (string)$data;
299 // First, we're going to write the file to disk and use getimagesize()
300 // to determine its dimensions without actually loading the pixel data
301 // into memory. For very large images, we'll bail out.
303 // In particular, this defuses a resource exhaustion attack where the
304 // attacker uploads a 40,000 x 40,000 pixel PNGs of solid white. These
305 // kinds of files compress extremely well, but require a huge amount
306 // of memory and CPU to process.
308 $tmp = new TempFile();
309 Filesystem
::writeFile($tmp, $data);
310 $tmp_path = (string)$tmp;
312 $trap = new PhutilErrorTrap();
313 $info = @getimagesize
($tmp_path);
314 $errors = $trap->getErrorsAsString();
319 if ($info === false) {
322 'Unable to get image information with getimagesize(): %s',
326 list($width, $height) = $info;
327 if (($width <= 0) ||
($height <= 0)) {
330 'Unable to determine image width and height with getimagesize().'));
333 $max_pixels = (4096 * 4096);
334 $img_pixels = ($width * $height);
336 if ($img_pixels > $max_pixels) {
339 'This image (with dimensions %spx x %spx) is too large to '.
340 'transform. The image has %s pixels, but transforms are limited '.
341 'to images with %s or fewer pixels.',
342 new PhutilNumber($width),
343 new PhutilNumber($height),
344 new PhutilNumber($img_pixels),
345 new PhutilNumber($max_pixels)));
348 $trap = new PhutilErrorTrap();
349 $image = @imagecreatefromstring
($data);
350 $errors = $trap->getErrorsAsString();
353 if ($image === false) {
356 'Unable to load image data with imagecreatefromstring(): %s',
360 $this->image
= $image;
364 private function shouldUseImagemagick() {
365 if (!PhabricatorEnv
::getEnvConfig('files.enable-imagemagick')) {
369 if ($this->file
->getMimeType() != 'image/gif') {
373 // Don't try to preserve the animation in huge GIFs.
374 list($x, $y) = $this->getImageDimensions();
375 if (($x * $y) > (512 * 512)) {