Differential D13933 Diff 33634 src/applications/files/transform/PhabricatorFileThumbnailTransform.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/transform/PhabricatorFileThumbnailTransform.php
| <?php | <?php | ||||
| final class PhabricatorFileThumbnailTransform | final class PhabricatorFileThumbnailTransform | ||||
| extends PhabricatorFileImageTransform { | extends PhabricatorFileImageTransform { | ||||
| const TRANSFORM_PROFILE = 'profile'; | const TRANSFORM_PROFILE = 'profile'; | ||||
| const TRANSFORM_PINBOARD = 'pinboard'; | const TRANSFORM_PINBOARD = 'pinboard'; | ||||
| const TRANSFORM_THUMBGRID = 'thumbgrid'; | const TRANSFORM_THUMBGRID = 'thumbgrid'; | ||||
| const TRANSFORM_PREVIEW = 'preview'; | const TRANSFORM_PREVIEW = 'preview'; | ||||
| const TRANSFORM_MACRO = 'macro'; | |||||
| private $name; | private $name; | ||||
| private $key; | private $key; | ||||
| private $dstX; | private $dstX; | ||||
| private $dstY; | private $dstY; | ||||
| private $scaleUp; | private $scaleUp; | ||||
| private $preserveAspectRatio; | |||||
| public function setName($name) { | public function setName($name) { | ||||
| $this->name = $name; | $this->name = $name; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function setKey($key) { | public function setKey($key) { | ||||
| $this->key = $key; | $this->key = $key; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function setDimensions($x, $y) { | public function setDimensions($x, $y) { | ||||
| $this->dstX = $x; | $this->dstX = $x; | ||||
| $this->dstY = $y; | $this->dstY = $y; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function setScaleUp($scale) { | public function setScaleUp($scale) { | ||||
| $this->scaleUp = $scale; | $this->scaleUp = $scale; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function setPreserveAspectRatio($preserve_aspect_ratio) { | |||||
| $this->preserveAspectRatio = $preserve_aspect_ratio; | |||||
| return $this; | |||||
| } | |||||
| public function getTransformName() { | public function getTransformName() { | ||||
| return $this->name; | return $this->name; | ||||
| } | } | ||||
| public function getTransformKey() { | public function getTransformKey() { | ||||
| return $this->key; | return $this->key; | ||||
| } | } | ||||
| Show All 22 Lines | return array( | ||||
| id(new PhabricatorFileThumbnailTransform()) | id(new PhabricatorFileThumbnailTransform()) | ||||
| ->setName(pht('Thumbgrid (100px)')) | ->setName(pht('Thumbgrid (100px)')) | ||||
| ->setKey(self::TRANSFORM_THUMBGRID) | ->setKey(self::TRANSFORM_THUMBGRID) | ||||
| ->setDimensions(100, null), | ->setDimensions(100, null), | ||||
| id(new PhabricatorFileThumbnailTransform()) | id(new PhabricatorFileThumbnailTransform()) | ||||
| ->setName(pht('Preview (220px)')) | ->setName(pht('Preview (220px)')) | ||||
| ->setKey(self::TRANSFORM_PREVIEW) | ->setKey(self::TRANSFORM_PREVIEW) | ||||
| ->setDimensions(220, null), | ->setDimensions(220, null), | ||||
| id(new PhabricatorFileThumbnailTransform()) | |||||
| ->setName(pht('Macro (400px)')) | |||||
| ->setKey(self::TRANSFORM_MACRO) | |||||
| ->setDimensions(400, 400) | |||||
| ->setPreserveAspectRatio(true), | |||||
| ); | ); | ||||
| } | } | ||||
| public function applyTransform(PhabricatorFile $file) { | public function applyTransform(PhabricatorFile $file) { | ||||
| $this->willTransformFile($file); | $this->willTransformFile($file); | ||||
| list($src_x, $src_y) = $this->getImageDimensions(); | list($src_x, $src_y) = $this->getImageDimensions(); | ||||
| $dst_x = $this->dstX; | $dst_x = $this->dstX; | ||||
| $dst_y = $this->dstY; | $dst_y = $this->dstY; | ||||
| if ($this->preserveAspectRatio) { | |||||
| if ($src_x < $src_y && $src_y > $dst_y) { | |||||
| $ratio = $dst_y / $src_y; | |||||
| $dst_x = $src_x * $ratio; | |||||
| } else { | |||||
| $dst_y = null; | |||||
| } | |||||
| } | |||||
| $dimensions = $this->computeDimensions( | $dimensions = $this->computeDimensions( | ||||
| $src_x, | $src_x, | ||||
| $src_y, | $src_y, | ||||
| $dst_x, | $dst_x, | ||||
| $dst_y); | $dst_y); | ||||
| $copy_x = $dimensions['copy_x']; | $copy_x = $dimensions['copy_x']; | ||||
| $copy_y = $dimensions['copy_y']; | $copy_y = $dimensions['copy_y']; | ||||
| ▲ Show 20 Lines • Show All 132 Lines • Show Last 20 Lines | |||||