@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.

Convert "preview" image transforms to new pathway

Summary: Ref T7707. Move the 220px (file uploads) and 100px (Pholio thumbgrid) previews over to the new stuff.

Test Plan: Uploaded a bunch of images to remarkup and Pholio; they generated reasonable results in the web UI.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7707

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

+118 -121
-70
src/applications/files/PhabricatorImageTransformer.php
··· 51 51 )); 52 52 } 53 53 54 - public function executePreviewTransform( 55 - PhabricatorFile $file, 56 - $size) { 57 - 58 - $image = $this->generatePreview($file, $size); 59 - 60 - return PhabricatorFile::newFromFileData( 61 - $image, 62 - array( 63 - 'name' => 'preview-'.$file->getName(), 64 - 'canCDN' => true, 65 - )); 66 - } 67 - 68 54 public function executeConpherenceTransform( 69 55 PhabricatorFile $file, 70 56 $top, ··· 188 174 189 175 } 190 176 191 - public static function getPreviewDimensions(PhabricatorFile $file, $size) { 192 - $metadata = $file->getMetadata(); 193 - $x = idx($metadata, PhabricatorFile::METADATA_IMAGE_WIDTH); 194 - $y = idx($metadata, PhabricatorFile::METADATA_IMAGE_HEIGHT); 195 - 196 - if (!$x || !$y) { 197 - $data = $file->loadFileData(); 198 - $src = imagecreatefromstring($data); 199 - 200 - $x = imagesx($src); 201 - $y = imagesy($src); 202 - } 203 - 204 - $scale = min($size / $x, $size / $y, 1); 205 - 206 - $dx = max($size / 4, $scale * $x); 207 - $dy = max($size / 4, $scale * $y); 208 - 209 - $sdx = $scale * $x; 210 - $sdy = $scale * $y; 211 - 212 - return array( 213 - 'x' => $x, 214 - 'y' => $y, 215 - 'dx' => $dx, 216 - 'dy' => $dy, 217 - 'sdx' => $sdx, 218 - 'sdy' => $sdy, 219 - ); 220 - } 221 - 222 177 public static function getScaleForCrop( 223 178 PhabricatorFile $file, 224 179 $des_width, ··· 239 194 } 240 195 241 196 return $scale; 242 - } 243 - 244 - private function generatePreview(PhabricatorFile $file, $size) { 245 - $data = $file->loadFileData(); 246 - $src = imagecreatefromstring($data); 247 - 248 - $dimensions = self::getPreviewDimensions($file, $size); 249 - $x = $dimensions['x']; 250 - $y = $dimensions['y']; 251 - $dx = $dimensions['dx']; 252 - $dy = $dimensions['dy']; 253 - $sdx = $dimensions['sdx']; 254 - $sdy = $dimensions['sdy']; 255 - 256 - $dst = $this->getBlankDestinationFile($dx, $dy); 257 - 258 - imagecopyresampled( 259 - $dst, 260 - $src, 261 - ($dx - $sdx) / 2, ($dy - $sdy) / 2, 262 - 0, 0, 263 - $sdx, $sdy, 264 - $x, $y); 265 - 266 - return self::saveImageDataInAnyFormat($dst, $file->getMimeType()); 267 197 } 268 198 269 199 private function applyMemeToFile(
-14
src/applications/files/controller/PhabricatorFileTransformController.php
··· 85 85 case 'thumb-280x210': 86 86 $xformed_file = $this->executeThumbTransform($file, 280, 210); 87 87 break; 88 - case 'preview-100': 89 - $xformed_file = $this->executePreviewTransform($file, 100); 90 - break; 91 - case 'preview-220': 92 - $xformed_file = $this->executePreviewTransform($file, 220); 93 - break; 94 88 default: 95 89 return new Aphront400Response(); 96 90 } ··· 132 126 case 'thumb-280x210': 133 127 $suffix = '280x210'; 134 128 break; 135 - case 'preview-100': 136 - $suffix = '.p100'; 137 - break; 138 129 default: 139 130 throw new Exception('Unsupported transformation type!'); 140 131 } ··· 161 152 // which would save the client a roundtrip, but is slightly more complex. 162 153 163 154 return $file->getRedirectResponse(); 164 - } 165 - 166 - private function executePreviewTransform(PhabricatorFile $file, $size) { 167 - $xformer = new PhabricatorImageTransformer(); 168 - return $xformer->executePreviewTransform($file, $size); 169 155 } 170 156 171 157 private function executeThumbTransform(PhabricatorFile $file, $x, $y) {
+11 -5
src/applications/files/markup/PhabricatorEmbedFileRemarkupRule.php
··· 107 107 break; 108 108 case 'thumb': 109 109 default: 110 - $attrs['src'] = $file->getPreview220URI(); 111 - $dimensions = 112 - PhabricatorImageTransformer::getPreviewDimensions($file, 220); 113 - $attrs['width'] = $dimensions['dx']; 114 - $attrs['height'] = $dimensions['dy']; 110 + $preview_key = PhabricatorFileThumbnailTransform::TRANSFORM_PREVIEW; 111 + $xform = PhabricatorFileTransform::getTransformByKey($preview_key); 112 + $attrs['src'] = $file->getURIForTransform($xform); 113 + 114 + $dimensions = $xform->getTransformedDimensions($file); 115 + if ($dimensions) { 116 + list($x, $y) = $dimensions; 117 + $attrs['width'] = $x; 118 + $attrs['height'] = $y; 119 + } 120 + 115 121 $image_class = 'phabricator-remarkup-embed-image'; 116 122 break; 117 123 }
-8
src/applications/files/storage/PhabricatorFile.php
··· 788 788 return $this->getTransformedURI('thumb-profile'); 789 789 } 790 790 791 - public function getPreview100URI() { 792 - return $this->getTransformedURI('preview-100'); 793 - } 794 - 795 - public function getPreview220URI() { 796 - return $this->getTransformedURI('preview-220'); 797 - } 798 - 799 791 public function getThumb280x210URI() { 800 792 return $this->getTransformedURI('thumb-280x210'); 801 793 }
+10
src/applications/files/transform/PhabricatorFileImageTransform.php
··· 8 8 private $imageX; 9 9 private $imageY; 10 10 11 + /** 12 + * Get an estimate of the transformed dimensions of a file. 13 + * 14 + * @param PhabricatorFile File to transform. 15 + * @return list<int, int>|null Width and height, if available. 16 + */ 17 + public function getTransformedDimensions(PhabricatorFile $file) { 18 + return null; 19 + } 20 + 11 21 public function canApplyTransform(PhabricatorFile $file) { 12 22 if (!$file->isViewableImage()) { 13 23 return false;
+62 -9
src/applications/files/transform/PhabricatorFileThumbnailTransform.php
··· 65 65 $dst_x = $this->dstX; 66 66 $dst_y = $this->dstY; 67 67 68 + $dimensions = $this->computeDimensions( 69 + $src_x, 70 + $src_y, 71 + $dst_x, 72 + $dst_y); 73 + 74 + $copy_x = $dimensions['copy_x']; 75 + $copy_y = $dimensions['copy_y']; 76 + $use_x = $dimensions['use_x']; 77 + $use_y = $dimensions['use_y']; 78 + $dst_x = $dimensions['dst_x']; 79 + $dst_y = $dimensions['dst_y']; 80 + 81 + return $this->applyCropAndScale( 82 + $dst_x, 83 + $dst_y, 84 + ($src_x - $copy_x) / 2, 85 + ($src_y - $copy_y) / 2, 86 + $copy_x, 87 + $copy_y, 88 + $use_x, 89 + $use_y); 90 + } 91 + 92 + 93 + public function getTransformedDimensions(PhabricatorFile $file) { 94 + $dst_x = $this->dstX; 95 + $dst_y = $this->dstY; 96 + 97 + // If this is transform has fixed dimensions, we can trivially predict 98 + // the dimensions of the transformed file. 99 + if ($dst_y !== null) { 100 + return array($dst_x, $dst_y); 101 + } 102 + 103 + $src_x = $file->getImageWidth(); 104 + $src_y = $file->getImageHeight(); 105 + 106 + if (!$src_x || !$src_y) { 107 + return null; 108 + } 109 + 110 + $dimensions = $this->computeDimensions( 111 + $src_x, 112 + $src_y, 113 + $dst_x, 114 + $dst_y); 115 + 116 + return array($dimensions['dst_x'], $dimensions['dst_y']); 117 + } 118 + 119 + 120 + private function computeDimensions($src_x, $src_y, $dst_x, $dst_y) { 68 121 if ($dst_y === null) { 69 122 // If we only have one dimension, it represents a maximum dimension. 70 123 // The other dimension of the transform is scaled appropriately, except ··· 115 168 $use_y = $dst_y; 116 169 } 117 170 118 - return $this->applyCropAndScale( 119 - $dst_x, 120 - $dst_y, 121 - ($src_x - $copy_x) / 2, 122 - ($src_y - $copy_y) / 2, 123 - $copy_x, 124 - $copy_y, 125 - $use_x, 126 - $use_y); 171 + return array( 172 + 'copy_x' => $copy_x, 173 + 'copy_y' => $copy_y, 174 + 'use_x' => $use_x, 175 + 'use_y' => $use_y, 176 + 'dst_x' => $dst_x, 177 + 'dst_y' => $dst_y, 178 + ); 127 179 } 180 + 128 181 129 182 public function getDefaultTransform(PhabricatorFile $file) { 130 183 $x = (int)$this->dstX;
+14
src/applications/files/transform/PhabricatorFileTransform.php
··· 45 45 return $map; 46 46 } 47 47 48 + public static function getTransformByKey($key) { 49 + $all = self::getAllTransforms(); 50 + 51 + $xform = idx($all, $key); 52 + if (!$xform) { 53 + throw new Exception( 54 + pht( 55 + 'No file transform with key "%s" exists.', 56 + $key)); 57 + } 58 + 59 + return $xform; 60 + } 61 + 48 62 }
+21 -15
src/applications/pholio/view/PholioMockThumbGridView.php
··· 114 114 private function renderThumbnail(PholioImage $image) { 115 115 $thumbfile = $image->getFile(); 116 116 117 + $preview_key = PhabricatorFileThumbnailTransform::TRANSFORM_THUMBGRID; 118 + $xform = PhabricatorFileTransform::getTransformByKey($preview_key); 119 + 120 + $attributes = array( 121 + 'class' => 'pholio-mock-thumb-grid-image', 122 + 'src' => $thumbfile->getURIForTransform($xform), 123 + ); 124 + 117 125 if ($image->getFile()->isViewableImage()) { 118 - $dimensions = PhabricatorImageTransformer::getPreviewDimensions( 119 - $thumbfile, 120 - 100); 126 + $dimensions = $xform->getTransformedDimensions($thumbfile); 127 + if ($dimensions) { 128 + list($x, $y) = $dimensions; 129 + $attributes += array( 130 + 'width' => $x, 131 + 'height' => $y, 132 + 'style' => 'top: '.floor((100 - $y) / 2).'px', 133 + ); 134 + } 121 135 } else { 122 136 // If this is a PDF or a text file or something, we'll end up using a 123 137 // generic thumbnail which is always sized correctly. 124 - $dimensions = array( 125 - 'sdx' => 100, 126 - 'sdy' => 100, 138 + $attributes += array( 139 + 'width' => 100, 140 + 'height' => 100, 127 141 ); 128 142 } 129 143 130 - $tag = phutil_tag( 131 - 'img', 132 - array( 133 - 'width' => $dimensions['sdx'], 134 - 'height' => $dimensions['sdy'], 135 - 'src' => $thumbfile->getPreview100URI(), 136 - 'class' => 'pholio-mock-thumb-grid-image', 137 - 'style' => 'top: '.floor((100 - $dimensions['sdy'] ) / 2).'px', 138 - )); 144 + $tag = phutil_tag('img', $attributes); 139 145 140 146 $classes = array('pholio-mock-thumb-grid-item'); 141 147 if ($image->getIsObsolete()) {
webroot/rsrc/image/icon/fatcow/thumbnails/default.p100.png

This is a binary file and will not be displayed.

webroot/rsrc/image/icon/fatcow/thumbnails/image.p100.png

This is a binary file and will not be displayed.

webroot/rsrc/image/icon/fatcow/thumbnails/pdf.p100.png

This is a binary file and will not be displayed.

webroot/rsrc/image/icon/fatcow/thumbnails/zip.p100.png

This is a binary file and will not be displayed.