|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2011 Facebook, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +class PhabricatorFileTransformController extends PhabricatorFileController { |
| 20 | + |
| 21 | + private $transform; |
| 22 | + private $phid; |
| 23 | + |
| 24 | + public function willProcessRequest(array $data) { |
| 25 | + $this->transform = $data['transform']; |
| 26 | + $this->phid = $data['phid']; |
| 27 | + } |
| 28 | + |
| 29 | + public function processRequest() { |
| 30 | + |
| 31 | + $xform = id(new PhabricatorTransformedFile()) |
| 32 | + ->loadOneWhere( |
| 33 | + 'originalPHID = %s AND transform = %s', |
| 34 | + $this->phid, |
| 35 | + $this->transform); |
| 36 | + |
| 37 | + if ($xform) { |
| 38 | + return $this->buildTransformedFileResponse($xform); |
| 39 | + } |
| 40 | + |
| 41 | + $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $this->phid); |
| 42 | + if (!$file) { |
| 43 | + return new Aphront404Response(); |
| 44 | + } |
| 45 | + |
| 46 | + if (!$file->isViewableInBrowser()) { |
| 47 | + return new Aphront400Response(); |
| 48 | + } |
| 49 | + |
| 50 | + if (!$file->isTransformableImage()) { |
| 51 | + return new Aphront400Response(); |
| 52 | + } |
| 53 | + |
| 54 | + switch ($this->transform) { |
| 55 | + case 'thumb-160x120': |
| 56 | + $xformed_file = $this->executeThumbTransform($file, 160, 120); |
| 57 | + break; |
| 58 | + case 'thumb-60x45': |
| 59 | + $xformed_file = $this->executeThumbTransform($file, 60, 45); |
| 60 | + break; |
| 61 | + case 'profile-50x50': |
| 62 | + $xformed_file = $this->executeProfile50x50Transform($file); |
| 63 | + break; |
| 64 | + default: |
| 65 | + return new Aphront400Response(); |
| 66 | + } |
| 67 | + |
| 68 | + if (!$xformed_file) { |
| 69 | + return new Aphront400Response(); |
| 70 | + } |
| 71 | + |
| 72 | + $xform = new PhabricatorTransformedFile(); |
| 73 | + $xform->setOriginalPHID($this->phid); |
| 74 | + $xform->setTransform($this->transform); |
| 75 | + $xform->setTransformedPHID($xformed_file->getPHID()); |
| 76 | + $xform->save(); |
| 77 | + |
| 78 | + return $this->buildTransformedFileResponse($xform); |
| 79 | + } |
| 80 | + |
| 81 | + private function buildTransformedFileResponse( |
| 82 | + PhabricatorTransformedFile $xform) { |
| 83 | + |
| 84 | + // TODO: We could just delegate to the file view controller instead, |
| 85 | + // which would save the client a roundtrip, but is slightly more complex. |
| 86 | + return id(new AphrontRedirectResponse())->setURI( |
| 87 | + PhabricatorFileURI::getViewURIForPHID($xform->getTransformedPHID())); |
| 88 | + } |
| 89 | + |
| 90 | + private function executeProfile50x50Transform(PhabricatorFile $file) { |
| 91 | + $data = $file->loadFileData(); |
| 92 | + $jpeg = $this->crudelyScaleTo($data, 50, 50); |
| 93 | + |
| 94 | + return PhabricatorFile::newFromFileData($jpeg, array( |
| 95 | + 'name' => 'profile-'.$file->getName(), |
| 96 | + )); |
| 97 | + } |
| 98 | + |
| 99 | + private function executeThumbTransform(PhabricatorFile $file, $x, $y) { |
| 100 | + $data = $file->loadFileData(); |
| 101 | + $jpeg = $this->crudelyScaleTo($data, $x, $y); |
| 102 | + return PhabricatorFile::newFromFileData($jpeg, array( |
| 103 | + 'name' => 'thumb-'.$file->getName(), |
| 104 | + )); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Very crudely scale an image up or down to an exact size. |
| 109 | + */ |
| 110 | + private function crudelyScaleTo($data, $dx, $dy) { |
| 111 | + $src = imagecreatefromstring($data); |
| 112 | + $x = imagesx($src); |
| 113 | + $y = imagesy($src); |
| 114 | + |
| 115 | + $scale = min($x / $dx, $y / $dy); |
| 116 | + $dst = imagecreatetruecolor($dx, $dy); |
| 117 | + |
| 118 | + imagecopyresampled( |
| 119 | + $dst, |
| 120 | + $src, |
| 121 | + 0, 0, |
| 122 | + 0, 0, |
| 123 | + $dx, $dy, |
| 124 | + $scale * $dx, $scale * $dy); |
| 125 | + |
| 126 | + ob_start(); |
| 127 | + imagejpeg($dst); |
| 128 | + return ob_get_clean(); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments