@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement error-checked "preview" transforms

Summary: Ref T7707. These transforms have a single maximum dimension instead of fixed X and Y dimensions.

Test Plan: Transformed a bunch of files with different sizes/aspect ratios, got sensible results.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7707

Differential Revision: https://secure.phabricator.com/D12812

+52 -21
+4 -3
src/applications/files/transform/PhabricatorFileImageTransform.php
··· 31 31 protected function applyCropAndScale( 32 32 $dst_w, $dst_h, 33 33 $src_x, $src_y, 34 - $src_w, $src_h) { 34 + $src_w, $src_h, 35 + $use_w, $use_h) { 35 36 36 37 // Figure out the effective destination width, height, and offsets. We 37 38 // never want to scale images up, so if we're copying a very small source 38 39 // image we're just going to center it in the destination image. 39 - $cpy_w = min($dst_w, $src_w); 40 - $cpy_h = min($dst_h, $src_h); 40 + $cpy_w = min($dst_w, $src_w, $use_w); 41 + $cpy_h = min($dst_h, $src_h, $use_h); 41 42 $off_x = ($dst_w - $cpy_w) / 2; 42 43 $off_y = ($dst_h - $cpy_h) / 2; 43 44
+48 -18
src/applications/files/transform/PhabricatorFileThumbnailTransform.php
··· 59 59 } 60 60 61 61 public function applyTransform(PhabricatorFile $file) { 62 - $xformer = new PhabricatorImageTransformer(); 63 - if ($this->dstY === null) { 64 - return $xformer->executePreviewTransform($file, $this->dstX); 65 - } 66 - 67 62 $this->willTransformFile($file); 68 63 69 64 list($src_x, $src_y) = $this->getImageDimensions(); 70 65 $dst_x = $this->dstX; 71 66 $dst_y = $this->dstY; 72 67 73 - // Figure out how much we'd have to scale the image down along each 74 - // dimension to get the entire thing to fit. 75 - $scale_x = min(($dst_x / $src_x), 1); 76 - $scale_y = min(($dst_y / $src_y), 1); 68 + if ($dst_y === null) { 69 + // If we only have one dimension, it represents a maximum dimension. 70 + // The other dimension of the transform is scaled appropriately, except 71 + // that we never generate images with crazily extreme aspect ratios. 72 + if ($src_x < $src_y) { 73 + // This is a tall, narrow image. Use the maximum dimension for the 74 + // height and scale the width. 75 + $use_y = $dst_x; 76 + $dst_y = $dst_x; 77 + 78 + $use_x = $dst_y * ($src_x / $src_y); 79 + $dst_x = max($dst_y / 4, $use_x); 80 + } else { 81 + // This is a short, wide image. Use the maximum dimension for the width 82 + // and scale the height. 83 + $use_x = $dst_x; 77 84 78 - if ($scale_x > $scale_y) { 79 - // This image is relatively tall and narrow. We're going to crop off the 80 - // top and bottom. 85 + $use_y = $dst_x * ($src_y / $src_x); 86 + $dst_y = max($dst_x / 4, $use_y); 87 + } 88 + 89 + // In this mode, we always copy the entire source image. We may generate 90 + // margins in the output. 81 91 $copy_x = $src_x; 82 - $copy_y = min($src_y, $dst_y / $scale_x); 92 + $copy_y = $src_y; 83 93 } else { 84 - // This image is relatively short and wide. We're going to crop off the 85 - // left and right. 86 - $copy_x = min($src_x, $dst_x / $scale_y); 87 - $copy_y = $src_y; 94 + // Otherwise, both dimensions are fixed. Figure out how much we'd have to 95 + // scale the image down along each dimension to get the entire thing to 96 + // fit. 97 + $scale_x = min(($dst_x / $src_x), 1); 98 + $scale_y = min(($dst_y / $src_y), 1); 99 + 100 + if ($scale_x > $scale_y) { 101 + // This image is relatively tall and narrow. We're going to crop off the 102 + // top and bottom. 103 + $copy_x = $src_x; 104 + $copy_y = min($src_y, $dst_y / $scale_x); 105 + } else { 106 + // This image is relatively short and wide. We're going to crop off the 107 + // left and right. 108 + $copy_x = min($src_x, $dst_x / $scale_y); 109 + $copy_y = $src_y; 110 + } 111 + 112 + // In this mode, we always use the entire destination image. We may 113 + // crop the source input. 114 + $use_x = $dst_x; 115 + $use_y = $dst_y; 88 116 } 89 117 90 118 return $this->applyCropAndScale( ··· 93 121 ($src_x - $copy_x) / 2, 94 122 ($src_y - $copy_y) / 2, 95 123 $copy_x, 96 - $copy_y); 124 + $copy_y, 125 + $use_x, 126 + $use_y); 97 127 } 98 128 99 129 public function getDefaultTransform(PhabricatorFile $file) {