2011-06-26 17:37:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorImageTransformer {
|
|
|
|
|
|
|
|
public function executeThumbTransform(
|
|
|
|
PhabricatorFile $file,
|
|
|
|
$x,
|
|
|
|
$y) {
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
$image = $this->crudelyScaleTo($file, $x, $y);
|
2011-06-26 17:37:47 +02:00
|
|
|
|
|
|
|
return PhabricatorFile::newFromFileData(
|
|
|
|
$image,
|
|
|
|
array(
|
|
|
|
'name' => 'thumb-'.$file->getName(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function executeProfileTransform(
|
|
|
|
PhabricatorFile $file,
|
|
|
|
$x,
|
|
|
|
$min_y,
|
|
|
|
$max_y) {
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
$image = $this->crudelyCropTo($file, $x, $min_y, $max_y);
|
2011-06-26 17:37:47 +02:00
|
|
|
|
|
|
|
return PhabricatorFile::newFromFileData(
|
|
|
|
$image,
|
|
|
|
array(
|
|
|
|
'name' => 'profile-'.$file->getName(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
public function executePreviewTransform(
|
|
|
|
PhabricatorFile $file,
|
|
|
|
$size) {
|
|
|
|
|
|
|
|
$image = $this->generatePreview($file, $size);
|
|
|
|
|
|
|
|
return PhabricatorFile::newFromFileData(
|
|
|
|
$image,
|
|
|
|
array(
|
|
|
|
'name' => 'preview-'.$file->getName(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
private function crudelyCropTo(PhabricatorFile $file, $x, $min_y, $max_y) {
|
|
|
|
$data = $file->loadFileData();
|
2011-06-26 17:37:47 +02:00
|
|
|
$img = imagecreatefromstring($data);
|
|
|
|
$sx = imagesx($img);
|
|
|
|
$sy = imagesy($img);
|
|
|
|
|
|
|
|
$scaled_y = ($x / $sx) * $sy;
|
|
|
|
if ($scaled_y > $max_y) {
|
|
|
|
// This image is very tall and thin.
|
|
|
|
$scaled_y = $max_y;
|
|
|
|
} else if ($scaled_y < $min_y) {
|
|
|
|
// This image is very short and wide.
|
|
|
|
$scaled_y = $min_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
$img = $this->applyScaleTo(
|
|
|
|
$img,
|
|
|
|
$x,
|
|
|
|
$scaled_y);
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
return $this->saveImageDataInAnyFormat($img, $file->getMimeType());
|
2011-06-26 17:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Very crudely scale an image up or down to an exact size.
|
|
|
|
*/
|
2012-03-28 08:39:50 +02:00
|
|
|
private function crudelyScaleTo(PhabricatorFile $file, $dx, $dy) {
|
|
|
|
$data = $file->loadFileData();
|
2011-06-26 17:37:47 +02:00
|
|
|
$src = imagecreatefromstring($data);
|
|
|
|
|
|
|
|
$dst = $this->applyScaleTo($src, $dx, $dy);
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
|
2011-06-26 17:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function applyScaleTo($src, $dx, $dy) {
|
|
|
|
$x = imagesx($src);
|
|
|
|
$y = imagesy($src);
|
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
$scale = min(($dx / $x), ($dy / $y), 1);
|
|
|
|
|
2011-06-26 17:37:47 +02:00
|
|
|
$dst = imagecreatetruecolor($dx, $dy);
|
2012-08-13 21:31:13 +02:00
|
|
|
imagesavealpha($dst, true);
|
2012-10-08 22:26:10 +02:00
|
|
|
imagefill($dst, 0, 0, imagecolorallocatealpha($dst, 255, 255, 255, 127));
|
2011-06-26 17:37:47 +02:00
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
$sdx = $scale * $x;
|
|
|
|
$sdy = $scale * $y;
|
2011-12-17 05:01:38 +01:00
|
|
|
|
2011-06-26 17:37:47 +02:00
|
|
|
imagecopyresampled(
|
|
|
|
$dst,
|
|
|
|
$src,
|
2012-10-08 22:26:10 +02:00
|
|
|
($dx - $sdx) / 2, ($dy - $sdy) / 2,
|
2011-06-26 17:37:47 +02:00
|
|
|
0, 0,
|
2012-10-08 22:26:10 +02:00
|
|
|
$sdx, $sdy,
|
|
|
|
$x, $y);
|
2011-06-26 17:37:47 +02:00
|
|
|
|
|
|
|
return $dst;
|
|
|
|
}
|
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
private function generatePreview(PhabricatorFile $file, $size) {
|
|
|
|
$data = $file->loadFileData();
|
|
|
|
$src = imagecreatefromstring($data);
|
|
|
|
|
|
|
|
$x = imagesx($src);
|
|
|
|
$y = imagesy($src);
|
|
|
|
|
|
|
|
$scale = min($size / $x, $size / $y, 1);
|
|
|
|
|
|
|
|
$dx = max($size / 4, $scale * $x);
|
|
|
|
$dy = max($size / 4, $scale * $y);
|
|
|
|
|
|
|
|
$dst = imagecreatetruecolor($dx, $dy);
|
|
|
|
imagesavealpha($dst, true);
|
|
|
|
imagefill($dst, 0, 0, imagecolorallocatealpha($dst, 255, 255, 255, 127));
|
|
|
|
|
|
|
|
$sdx = $scale * $x;
|
|
|
|
$sdy = $scale * $y;
|
|
|
|
|
|
|
|
imagecopyresampled(
|
|
|
|
$dst,
|
|
|
|
$src,
|
|
|
|
($dx - $sdx) / 2, ($dy - $sdy) / 2,
|
|
|
|
0, 0,
|
|
|
|
$sdx, $sdy,
|
|
|
|
$x, $y);
|
|
|
|
|
|
|
|
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
|
|
|
|
}
|
|
|
|
|
2012-03-28 08:39:50 +02:00
|
|
|
private function saveImageDataInAnyFormat($data, $preferred_mime = '') {
|
|
|
|
switch ($preferred_mime) {
|
|
|
|
case 'image/gif': // GIF doesn't support true color.
|
|
|
|
case 'image/png':
|
|
|
|
if (function_exists('imagepng')) {
|
|
|
|
ob_start();
|
|
|
|
imagepng($data);
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-26 17:37:47 +02:00
|
|
|
$img = null;
|
|
|
|
|
|
|
|
if (function_exists('imagejpeg')) {
|
|
|
|
ob_start();
|
|
|
|
imagejpeg($data);
|
|
|
|
$img = ob_get_clean();
|
|
|
|
} else if (function_exists('imagepng')) {
|
|
|
|
ob_start();
|
|
|
|
imagepng($data);
|
|
|
|
$img = ob_get_clean();
|
|
|
|
} else if (function_exists('imagegif')) {
|
|
|
|
ob_start();
|
|
|
|
imagegif($data);
|
|
|
|
$img = ob_get_clean();
|
|
|
|
} else {
|
|
|
|
throw new Exception("No image generation functions exist!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $img;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|