@@ -43,556 +43,556 @@ discard block |
||
| 43 | 43 | * Class for basic image manipulation |
| 44 | 44 | */ |
| 45 | 45 | class OC_Image implements \OCP\IImage { |
| 46 | - /** @var false|resource */ |
|
| 47 | - protected $resource = false; // tmp resource. |
|
| 48 | - /** @var int */ |
|
| 49 | - protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $mimeType = 'image/png'; // Default to png |
|
| 52 | - /** @var int */ |
|
| 53 | - protected $bitDepth = 24; |
|
| 54 | - /** @var null|string */ |
|
| 55 | - protected $filePath = null; |
|
| 56 | - /** @var finfo */ |
|
| 57 | - private $fileInfo; |
|
| 58 | - /** @var \OCP\ILogger */ |
|
| 59 | - private $logger; |
|
| 60 | - /** @var \OCP\IConfig */ |
|
| 61 | - private $config; |
|
| 62 | - /** @var array */ |
|
| 63 | - private $exif; |
|
| 46 | + /** @var false|resource */ |
|
| 47 | + protected $resource = false; // tmp resource. |
|
| 48 | + /** @var int */ |
|
| 49 | + protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $mimeType = 'image/png'; // Default to png |
|
| 52 | + /** @var int */ |
|
| 53 | + protected $bitDepth = 24; |
|
| 54 | + /** @var null|string */ |
|
| 55 | + protected $filePath = null; |
|
| 56 | + /** @var finfo */ |
|
| 57 | + private $fileInfo; |
|
| 58 | + /** @var \OCP\ILogger */ |
|
| 59 | + private $logger; |
|
| 60 | + /** @var \OCP\IConfig */ |
|
| 61 | + private $config; |
|
| 62 | + /** @var array */ |
|
| 63 | + private $exif; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Constructor. |
|
| 67 | - * |
|
| 68 | - * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | - * an imagecreate* function. |
|
| 70 | - * @param \OCP\ILogger $logger |
|
| 71 | - * @param \OCP\IConfig $config |
|
| 72 | - * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | - */ |
|
| 74 | - public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - if ($logger === null) { |
|
| 77 | - $this->logger = \OC::$server->getLogger(); |
|
| 78 | - } |
|
| 79 | - $this->config = $config; |
|
| 80 | - if ($config === null) { |
|
| 81 | - $this->config = \OC::$server->getConfig(); |
|
| 82 | - } |
|
| 65 | + /** |
|
| 66 | + * Constructor. |
|
| 67 | + * |
|
| 68 | + * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | + * an imagecreate* function. |
|
| 70 | + * @param \OCP\ILogger $logger |
|
| 71 | + * @param \OCP\IConfig $config |
|
| 72 | + * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | + */ |
|
| 74 | + public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + if ($logger === null) { |
|
| 77 | + $this->logger = \OC::$server->getLogger(); |
|
| 78 | + } |
|
| 79 | + $this->config = $config; |
|
| 80 | + if ($config === null) { |
|
| 81 | + $this->config = \OC::$server->getConfig(); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | - $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | - } |
|
| 84 | + if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | + $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - if ($imageRef !== null) { |
|
| 89 | - throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | - } |
|
| 91 | - } |
|
| 88 | + if ($imageRef !== null) { |
|
| 89 | + throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Determine whether the object contains an image resource. |
|
| 95 | - * |
|
| 96 | - * @return bool |
|
| 97 | - */ |
|
| 98 | - public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | - return is_resource($this->resource); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Determine whether the object contains an image resource. |
|
| 95 | + * |
|
| 96 | + * @return bool |
|
| 97 | + */ |
|
| 98 | + public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | + return is_resource($this->resource); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function mimeType() { |
|
| 108 | - return $this->valid() ? $this->mimeType : ''; |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function mimeType() { |
|
| 108 | + return $this->valid() ? $this->mimeType : ''; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | - * |
|
| 114 | - * @return int |
|
| 115 | - */ |
|
| 116 | - public function width() { |
|
| 117 | - return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | - } |
|
| 111 | + /** |
|
| 112 | + * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | + * |
|
| 114 | + * @return int |
|
| 115 | + */ |
|
| 116 | + public function width() { |
|
| 117 | + return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | - * |
|
| 123 | - * @return int |
|
| 124 | - */ |
|
| 125 | - public function height() { |
|
| 126 | - return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | + * |
|
| 123 | + * @return int |
|
| 124 | + */ |
|
| 125 | + public function height() { |
|
| 126 | + return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Returns the width when the image orientation is top-left. |
|
| 131 | - * |
|
| 132 | - * @return int |
|
| 133 | - */ |
|
| 134 | - public function widthTopLeft() { |
|
| 135 | - $o = $this->getOrientation(); |
|
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | - switch ($o) { |
|
| 138 | - case -1: |
|
| 139 | - case 1: |
|
| 140 | - case 2: // Not tested |
|
| 141 | - case 3: |
|
| 142 | - case 4: // Not tested |
|
| 143 | - return $this->width(); |
|
| 144 | - case 5: // Not tested |
|
| 145 | - case 6: |
|
| 146 | - case 7: // Not tested |
|
| 147 | - case 8: |
|
| 148 | - return $this->height(); |
|
| 149 | - } |
|
| 150 | - return $this->width(); |
|
| 151 | - } |
|
| 129 | + /** |
|
| 130 | + * Returns the width when the image orientation is top-left. |
|
| 131 | + * |
|
| 132 | + * @return int |
|
| 133 | + */ |
|
| 134 | + public function widthTopLeft() { |
|
| 135 | + $o = $this->getOrientation(); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | + switch ($o) { |
|
| 138 | + case -1: |
|
| 139 | + case 1: |
|
| 140 | + case 2: // Not tested |
|
| 141 | + case 3: |
|
| 142 | + case 4: // Not tested |
|
| 143 | + return $this->width(); |
|
| 144 | + case 5: // Not tested |
|
| 145 | + case 6: |
|
| 146 | + case 7: // Not tested |
|
| 147 | + case 8: |
|
| 148 | + return $this->height(); |
|
| 149 | + } |
|
| 150 | + return $this->width(); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Returns the height when the image orientation is top-left. |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public function heightTopLeft() { |
|
| 159 | - $o = $this->getOrientation(); |
|
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | - switch ($o) { |
|
| 162 | - case -1: |
|
| 163 | - case 1: |
|
| 164 | - case 2: // Not tested |
|
| 165 | - case 3: |
|
| 166 | - case 4: // Not tested |
|
| 167 | - return $this->height(); |
|
| 168 | - case 5: // Not tested |
|
| 169 | - case 6: |
|
| 170 | - case 7: // Not tested |
|
| 171 | - case 8: |
|
| 172 | - return $this->width(); |
|
| 173 | - } |
|
| 174 | - return $this->height(); |
|
| 175 | - } |
|
| 153 | + /** |
|
| 154 | + * Returns the height when the image orientation is top-left. |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public function heightTopLeft() { |
|
| 159 | + $o = $this->getOrientation(); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | + switch ($o) { |
|
| 162 | + case -1: |
|
| 163 | + case 1: |
|
| 164 | + case 2: // Not tested |
|
| 165 | + case 3: |
|
| 166 | + case 4: // Not tested |
|
| 167 | + return $this->height(); |
|
| 168 | + case 5: // Not tested |
|
| 169 | + case 6: |
|
| 170 | + case 7: // Not tested |
|
| 171 | + case 8: |
|
| 172 | + return $this->width(); |
|
| 173 | + } |
|
| 174 | + return $this->height(); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Outputs the image. |
|
| 179 | - * |
|
| 180 | - * @param string $mimeType |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function show($mimeType = null) { |
|
| 184 | - if ($mimeType === null) { |
|
| 185 | - $mimeType = $this->mimeType(); |
|
| 186 | - } |
|
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 188 | - return $this->_output(null, $mimeType); |
|
| 189 | - } |
|
| 177 | + /** |
|
| 178 | + * Outputs the image. |
|
| 179 | + * |
|
| 180 | + * @param string $mimeType |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function show($mimeType = null) { |
|
| 184 | + if ($mimeType === null) { |
|
| 185 | + $mimeType = $this->mimeType(); |
|
| 186 | + } |
|
| 187 | + header('Content-Type: ' . $mimeType); |
|
| 188 | + return $this->_output(null, $mimeType); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Saves the image. |
|
| 193 | - * |
|
| 194 | - * @param string $filePath |
|
| 195 | - * @param string $mimeType |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 191 | + /** |
|
| 192 | + * Saves the image. |
|
| 193 | + * |
|
| 194 | + * @param string $filePath |
|
| 195 | + * @param string $mimeType |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | 198 | |
| 199 | - public function save($filePath = null, $mimeType = null) { |
|
| 200 | - if ($mimeType === null) { |
|
| 201 | - $mimeType = $this->mimeType(); |
|
| 202 | - } |
|
| 203 | - if ($filePath === null) { |
|
| 204 | - if ($this->filePath === null) { |
|
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | - return false; |
|
| 207 | - } else { |
|
| 208 | - $filePath = $this->filePath; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - return $this->_output($filePath, $mimeType); |
|
| 212 | - } |
|
| 199 | + public function save($filePath = null, $mimeType = null) { |
|
| 200 | + if ($mimeType === null) { |
|
| 201 | + $mimeType = $this->mimeType(); |
|
| 202 | + } |
|
| 203 | + if ($filePath === null) { |
|
| 204 | + if ($this->filePath === null) { |
|
| 205 | + $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | + return false; |
|
| 207 | + } else { |
|
| 208 | + $filePath = $this->filePath; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + return $this->_output($filePath, $mimeType); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Outputs/saves the image. |
|
| 216 | - * |
|
| 217 | - * @param string $filePath |
|
| 218 | - * @param string $mimeType |
|
| 219 | - * @return bool |
|
| 220 | - * @throws Exception |
|
| 221 | - */ |
|
| 222 | - private function _output($filePath = null, $mimeType = null) { |
|
| 223 | - if ($filePath) { |
|
| 224 | - if (!file_exists(dirname($filePath))) { |
|
| 225 | - mkdir(dirname($filePath), 0777, true); |
|
| 226 | - } |
|
| 227 | - $isWritable = is_writable(dirname($filePath)); |
|
| 228 | - if (!$isWritable) { |
|
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | - return false; |
|
| 231 | - } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - if (!$this->valid()) { |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 214 | + /** |
|
| 215 | + * Outputs/saves the image. |
|
| 216 | + * |
|
| 217 | + * @param string $filePath |
|
| 218 | + * @param string $mimeType |
|
| 219 | + * @return bool |
|
| 220 | + * @throws Exception |
|
| 221 | + */ |
|
| 222 | + private function _output($filePath = null, $mimeType = null) { |
|
| 223 | + if ($filePath) { |
|
| 224 | + if (!file_exists(dirname($filePath))) { |
|
| 225 | + mkdir(dirname($filePath), 0777, true); |
|
| 226 | + } |
|
| 227 | + $isWritable = is_writable(dirname($filePath)); |
|
| 228 | + if (!$isWritable) { |
|
| 229 | + $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | + return false; |
|
| 231 | + } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | + $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + if (!$this->valid()) { |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - $imageType = $this->imageType; |
|
| 241 | - if ($mimeType !== null) { |
|
| 242 | - switch ($mimeType) { |
|
| 243 | - case 'image/gif': |
|
| 244 | - $imageType = IMAGETYPE_GIF; |
|
| 245 | - break; |
|
| 246 | - case 'image/jpeg': |
|
| 247 | - $imageType = IMAGETYPE_JPEG; |
|
| 248 | - break; |
|
| 249 | - case 'image/png': |
|
| 250 | - $imageType = IMAGETYPE_PNG; |
|
| 251 | - break; |
|
| 252 | - case 'image/x-xbitmap': |
|
| 253 | - $imageType = IMAGETYPE_XBM; |
|
| 254 | - break; |
|
| 255 | - case 'image/bmp': |
|
| 256 | - case 'image/x-ms-bmp': |
|
| 257 | - $imageType = IMAGETYPE_BMP; |
|
| 258 | - break; |
|
| 259 | - default: |
|
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | - } |
|
| 262 | - } |
|
| 240 | + $imageType = $this->imageType; |
|
| 241 | + if ($mimeType !== null) { |
|
| 242 | + switch ($mimeType) { |
|
| 243 | + case 'image/gif': |
|
| 244 | + $imageType = IMAGETYPE_GIF; |
|
| 245 | + break; |
|
| 246 | + case 'image/jpeg': |
|
| 247 | + $imageType = IMAGETYPE_JPEG; |
|
| 248 | + break; |
|
| 249 | + case 'image/png': |
|
| 250 | + $imageType = IMAGETYPE_PNG; |
|
| 251 | + break; |
|
| 252 | + case 'image/x-xbitmap': |
|
| 253 | + $imageType = IMAGETYPE_XBM; |
|
| 254 | + break; |
|
| 255 | + case 'image/bmp': |
|
| 256 | + case 'image/x-ms-bmp': |
|
| 257 | + $imageType = IMAGETYPE_BMP; |
|
| 258 | + break; |
|
| 259 | + default: |
|
| 260 | + throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - switch ($imageType) { |
|
| 265 | - case IMAGETYPE_GIF: |
|
| 266 | - $retVal = imagegif($this->resource, $filePath); |
|
| 267 | - break; |
|
| 268 | - case IMAGETYPE_JPEG: |
|
| 269 | - $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | - break; |
|
| 271 | - case IMAGETYPE_PNG: |
|
| 272 | - $retVal = imagepng($this->resource, $filePath); |
|
| 273 | - break; |
|
| 274 | - case IMAGETYPE_XBM: |
|
| 275 | - if (function_exists('imagexbm')) { |
|
| 276 | - $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | - } else { |
|
| 278 | - throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | - } |
|
| 264 | + switch ($imageType) { |
|
| 265 | + case IMAGETYPE_GIF: |
|
| 266 | + $retVal = imagegif($this->resource, $filePath); |
|
| 267 | + break; |
|
| 268 | + case IMAGETYPE_JPEG: |
|
| 269 | + $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | + break; |
|
| 271 | + case IMAGETYPE_PNG: |
|
| 272 | + $retVal = imagepng($this->resource, $filePath); |
|
| 273 | + break; |
|
| 274 | + case IMAGETYPE_XBM: |
|
| 275 | + if (function_exists('imagexbm')) { |
|
| 276 | + $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | + } else { |
|
| 278 | + throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - break; |
|
| 282 | - case IMAGETYPE_WBMP: |
|
| 283 | - $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | - break; |
|
| 285 | - case IMAGETYPE_BMP: |
|
| 286 | - $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | - break; |
|
| 288 | - default: |
|
| 289 | - $retVal = imagepng($this->resource, $filePath); |
|
| 290 | - } |
|
| 291 | - return $retVal; |
|
| 292 | - } |
|
| 281 | + break; |
|
| 282 | + case IMAGETYPE_WBMP: |
|
| 283 | + $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | + break; |
|
| 285 | + case IMAGETYPE_BMP: |
|
| 286 | + $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | + break; |
|
| 288 | + default: |
|
| 289 | + $retVal = imagepng($this->resource, $filePath); |
|
| 290 | + } |
|
| 291 | + return $retVal; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Prints the image when called as $image(). |
|
| 296 | - */ |
|
| 297 | - public function __invoke() { |
|
| 298 | - return $this->show(); |
|
| 299 | - } |
|
| 294 | + /** |
|
| 295 | + * Prints the image when called as $image(). |
|
| 296 | + */ |
|
| 297 | + public function __invoke() { |
|
| 298 | + return $this->show(); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * @param resource Returns the image resource in any. |
|
| 303 | - * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | - */ |
|
| 305 | - public function setResource($resource) { |
|
| 306 | - if (get_resource_type($resource) === 'gd') { |
|
| 307 | - $this->resource = $resource; |
|
| 308 | - return; |
|
| 309 | - } |
|
| 310 | - throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | - } |
|
| 301 | + /** |
|
| 302 | + * @param resource Returns the image resource in any. |
|
| 303 | + * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | + */ |
|
| 305 | + public function setResource($resource) { |
|
| 306 | + if (get_resource_type($resource) === 'gd') { |
|
| 307 | + $this->resource = $resource; |
|
| 308 | + return; |
|
| 309 | + } |
|
| 310 | + throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** |
|
| 314 | - * @return resource Returns the image resource in any. |
|
| 315 | - */ |
|
| 316 | - public function resource() { |
|
| 317 | - return $this->resource; |
|
| 318 | - } |
|
| 313 | + /** |
|
| 314 | + * @return resource Returns the image resource in any. |
|
| 315 | + */ |
|
| 316 | + public function resource() { |
|
| 317 | + return $this->resource; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - /** |
|
| 321 | - * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | - * if the data is not valid. |
|
| 323 | - */ |
|
| 324 | - public function dataMimeType() { |
|
| 325 | - if (!$this->valid()) { |
|
| 326 | - return ''; |
|
| 327 | - } |
|
| 320 | + /** |
|
| 321 | + * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | + * if the data is not valid. |
|
| 323 | + */ |
|
| 324 | + public function dataMimeType() { |
|
| 325 | + if (!$this->valid()) { |
|
| 326 | + return ''; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - switch ($this->mimeType) { |
|
| 330 | - case 'image/png': |
|
| 331 | - case 'image/jpeg': |
|
| 332 | - case 'image/gif': |
|
| 333 | - return $this->mimeType; |
|
| 334 | - default: |
|
| 335 | - return 'image/png'; |
|
| 336 | - } |
|
| 337 | - } |
|
| 329 | + switch ($this->mimeType) { |
|
| 330 | + case 'image/png': |
|
| 331 | + case 'image/jpeg': |
|
| 332 | + case 'image/gif': |
|
| 333 | + return $this->mimeType; |
|
| 334 | + default: |
|
| 335 | + return 'image/png'; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * @return null|string Returns the raw image data. |
|
| 341 | - */ |
|
| 342 | - public function data() { |
|
| 343 | - if (!$this->valid()) { |
|
| 344 | - return null; |
|
| 345 | - } |
|
| 346 | - ob_start(); |
|
| 347 | - switch ($this->mimeType) { |
|
| 348 | - case "image/png": |
|
| 349 | - $res = imagepng($this->resource); |
|
| 350 | - break; |
|
| 351 | - case "image/jpeg": |
|
| 352 | - $quality = $this->getJpegQuality(); |
|
| 353 | - if ($quality !== null) { |
|
| 354 | - $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | - } else { |
|
| 356 | - $res = imagejpeg($this->resource); |
|
| 357 | - } |
|
| 358 | - break; |
|
| 359 | - case "image/gif": |
|
| 360 | - $res = imagegif($this->resource); |
|
| 361 | - break; |
|
| 362 | - default: |
|
| 363 | - $res = imagepng($this->resource); |
|
| 364 | - $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - if (!$res) { |
|
| 368 | - $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | - } |
|
| 370 | - return ob_get_clean(); |
|
| 371 | - } |
|
| 339 | + /** |
|
| 340 | + * @return null|string Returns the raw image data. |
|
| 341 | + */ |
|
| 342 | + public function data() { |
|
| 343 | + if (!$this->valid()) { |
|
| 344 | + return null; |
|
| 345 | + } |
|
| 346 | + ob_start(); |
|
| 347 | + switch ($this->mimeType) { |
|
| 348 | + case "image/png": |
|
| 349 | + $res = imagepng($this->resource); |
|
| 350 | + break; |
|
| 351 | + case "image/jpeg": |
|
| 352 | + $quality = $this->getJpegQuality(); |
|
| 353 | + if ($quality !== null) { |
|
| 354 | + $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | + } else { |
|
| 356 | + $res = imagejpeg($this->resource); |
|
| 357 | + } |
|
| 358 | + break; |
|
| 359 | + case "image/gif": |
|
| 360 | + $res = imagegif($this->resource); |
|
| 361 | + break; |
|
| 362 | + default: |
|
| 363 | + $res = imagepng($this->resource); |
|
| 364 | + $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + if (!$res) { |
|
| 368 | + $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | + } |
|
| 370 | + return ob_get_clean(); |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - /** |
|
| 374 | - * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | - */ |
|
| 376 | - public function __toString() { |
|
| 377 | - return base64_encode($this->data()); |
|
| 378 | - } |
|
| 373 | + /** |
|
| 374 | + * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | + */ |
|
| 376 | + public function __toString() { |
|
| 377 | + return base64_encode($this->data()); |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * @return int|null |
|
| 382 | - */ |
|
| 383 | - protected function getJpegQuality() { |
|
| 384 | - $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | - if ($quality !== null) { |
|
| 386 | - $quality = min(100, max(10, (int) $quality)); |
|
| 387 | - } |
|
| 388 | - return $quality; |
|
| 389 | - } |
|
| 380 | + /** |
|
| 381 | + * @return int|null |
|
| 382 | + */ |
|
| 383 | + protected function getJpegQuality() { |
|
| 384 | + $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | + if ($quality !== null) { |
|
| 386 | + $quality = min(100, max(10, (int) $quality)); |
|
| 387 | + } |
|
| 388 | + return $quality; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * (I'm open for suggestions on better method name ;) |
|
| 393 | - * Get the orientation based on EXIF data. |
|
| 394 | - * |
|
| 395 | - * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | - */ |
|
| 397 | - public function getOrientation() { |
|
| 398 | - if ($this->exif !== null) { |
|
| 399 | - return $this->exif['Orientation']; |
|
| 400 | - } |
|
| 391 | + /** |
|
| 392 | + * (I'm open for suggestions on better method name ;) |
|
| 393 | + * Get the orientation based on EXIF data. |
|
| 394 | + * |
|
| 395 | + * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | + */ |
|
| 397 | + public function getOrientation() { |
|
| 398 | + if ($this->exif !== null) { |
|
| 399 | + return $this->exif['Orientation']; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | - $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | - return -1; |
|
| 405 | - } |
|
| 406 | - if (!is_callable('exif_read_data')) { |
|
| 407 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | - return -1; |
|
| 409 | - } |
|
| 410 | - if (!$this->valid()) { |
|
| 411 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | - return -1; |
|
| 413 | - } |
|
| 414 | - if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | - $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | - return -1; |
|
| 417 | - } |
|
| 418 | - $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | - if (!$exif) { |
|
| 420 | - return -1; |
|
| 421 | - } |
|
| 422 | - if (!isset($exif['Orientation'])) { |
|
| 423 | - return -1; |
|
| 424 | - } |
|
| 425 | - $this->exif = $exif; |
|
| 426 | - return $exif['Orientation']; |
|
| 427 | - } |
|
| 402 | + if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | + $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | + return -1; |
|
| 405 | + } |
|
| 406 | + if (!is_callable('exif_read_data')) { |
|
| 407 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | + return -1; |
|
| 409 | + } |
|
| 410 | + if (!$this->valid()) { |
|
| 411 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | + return -1; |
|
| 413 | + } |
|
| 414 | + if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | + $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | + return -1; |
|
| 417 | + } |
|
| 418 | + $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | + if (!$exif) { |
|
| 420 | + return -1; |
|
| 421 | + } |
|
| 422 | + if (!isset($exif['Orientation'])) { |
|
| 423 | + return -1; |
|
| 424 | + } |
|
| 425 | + $this->exif = $exif; |
|
| 426 | + return $exif['Orientation']; |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | - public function readExif($data) { |
|
| 430 | - if (!is_callable('exif_read_data')) { |
|
| 431 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | - return; |
|
| 433 | - } |
|
| 434 | - if (!$this->valid()) { |
|
| 435 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | - return; |
|
| 437 | - } |
|
| 429 | + public function readExif($data) { |
|
| 430 | + if (!is_callable('exif_read_data')) { |
|
| 431 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | + return; |
|
| 433 | + } |
|
| 434 | + if (!$this->valid()) { |
|
| 435 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | + return; |
|
| 437 | + } |
|
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | - if (!$exif) { |
|
| 441 | - return; |
|
| 442 | - } |
|
| 443 | - if (!isset($exif['Orientation'])) { |
|
| 444 | - return; |
|
| 445 | - } |
|
| 446 | - $this->exif = $exif; |
|
| 447 | - } |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | + if (!$exif) { |
|
| 441 | + return; |
|
| 442 | + } |
|
| 443 | + if (!isset($exif['Orientation'])) { |
|
| 444 | + return; |
|
| 445 | + } |
|
| 446 | + $this->exif = $exif; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * (I'm open for suggestions on better method name ;) |
|
| 451 | - * Fixes orientation based on EXIF data. |
|
| 452 | - * |
|
| 453 | - * @return bool |
|
| 454 | - */ |
|
| 455 | - public function fixOrientation() { |
|
| 456 | - $o = $this->getOrientation(); |
|
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | - $rotate = 0; |
|
| 459 | - $flip = false; |
|
| 460 | - switch ($o) { |
|
| 461 | - case -1: |
|
| 462 | - return false; //Nothing to fix |
|
| 463 | - case 1: |
|
| 464 | - $rotate = 0; |
|
| 465 | - break; |
|
| 466 | - case 2: |
|
| 467 | - $rotate = 0; |
|
| 468 | - $flip = true; |
|
| 469 | - break; |
|
| 470 | - case 3: |
|
| 471 | - $rotate = 180; |
|
| 472 | - break; |
|
| 473 | - case 4: |
|
| 474 | - $rotate = 180; |
|
| 475 | - $flip = true; |
|
| 476 | - break; |
|
| 477 | - case 5: |
|
| 478 | - $rotate = 90; |
|
| 479 | - $flip = true; |
|
| 480 | - break; |
|
| 481 | - case 6: |
|
| 482 | - $rotate = 270; |
|
| 483 | - break; |
|
| 484 | - case 7: |
|
| 485 | - $rotate = 270; |
|
| 486 | - $flip = true; |
|
| 487 | - break; |
|
| 488 | - case 8: |
|
| 489 | - $rotate = 90; |
|
| 490 | - break; |
|
| 491 | - } |
|
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 493 | - imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | - } |
|
| 495 | - if ($rotate) { |
|
| 496 | - $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | - if ($res) { |
|
| 498 | - if (imagealphablending($res, true)) { |
|
| 499 | - if (imagesavealpha($res, true)) { |
|
| 500 | - imagedestroy($this->resource); |
|
| 501 | - $this->resource = $res; |
|
| 502 | - return true; |
|
| 503 | - } else { |
|
| 504 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - } else { |
|
| 508 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 511 | - } else { |
|
| 512 | - $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | - return false; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 449 | + /** |
|
| 450 | + * (I'm open for suggestions on better method name ;) |
|
| 451 | + * Fixes orientation based on EXIF data. |
|
| 452 | + * |
|
| 453 | + * @return bool |
|
| 454 | + */ |
|
| 455 | + public function fixOrientation() { |
|
| 456 | + $o = $this->getOrientation(); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | + $rotate = 0; |
|
| 459 | + $flip = false; |
|
| 460 | + switch ($o) { |
|
| 461 | + case -1: |
|
| 462 | + return false; //Nothing to fix |
|
| 463 | + case 1: |
|
| 464 | + $rotate = 0; |
|
| 465 | + break; |
|
| 466 | + case 2: |
|
| 467 | + $rotate = 0; |
|
| 468 | + $flip = true; |
|
| 469 | + break; |
|
| 470 | + case 3: |
|
| 471 | + $rotate = 180; |
|
| 472 | + break; |
|
| 473 | + case 4: |
|
| 474 | + $rotate = 180; |
|
| 475 | + $flip = true; |
|
| 476 | + break; |
|
| 477 | + case 5: |
|
| 478 | + $rotate = 90; |
|
| 479 | + $flip = true; |
|
| 480 | + break; |
|
| 481 | + case 6: |
|
| 482 | + $rotate = 270; |
|
| 483 | + break; |
|
| 484 | + case 7: |
|
| 485 | + $rotate = 270; |
|
| 486 | + $flip = true; |
|
| 487 | + break; |
|
| 488 | + case 8: |
|
| 489 | + $rotate = 90; |
|
| 490 | + break; |
|
| 491 | + } |
|
| 492 | + if($flip && function_exists('imageflip')) { |
|
| 493 | + imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | + } |
|
| 495 | + if ($rotate) { |
|
| 496 | + $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | + if ($res) { |
|
| 498 | + if (imagealphablending($res, true)) { |
|
| 499 | + if (imagesavealpha($res, true)) { |
|
| 500 | + imagedestroy($this->resource); |
|
| 501 | + $this->resource = $res; |
|
| 502 | + return true; |
|
| 503 | + } else { |
|
| 504 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + } else { |
|
| 508 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | + } else { |
|
| 512 | + $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | + return false; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - /** |
|
| 520 | - * Loads an image from an open file handle. |
|
| 521 | - * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | - * |
|
| 523 | - * @param resource $handle |
|
| 524 | - * @return resource|false An image resource or false on error |
|
| 525 | - */ |
|
| 526 | - public function loadFromFileHandle($handle) { |
|
| 527 | - $contents = stream_get_contents($handle); |
|
| 528 | - if ($this->loadFromData($contents)) { |
|
| 529 | - return $this->resource; |
|
| 530 | - } |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 519 | + /** |
|
| 520 | + * Loads an image from an open file handle. |
|
| 521 | + * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | + * |
|
| 523 | + * @param resource $handle |
|
| 524 | + * @return resource|false An image resource or false on error |
|
| 525 | + */ |
|
| 526 | + public function loadFromFileHandle($handle) { |
|
| 527 | + $contents = stream_get_contents($handle); |
|
| 528 | + if ($this->loadFromData($contents)) { |
|
| 529 | + return $this->resource; |
|
| 530 | + } |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | 533 | |
| 534 | - /** |
|
| 535 | - * Loads an image from a local file. |
|
| 536 | - * |
|
| 537 | - * @param bool|string $imagePath The path to a local file. |
|
| 538 | - * @return bool|resource An image resource or false on error |
|
| 539 | - */ |
|
| 540 | - public function loadFromFile($imagePath = false) { |
|
| 541 | - // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | - if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - $iType = exif_imagetype($imagePath); |
|
| 546 | - switch ($iType) { |
|
| 547 | - case IMAGETYPE_GIF: |
|
| 548 | - if (imagetypes() & IMG_GIF) { |
|
| 549 | - $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | - // Preserve transparency |
|
| 551 | - imagealphablending($this->resource, true); |
|
| 552 | - imagesavealpha($this->resource, true); |
|
| 553 | - } else { |
|
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | - } |
|
| 556 | - break; |
|
| 557 | - case IMAGETYPE_JPEG: |
|
| 558 | - if (imagetypes() & IMG_JPG) { |
|
| 559 | - if (getimagesize($imagePath) !== false) { |
|
| 560 | - $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | - } else { |
|
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | - } |
|
| 564 | - } else { |
|
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | - } |
|
| 567 | - break; |
|
| 568 | - case IMAGETYPE_PNG: |
|
| 569 | - if (imagetypes() & IMG_PNG) { |
|
| 570 | - $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | - // Preserve transparency |
|
| 572 | - imagealphablending($this->resource, true); |
|
| 573 | - imagesavealpha($this->resource, true); |
|
| 574 | - } else { |
|
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | - } |
|
| 577 | - break; |
|
| 578 | - case IMAGETYPE_XBM: |
|
| 579 | - if (imagetypes() & IMG_XPM) { |
|
| 580 | - $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | - } else { |
|
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | - } |
|
| 584 | - break; |
|
| 585 | - case IMAGETYPE_WBMP: |
|
| 586 | - if (imagetypes() & IMG_WBMP) { |
|
| 587 | - $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | - } else { |
|
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | - } |
|
| 591 | - break; |
|
| 592 | - case IMAGETYPE_BMP: |
|
| 593 | - $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | - break; |
|
| 595 | - /* |
|
| 534 | + /** |
|
| 535 | + * Loads an image from a local file. |
|
| 536 | + * |
|
| 537 | + * @param bool|string $imagePath The path to a local file. |
|
| 538 | + * @return bool|resource An image resource or false on error |
|
| 539 | + */ |
|
| 540 | + public function loadFromFile($imagePath = false) { |
|
| 541 | + // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | + if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + $iType = exif_imagetype($imagePath); |
|
| 546 | + switch ($iType) { |
|
| 547 | + case IMAGETYPE_GIF: |
|
| 548 | + if (imagetypes() & IMG_GIF) { |
|
| 549 | + $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | + // Preserve transparency |
|
| 551 | + imagealphablending($this->resource, true); |
|
| 552 | + imagesavealpha($this->resource, true); |
|
| 553 | + } else { |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | + } |
|
| 556 | + break; |
|
| 557 | + case IMAGETYPE_JPEG: |
|
| 558 | + if (imagetypes() & IMG_JPG) { |
|
| 559 | + if (getimagesize($imagePath) !== false) { |
|
| 560 | + $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | + } else { |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | + } |
|
| 564 | + } else { |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | + } |
|
| 567 | + break; |
|
| 568 | + case IMAGETYPE_PNG: |
|
| 569 | + if (imagetypes() & IMG_PNG) { |
|
| 570 | + $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | + // Preserve transparency |
|
| 572 | + imagealphablending($this->resource, true); |
|
| 573 | + imagesavealpha($this->resource, true); |
|
| 574 | + } else { |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | + } |
|
| 577 | + break; |
|
| 578 | + case IMAGETYPE_XBM: |
|
| 579 | + if (imagetypes() & IMG_XPM) { |
|
| 580 | + $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | + } else { |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | + } |
|
| 584 | + break; |
|
| 585 | + case IMAGETYPE_WBMP: |
|
| 586 | + if (imagetypes() & IMG_WBMP) { |
|
| 587 | + $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | + } else { |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | + } |
|
| 591 | + break; |
|
| 592 | + case IMAGETYPE_BMP: |
|
| 593 | + $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | + break; |
|
| 595 | + /* |
|
| 596 | 596 | case IMAGETYPE_TIFF_II: // (intel byte order) |
| 597 | 597 | break; |
| 598 | 598 | case IMAGETYPE_TIFF_MM: // (motorola byte order) |
@@ -616,579 +616,579 @@ discard block |
||
| 616 | 616 | case IMAGETYPE_PSD: |
| 617 | 617 | break; |
| 618 | 618 | */ |
| 619 | - default: |
|
| 619 | + default: |
|
| 620 | 620 | |
| 621 | - // this is mostly file created from encrypted file |
|
| 622 | - $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | - $iType = IMAGETYPE_PNG; |
|
| 624 | - $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | - break; |
|
| 626 | - } |
|
| 627 | - if ($this->valid()) { |
|
| 628 | - $this->imageType = $iType; |
|
| 629 | - $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | - $this->filePath = $imagePath; |
|
| 631 | - } |
|
| 632 | - return $this->resource; |
|
| 633 | - } |
|
| 621 | + // this is mostly file created from encrypted file |
|
| 622 | + $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | + $iType = IMAGETYPE_PNG; |
|
| 624 | + $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | + break; |
|
| 626 | + } |
|
| 627 | + if ($this->valid()) { |
|
| 628 | + $this->imageType = $iType; |
|
| 629 | + $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | + $this->filePath = $imagePath; |
|
| 631 | + } |
|
| 632 | + return $this->resource; |
|
| 633 | + } |
|
| 634 | 634 | |
| 635 | - /** |
|
| 636 | - * Loads an image from a string of data. |
|
| 637 | - * |
|
| 638 | - * @param string $str A string of image data as read from a file. |
|
| 639 | - * @return bool|resource An image resource or false on error |
|
| 640 | - */ |
|
| 641 | - public function loadFromData($str) { |
|
| 642 | - if (is_resource($str)) { |
|
| 643 | - return false; |
|
| 644 | - } |
|
| 645 | - $this->resource = @imagecreatefromstring($str); |
|
| 646 | - if ($this->fileInfo) { |
|
| 647 | - $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | - } |
|
| 649 | - if (is_resource($this->resource)) { |
|
| 650 | - imagealphablending($this->resource, false); |
|
| 651 | - imagesavealpha($this->resource, true); |
|
| 652 | - } |
|
| 635 | + /** |
|
| 636 | + * Loads an image from a string of data. |
|
| 637 | + * |
|
| 638 | + * @param string $str A string of image data as read from a file. |
|
| 639 | + * @return bool|resource An image resource or false on error |
|
| 640 | + */ |
|
| 641 | + public function loadFromData($str) { |
|
| 642 | + if (is_resource($str)) { |
|
| 643 | + return false; |
|
| 644 | + } |
|
| 645 | + $this->resource = @imagecreatefromstring($str); |
|
| 646 | + if ($this->fileInfo) { |
|
| 647 | + $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | + } |
|
| 649 | + if (is_resource($this->resource)) { |
|
| 650 | + imagealphablending($this->resource, false); |
|
| 651 | + imagesavealpha($this->resource, true); |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | - if (!$this->resource) { |
|
| 655 | - $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - return $this->resource; |
|
| 659 | - } |
|
| 654 | + if (!$this->resource) { |
|
| 655 | + $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + return $this->resource; |
|
| 659 | + } |
|
| 660 | 660 | |
| 661 | - /** |
|
| 662 | - * Loads an image from a base64 encoded string. |
|
| 663 | - * |
|
| 664 | - * @param string $str A string base64 encoded string of image data. |
|
| 665 | - * @return bool|resource An image resource or false on error |
|
| 666 | - */ |
|
| 667 | - public function loadFromBase64($str) { |
|
| 668 | - if (!is_string($str)) { |
|
| 669 | - return false; |
|
| 670 | - } |
|
| 671 | - $data = base64_decode($str); |
|
| 672 | - if ($data) { // try to load from string data |
|
| 673 | - $this->resource = @imagecreatefromstring($data); |
|
| 674 | - if ($this->fileInfo) { |
|
| 675 | - $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | - } |
|
| 677 | - if (!$this->resource) { |
|
| 678 | - $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - return $this->resource; |
|
| 682 | - } else { |
|
| 683 | - return false; |
|
| 684 | - } |
|
| 685 | - } |
|
| 661 | + /** |
|
| 662 | + * Loads an image from a base64 encoded string. |
|
| 663 | + * |
|
| 664 | + * @param string $str A string base64 encoded string of image data. |
|
| 665 | + * @return bool|resource An image resource or false on error |
|
| 666 | + */ |
|
| 667 | + public function loadFromBase64($str) { |
|
| 668 | + if (!is_string($str)) { |
|
| 669 | + return false; |
|
| 670 | + } |
|
| 671 | + $data = base64_decode($str); |
|
| 672 | + if ($data) { // try to load from string data |
|
| 673 | + $this->resource = @imagecreatefromstring($data); |
|
| 674 | + if ($this->fileInfo) { |
|
| 675 | + $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | + } |
|
| 677 | + if (!$this->resource) { |
|
| 678 | + $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + return $this->resource; |
|
| 682 | + } else { |
|
| 683 | + return false; |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - /** |
|
| 688 | - * Create a new image from file or URL |
|
| 689 | - * |
|
| 690 | - * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | - * @version 1.00 |
|
| 692 | - * @param string $fileName <p> |
|
| 693 | - * Path to the BMP image. |
|
| 694 | - * </p> |
|
| 695 | - * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | - */ |
|
| 697 | - private function imagecreatefrombmp($fileName) { |
|
| 698 | - if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | - return false; |
|
| 701 | - } |
|
| 702 | - // read file header |
|
| 703 | - $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | - // check for bitmap |
|
| 705 | - if ($meta['type'] != 19778) { |
|
| 706 | - fclose($fh); |
|
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | - return false; |
|
| 709 | - } |
|
| 710 | - // read image header |
|
| 711 | - $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | - // read additional 16bit header |
|
| 713 | - if ($meta['bits'] == 16) { |
|
| 714 | - $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | - } |
|
| 716 | - // set bytes and padding |
|
| 717 | - $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | - $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | - $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | - if ($meta['decal'] == 4) { |
|
| 721 | - $meta['decal'] = 0; |
|
| 722 | - } |
|
| 723 | - // obtain imagesize |
|
| 724 | - if ($meta['imagesize'] < 1) { |
|
| 725 | - $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | - // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | - if ($meta['imagesize'] < 1) { |
|
| 728 | - $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | - if ($meta['imagesize'] < 1) { |
|
| 730 | - fclose($fh); |
|
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | - return false; |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - // calculate colors |
|
| 737 | - $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | - // read color palette |
|
| 739 | - $palette = array(); |
|
| 740 | - if ($meta['bits'] < 16) { |
|
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | - // in rare cases the color value is signed |
|
| 743 | - if ($palette[1] < 0) { |
|
| 744 | - foreach ($palette as $i => $color) { |
|
| 745 | - $palette[$i] = $color + 16777216; |
|
| 746 | - } |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - // create gd image |
|
| 750 | - $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | - if ($im == false) { |
|
| 752 | - fclose($fh); |
|
| 753 | - $this->logger->warning( |
|
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | - array('app' => 'core')); |
|
| 756 | - return false; |
|
| 757 | - } |
|
| 687 | + /** |
|
| 688 | + * Create a new image from file or URL |
|
| 689 | + * |
|
| 690 | + * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | + * @version 1.00 |
|
| 692 | + * @param string $fileName <p> |
|
| 693 | + * Path to the BMP image. |
|
| 694 | + * </p> |
|
| 695 | + * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | + */ |
|
| 697 | + private function imagecreatefrombmp($fileName) { |
|
| 698 | + if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | + return false; |
|
| 701 | + } |
|
| 702 | + // read file header |
|
| 703 | + $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | + // check for bitmap |
|
| 705 | + if ($meta['type'] != 19778) { |
|
| 706 | + fclose($fh); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | + return false; |
|
| 709 | + } |
|
| 710 | + // read image header |
|
| 711 | + $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | + // read additional 16bit header |
|
| 713 | + if ($meta['bits'] == 16) { |
|
| 714 | + $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | + } |
|
| 716 | + // set bytes and padding |
|
| 717 | + $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | + $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | + $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | + if ($meta['decal'] == 4) { |
|
| 721 | + $meta['decal'] = 0; |
|
| 722 | + } |
|
| 723 | + // obtain imagesize |
|
| 724 | + if ($meta['imagesize'] < 1) { |
|
| 725 | + $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | + // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | + if ($meta['imagesize'] < 1) { |
|
| 728 | + $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | + if ($meta['imagesize'] < 1) { |
|
| 730 | + fclose($fh); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | + return false; |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + // calculate colors |
|
| 737 | + $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | + // read color palette |
|
| 739 | + $palette = array(); |
|
| 740 | + if ($meta['bits'] < 16) { |
|
| 741 | + $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | + // in rare cases the color value is signed |
|
| 743 | + if ($palette[1] < 0) { |
|
| 744 | + foreach ($palette as $i => $color) { |
|
| 745 | + $palette[$i] = $color + 16777216; |
|
| 746 | + } |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + // create gd image |
|
| 750 | + $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | + if ($im == false) { |
|
| 752 | + fclose($fh); |
|
| 753 | + $this->logger->warning( |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | + array('app' => 'core')); |
|
| 756 | + return false; |
|
| 757 | + } |
|
| 758 | 758 | |
| 759 | - $data = fread($fh, $meta['imagesize']); |
|
| 760 | - $p = 0; |
|
| 761 | - $vide = chr(0); |
|
| 762 | - $y = $meta['height'] - 1; |
|
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | - // loop through the image data beginning with the lower left corner |
|
| 765 | - while ($y >= 0) { |
|
| 766 | - $x = 0; |
|
| 767 | - while ($x < $meta['width']) { |
|
| 768 | - switch ($meta['bits']) { |
|
| 769 | - case 32: |
|
| 770 | - case 24: |
|
| 771 | - if (!($part = substr($data, $p, 3))) { |
|
| 772 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | - return $im; |
|
| 774 | - } |
|
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 776 | - break; |
|
| 777 | - case 16: |
|
| 778 | - if (!($part = substr($data, $p, 2))) { |
|
| 779 | - fclose($fh); |
|
| 780 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | - return $im; |
|
| 782 | - } |
|
| 783 | - $color = @unpack('v', $part); |
|
| 784 | - $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | - break; |
|
| 786 | - case 8: |
|
| 787 | - $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 788 | - $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | - break; |
|
| 790 | - case 4: |
|
| 791 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 792 | - $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | - $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | - break; |
|
| 795 | - case 1: |
|
| 796 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 797 | - switch (($p * 8) % 8) { |
|
| 798 | - case 0: |
|
| 799 | - $color[1] = $color[1] >> 7; |
|
| 800 | - break; |
|
| 801 | - case 1: |
|
| 802 | - $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | - break; |
|
| 804 | - case 2: |
|
| 805 | - $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | - break; |
|
| 807 | - case 3: |
|
| 808 | - $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | - break; |
|
| 810 | - case 4: |
|
| 811 | - $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | - break; |
|
| 813 | - case 5: |
|
| 814 | - $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | - break; |
|
| 816 | - case 6: |
|
| 817 | - $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | - break; |
|
| 819 | - case 7: |
|
| 820 | - $color[1] = ($color[1] & 0x1); |
|
| 821 | - break; |
|
| 822 | - } |
|
| 823 | - $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | - break; |
|
| 825 | - default: |
|
| 826 | - fclose($fh); |
|
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | - return false; |
|
| 829 | - } |
|
| 830 | - imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | - $x++; |
|
| 832 | - $p += $meta['bytes']; |
|
| 833 | - } |
|
| 834 | - $y--; |
|
| 835 | - $p += $meta['decal']; |
|
| 836 | - } |
|
| 837 | - fclose($fh); |
|
| 838 | - return $im; |
|
| 839 | - } |
|
| 759 | + $data = fread($fh, $meta['imagesize']); |
|
| 760 | + $p = 0; |
|
| 761 | + $vide = chr(0); |
|
| 762 | + $y = $meta['height'] - 1; |
|
| 763 | + $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | + // loop through the image data beginning with the lower left corner |
|
| 765 | + while ($y >= 0) { |
|
| 766 | + $x = 0; |
|
| 767 | + while ($x < $meta['width']) { |
|
| 768 | + switch ($meta['bits']) { |
|
| 769 | + case 32: |
|
| 770 | + case 24: |
|
| 771 | + if (!($part = substr($data, $p, 3))) { |
|
| 772 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | + return $im; |
|
| 774 | + } |
|
| 775 | + $color = @unpack('V', $part . $vide); |
|
| 776 | + break; |
|
| 777 | + case 16: |
|
| 778 | + if (!($part = substr($data, $p, 2))) { |
|
| 779 | + fclose($fh); |
|
| 780 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | + return $im; |
|
| 782 | + } |
|
| 783 | + $color = @unpack('v', $part); |
|
| 784 | + $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | + break; |
|
| 786 | + case 8: |
|
| 787 | + $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 788 | + $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | + break; |
|
| 790 | + case 4: |
|
| 791 | + $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 792 | + $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | + $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | + break; |
|
| 795 | + case 1: |
|
| 796 | + $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 797 | + switch (($p * 8) % 8) { |
|
| 798 | + case 0: |
|
| 799 | + $color[1] = $color[1] >> 7; |
|
| 800 | + break; |
|
| 801 | + case 1: |
|
| 802 | + $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | + break; |
|
| 804 | + case 2: |
|
| 805 | + $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | + break; |
|
| 807 | + case 3: |
|
| 808 | + $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | + break; |
|
| 810 | + case 4: |
|
| 811 | + $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | + break; |
|
| 813 | + case 5: |
|
| 814 | + $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | + break; |
|
| 816 | + case 6: |
|
| 817 | + $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | + break; |
|
| 819 | + case 7: |
|
| 820 | + $color[1] = ($color[1] & 0x1); |
|
| 821 | + break; |
|
| 822 | + } |
|
| 823 | + $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | + break; |
|
| 825 | + default: |
|
| 826 | + fclose($fh); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | + return false; |
|
| 829 | + } |
|
| 830 | + imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | + $x++; |
|
| 832 | + $p += $meta['bytes']; |
|
| 833 | + } |
|
| 834 | + $y--; |
|
| 835 | + $p += $meta['decal']; |
|
| 836 | + } |
|
| 837 | + fclose($fh); |
|
| 838 | + return $im; |
|
| 839 | + } |
|
| 840 | 840 | |
| 841 | - /** |
|
| 842 | - * Resizes the image preserving ratio. |
|
| 843 | - * |
|
| 844 | - * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | - * @return bool |
|
| 846 | - */ |
|
| 847 | - public function resize($maxSize) { |
|
| 848 | - if (!$this->valid()) { |
|
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | - return false; |
|
| 851 | - } |
|
| 852 | - $widthOrig = imagesx($this->resource); |
|
| 853 | - $heightOrig = imagesy($this->resource); |
|
| 854 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 841 | + /** |
|
| 842 | + * Resizes the image preserving ratio. |
|
| 843 | + * |
|
| 844 | + * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | + * @return bool |
|
| 846 | + */ |
|
| 847 | + public function resize($maxSize) { |
|
| 848 | + if (!$this->valid()) { |
|
| 849 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | + return false; |
|
| 851 | + } |
|
| 852 | + $widthOrig = imagesx($this->resource); |
|
| 853 | + $heightOrig = imagesy($this->resource); |
|
| 854 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 855 | 855 | |
| 856 | - if ($ratioOrig > 1) { |
|
| 857 | - $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | - $newWidth = $maxSize; |
|
| 859 | - } else { |
|
| 860 | - $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | - $newHeight = $maxSize; |
|
| 862 | - } |
|
| 856 | + if ($ratioOrig > 1) { |
|
| 857 | + $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | + $newWidth = $maxSize; |
|
| 859 | + } else { |
|
| 860 | + $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | + $newHeight = $maxSize; |
|
| 862 | + } |
|
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | - return true; |
|
| 866 | - } |
|
| 864 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | + return true; |
|
| 866 | + } |
|
| 867 | 867 | |
| 868 | - /** |
|
| 869 | - * @param int $width |
|
| 870 | - * @param int $height |
|
| 871 | - * @return bool |
|
| 872 | - */ |
|
| 873 | - public function preciseResize(int $width, int $height): bool { |
|
| 874 | - if (!$this->valid()) { |
|
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | - return false; |
|
| 877 | - } |
|
| 878 | - $widthOrig = imagesx($this->resource); |
|
| 879 | - $heightOrig = imagesy($this->resource); |
|
| 880 | - $process = imagecreatetruecolor($width, $height); |
|
| 881 | - if ($process === false) { |
|
| 882 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 883 | - return false; |
|
| 884 | - } |
|
| 868 | + /** |
|
| 869 | + * @param int $width |
|
| 870 | + * @param int $height |
|
| 871 | + * @return bool |
|
| 872 | + */ |
|
| 873 | + public function preciseResize(int $width, int $height): bool { |
|
| 874 | + if (!$this->valid()) { |
|
| 875 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | + return false; |
|
| 877 | + } |
|
| 878 | + $widthOrig = imagesx($this->resource); |
|
| 879 | + $heightOrig = imagesy($this->resource); |
|
| 880 | + $process = imagecreatetruecolor($width, $height); |
|
| 881 | + if ($process === false) { |
|
| 882 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 883 | + return false; |
|
| 884 | + } |
|
| 885 | 885 | |
| 886 | - // preserve transparency |
|
| 887 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 888 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 889 | - imagealphablending($process, false); |
|
| 890 | - imagesavealpha($process, true); |
|
| 891 | - } |
|
| 886 | + // preserve transparency |
|
| 887 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 888 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 889 | + imagealphablending($process, false); |
|
| 890 | + imagesavealpha($process, true); |
|
| 891 | + } |
|
| 892 | 892 | |
| 893 | - $res = imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 894 | - if ($res === false) { |
|
| 895 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 896 | - imagedestroy($process); |
|
| 897 | - return false; |
|
| 898 | - } |
|
| 899 | - imagedestroy($this->resource); |
|
| 900 | - $this->resource = $process; |
|
| 901 | - return true; |
|
| 902 | - } |
|
| 893 | + $res = imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 894 | + if ($res === false) { |
|
| 895 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 896 | + imagedestroy($process); |
|
| 897 | + return false; |
|
| 898 | + } |
|
| 899 | + imagedestroy($this->resource); |
|
| 900 | + $this->resource = $process; |
|
| 901 | + return true; |
|
| 902 | + } |
|
| 903 | 903 | |
| 904 | - /** |
|
| 905 | - * Crops the image to the middle square. If the image is already square it just returns. |
|
| 906 | - * |
|
| 907 | - * @param int $size maximum size for the result (optional) |
|
| 908 | - * @return bool for success or failure |
|
| 909 | - */ |
|
| 910 | - public function centerCrop($size = 0) { |
|
| 911 | - if (!$this->valid()) { |
|
| 912 | - $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 913 | - return false; |
|
| 914 | - } |
|
| 915 | - $widthOrig = imagesx($this->resource); |
|
| 916 | - $heightOrig = imagesy($this->resource); |
|
| 917 | - if ($widthOrig === $heightOrig and $size == 0) { |
|
| 918 | - return true; |
|
| 919 | - } |
|
| 920 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 921 | - $width = $height = min($widthOrig, $heightOrig); |
|
| 904 | + /** |
|
| 905 | + * Crops the image to the middle square. If the image is already square it just returns. |
|
| 906 | + * |
|
| 907 | + * @param int $size maximum size for the result (optional) |
|
| 908 | + * @return bool for success or failure |
|
| 909 | + */ |
|
| 910 | + public function centerCrop($size = 0) { |
|
| 911 | + if (!$this->valid()) { |
|
| 912 | + $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 913 | + return false; |
|
| 914 | + } |
|
| 915 | + $widthOrig = imagesx($this->resource); |
|
| 916 | + $heightOrig = imagesy($this->resource); |
|
| 917 | + if ($widthOrig === $heightOrig and $size == 0) { |
|
| 918 | + return true; |
|
| 919 | + } |
|
| 920 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 921 | + $width = $height = min($widthOrig, $heightOrig); |
|
| 922 | 922 | |
| 923 | - if ($ratioOrig > 1) { |
|
| 924 | - $x = ($widthOrig / 2) - ($width / 2); |
|
| 925 | - $y = 0; |
|
| 926 | - } else { |
|
| 927 | - $y = ($heightOrig / 2) - ($height / 2); |
|
| 928 | - $x = 0; |
|
| 929 | - } |
|
| 930 | - if ($size > 0) { |
|
| 931 | - $targetWidth = $size; |
|
| 932 | - $targetHeight = $size; |
|
| 933 | - } else { |
|
| 934 | - $targetWidth = $width; |
|
| 935 | - $targetHeight = $height; |
|
| 936 | - } |
|
| 937 | - $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 938 | - if ($process == false) { |
|
| 939 | - $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 940 | - imagedestroy($process); |
|
| 941 | - return false; |
|
| 942 | - } |
|
| 923 | + if ($ratioOrig > 1) { |
|
| 924 | + $x = ($widthOrig / 2) - ($width / 2); |
|
| 925 | + $y = 0; |
|
| 926 | + } else { |
|
| 927 | + $y = ($heightOrig / 2) - ($height / 2); |
|
| 928 | + $x = 0; |
|
| 929 | + } |
|
| 930 | + if ($size > 0) { |
|
| 931 | + $targetWidth = $size; |
|
| 932 | + $targetHeight = $size; |
|
| 933 | + } else { |
|
| 934 | + $targetWidth = $width; |
|
| 935 | + $targetHeight = $height; |
|
| 936 | + } |
|
| 937 | + $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 938 | + if ($process == false) { |
|
| 939 | + $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 940 | + imagedestroy($process); |
|
| 941 | + return false; |
|
| 942 | + } |
|
| 943 | 943 | |
| 944 | - // preserve transparency |
|
| 945 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 946 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 947 | - imagealphablending($process, false); |
|
| 948 | - imagesavealpha($process, true); |
|
| 949 | - } |
|
| 944 | + // preserve transparency |
|
| 945 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 946 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 947 | + imagealphablending($process, false); |
|
| 948 | + imagesavealpha($process, true); |
|
| 949 | + } |
|
| 950 | 950 | |
| 951 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 952 | - if ($process == false) { |
|
| 953 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 954 | - imagedestroy($process); |
|
| 955 | - return false; |
|
| 956 | - } |
|
| 957 | - imagedestroy($this->resource); |
|
| 958 | - $this->resource = $process; |
|
| 959 | - return true; |
|
| 960 | - } |
|
| 951 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 952 | + if ($process == false) { |
|
| 953 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 954 | + imagedestroy($process); |
|
| 955 | + return false; |
|
| 956 | + } |
|
| 957 | + imagedestroy($this->resource); |
|
| 958 | + $this->resource = $process; |
|
| 959 | + return true; |
|
| 960 | + } |
|
| 961 | 961 | |
| 962 | - /** |
|
| 963 | - * Crops the image from point $x$y with dimension $wx$h. |
|
| 964 | - * |
|
| 965 | - * @param int $x Horizontal position |
|
| 966 | - * @param int $y Vertical position |
|
| 967 | - * @param int $w Width |
|
| 968 | - * @param int $h Height |
|
| 969 | - * @return bool for success or failure |
|
| 970 | - */ |
|
| 971 | - public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 972 | - if (!$this->valid()) { |
|
| 973 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 974 | - return false; |
|
| 975 | - } |
|
| 976 | - $process = imagecreatetruecolor($w, $h); |
|
| 977 | - if ($process == false) { |
|
| 978 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 979 | - imagedestroy($process); |
|
| 980 | - return false; |
|
| 981 | - } |
|
| 962 | + /** |
|
| 963 | + * Crops the image from point $x$y with dimension $wx$h. |
|
| 964 | + * |
|
| 965 | + * @param int $x Horizontal position |
|
| 966 | + * @param int $y Vertical position |
|
| 967 | + * @param int $w Width |
|
| 968 | + * @param int $h Height |
|
| 969 | + * @return bool for success or failure |
|
| 970 | + */ |
|
| 971 | + public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 972 | + if (!$this->valid()) { |
|
| 973 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 974 | + return false; |
|
| 975 | + } |
|
| 976 | + $process = imagecreatetruecolor($w, $h); |
|
| 977 | + if ($process == false) { |
|
| 978 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 979 | + imagedestroy($process); |
|
| 980 | + return false; |
|
| 981 | + } |
|
| 982 | 982 | |
| 983 | - // preserve transparency |
|
| 984 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 985 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 986 | - imagealphablending($process, false); |
|
| 987 | - imagesavealpha($process, true); |
|
| 988 | - } |
|
| 983 | + // preserve transparency |
|
| 984 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 985 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 986 | + imagealphablending($process, false); |
|
| 987 | + imagesavealpha($process, true); |
|
| 988 | + } |
|
| 989 | 989 | |
| 990 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 991 | - if ($process == false) { |
|
| 992 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 993 | - imagedestroy($process); |
|
| 994 | - return false; |
|
| 995 | - } |
|
| 996 | - imagedestroy($this->resource); |
|
| 997 | - $this->resource = $process; |
|
| 998 | - return true; |
|
| 999 | - } |
|
| 990 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 991 | + if ($process == false) { |
|
| 992 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 993 | + imagedestroy($process); |
|
| 994 | + return false; |
|
| 995 | + } |
|
| 996 | + imagedestroy($this->resource); |
|
| 997 | + $this->resource = $process; |
|
| 998 | + return true; |
|
| 999 | + } |
|
| 1000 | 1000 | |
| 1001 | - /** |
|
| 1002 | - * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1003 | - * |
|
| 1004 | - * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1005 | - * |
|
| 1006 | - * @param integer $maxWidth |
|
| 1007 | - * @param integer $maxHeight |
|
| 1008 | - * @return bool |
|
| 1009 | - */ |
|
| 1010 | - public function fitIn($maxWidth, $maxHeight) { |
|
| 1011 | - if (!$this->valid()) { |
|
| 1012 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1013 | - return false; |
|
| 1014 | - } |
|
| 1015 | - $widthOrig = imagesx($this->resource); |
|
| 1016 | - $heightOrig = imagesy($this->resource); |
|
| 1017 | - $ratio = $widthOrig / $heightOrig; |
|
| 1001 | + /** |
|
| 1002 | + * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1003 | + * |
|
| 1004 | + * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1005 | + * |
|
| 1006 | + * @param integer $maxWidth |
|
| 1007 | + * @param integer $maxHeight |
|
| 1008 | + * @return bool |
|
| 1009 | + */ |
|
| 1010 | + public function fitIn($maxWidth, $maxHeight) { |
|
| 1011 | + if (!$this->valid()) { |
|
| 1012 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1013 | + return false; |
|
| 1014 | + } |
|
| 1015 | + $widthOrig = imagesx($this->resource); |
|
| 1016 | + $heightOrig = imagesy($this->resource); |
|
| 1017 | + $ratio = $widthOrig / $heightOrig; |
|
| 1018 | 1018 | |
| 1019 | - $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1020 | - $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1019 | + $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1020 | + $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1021 | 1021 | |
| 1022 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1023 | - return true; |
|
| 1024 | - } |
|
| 1022 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1023 | + return true; |
|
| 1024 | + } |
|
| 1025 | 1025 | |
| 1026 | - /** |
|
| 1027 | - * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1028 | - * |
|
| 1029 | - * @param integer $maxWidth |
|
| 1030 | - * @param integer $maxHeight |
|
| 1031 | - * @return bool |
|
| 1032 | - */ |
|
| 1033 | - public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1034 | - if (!$this->valid()) { |
|
| 1035 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1036 | - return false; |
|
| 1037 | - } |
|
| 1038 | - $widthOrig = imagesx($this->resource); |
|
| 1039 | - $heightOrig = imagesy($this->resource); |
|
| 1026 | + /** |
|
| 1027 | + * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1028 | + * |
|
| 1029 | + * @param integer $maxWidth |
|
| 1030 | + * @param integer $maxHeight |
|
| 1031 | + * @return bool |
|
| 1032 | + */ |
|
| 1033 | + public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1034 | + if (!$this->valid()) { |
|
| 1035 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1036 | + return false; |
|
| 1037 | + } |
|
| 1038 | + $widthOrig = imagesx($this->resource); |
|
| 1039 | + $heightOrig = imagesy($this->resource); |
|
| 1040 | 1040 | |
| 1041 | - if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1042 | - return $this->fitIn($maxWidth, $maxHeight); |
|
| 1043 | - } |
|
| 1041 | + if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1042 | + return $this->fitIn($maxWidth, $maxHeight); |
|
| 1043 | + } |
|
| 1044 | 1044 | |
| 1045 | - return false; |
|
| 1046 | - } |
|
| 1045 | + return false; |
|
| 1046 | + } |
|
| 1047 | 1047 | |
| 1048 | - /** |
|
| 1049 | - * Destroys the current image and resets the object |
|
| 1050 | - */ |
|
| 1051 | - public function destroy() { |
|
| 1052 | - if ($this->valid()) { |
|
| 1053 | - imagedestroy($this->resource); |
|
| 1054 | - } |
|
| 1055 | - $this->resource = null; |
|
| 1056 | - } |
|
| 1048 | + /** |
|
| 1049 | + * Destroys the current image and resets the object |
|
| 1050 | + */ |
|
| 1051 | + public function destroy() { |
|
| 1052 | + if ($this->valid()) { |
|
| 1053 | + imagedestroy($this->resource); |
|
| 1054 | + } |
|
| 1055 | + $this->resource = null; |
|
| 1056 | + } |
|
| 1057 | 1057 | |
| 1058 | - public function __destruct() { |
|
| 1059 | - $this->destroy(); |
|
| 1060 | - } |
|
| 1058 | + public function __destruct() { |
|
| 1059 | + $this->destroy(); |
|
| 1060 | + } |
|
| 1061 | 1061 | } |
| 1062 | 1062 | |
| 1063 | 1063 | if (!function_exists('imagebmp')) { |
| 1064 | - /** |
|
| 1065 | - * Output a BMP image to either the browser or a file |
|
| 1066 | - * |
|
| 1067 | - * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1068 | - * @author legend <[email protected]> |
|
| 1069 | - * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1070 | - * @author mgutt <[email protected]> |
|
| 1071 | - * @version 1.00 |
|
| 1072 | - * @param resource $im |
|
| 1073 | - * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1074 | - * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1075 | - * @param int $compression [optional] |
|
| 1076 | - * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1077 | - */ |
|
| 1078 | - function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1079 | - if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1080 | - $bit = 24; |
|
| 1081 | - } else if ($bit == 32) { |
|
| 1082 | - $bit = 24; |
|
| 1083 | - } |
|
| 1084 | - $bits = pow(2, $bit); |
|
| 1085 | - imagetruecolortopalette($im, true, $bits); |
|
| 1086 | - $width = imagesx($im); |
|
| 1087 | - $height = imagesy($im); |
|
| 1088 | - $colorsNum = imagecolorstotal($im); |
|
| 1089 | - $rgbQuad = ''; |
|
| 1090 | - if ($bit <= 8) { |
|
| 1091 | - for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1092 | - $colors = imagecolorsforindex($im, $i); |
|
| 1093 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1094 | - } |
|
| 1095 | - $bmpData = ''; |
|
| 1096 | - if ($compression == 0 || $bit < 8) { |
|
| 1097 | - $compression = 0; |
|
| 1098 | - $extra = ''; |
|
| 1099 | - $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1100 | - if ($padding % 4 != 0) { |
|
| 1101 | - $extra = str_repeat("\0", $padding); |
|
| 1102 | - } |
|
| 1103 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1104 | - $i = 0; |
|
| 1105 | - while ($i < $width) { |
|
| 1106 | - $bin = 0; |
|
| 1107 | - $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1108 | - for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1109 | - $index = imagecolorat($im, $i, $j); |
|
| 1110 | - $bin |= $index << $k; |
|
| 1111 | - $i++; |
|
| 1112 | - } |
|
| 1113 | - $bmpData .= chr($bin); |
|
| 1114 | - } |
|
| 1115 | - $bmpData .= $extra; |
|
| 1116 | - } |
|
| 1117 | - } // RLE8 |
|
| 1118 | - else if ($compression == 1 && $bit == 8) { |
|
| 1119 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1120 | - $lastIndex = "\0"; |
|
| 1121 | - $sameNum = 0; |
|
| 1122 | - for ($i = 0; $i <= $width; $i++) { |
|
| 1123 | - $index = imagecolorat($im, $i, $j); |
|
| 1124 | - if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1125 | - if ($sameNum != 0) { |
|
| 1126 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1127 | - } |
|
| 1128 | - $lastIndex = $index; |
|
| 1129 | - $sameNum = 1; |
|
| 1130 | - } else { |
|
| 1131 | - $sameNum++; |
|
| 1132 | - } |
|
| 1133 | - } |
|
| 1134 | - $bmpData .= "\0\0"; |
|
| 1135 | - } |
|
| 1136 | - $bmpData .= "\0\1"; |
|
| 1137 | - } |
|
| 1138 | - $sizeQuad = strlen($rgbQuad); |
|
| 1139 | - $sizeData = strlen($bmpData); |
|
| 1140 | - } else { |
|
| 1141 | - $extra = ''; |
|
| 1142 | - $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1143 | - if ($padding % 4 != 0) { |
|
| 1144 | - $extra = str_repeat("\0", $padding); |
|
| 1145 | - } |
|
| 1146 | - $bmpData = ''; |
|
| 1147 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1148 | - for ($i = 0; $i < $width; $i++) { |
|
| 1149 | - $index = imagecolorat($im, $i, $j); |
|
| 1150 | - $colors = imagecolorsforindex($im, $index); |
|
| 1151 | - if ($bit == 16) { |
|
| 1152 | - $bin = 0 << $bit; |
|
| 1153 | - $bin |= ($colors['red'] >> 3) << 10; |
|
| 1154 | - $bin |= ($colors['green'] >> 3) << 5; |
|
| 1155 | - $bin |= $colors['blue'] >> 3; |
|
| 1156 | - $bmpData .= pack("v", $bin); |
|
| 1157 | - } else { |
|
| 1158 | - $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1159 | - } |
|
| 1160 | - } |
|
| 1161 | - $bmpData .= $extra; |
|
| 1162 | - } |
|
| 1163 | - $sizeQuad = 0; |
|
| 1164 | - $sizeData = strlen($bmpData); |
|
| 1165 | - $colorsNum = 0; |
|
| 1166 | - } |
|
| 1167 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1168 | - $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1169 | - if ($fileName != '') { |
|
| 1170 | - $fp = fopen($fileName, 'wb'); |
|
| 1171 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1172 | - fclose($fp); |
|
| 1173 | - return true; |
|
| 1174 | - } |
|
| 1175 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1176 | - return true; |
|
| 1177 | - } |
|
| 1064 | + /** |
|
| 1065 | + * Output a BMP image to either the browser or a file |
|
| 1066 | + * |
|
| 1067 | + * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1068 | + * @author legend <[email protected]> |
|
| 1069 | + * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1070 | + * @author mgutt <[email protected]> |
|
| 1071 | + * @version 1.00 |
|
| 1072 | + * @param resource $im |
|
| 1073 | + * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1074 | + * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1075 | + * @param int $compression [optional] |
|
| 1076 | + * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1077 | + */ |
|
| 1078 | + function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1079 | + if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1080 | + $bit = 24; |
|
| 1081 | + } else if ($bit == 32) { |
|
| 1082 | + $bit = 24; |
|
| 1083 | + } |
|
| 1084 | + $bits = pow(2, $bit); |
|
| 1085 | + imagetruecolortopalette($im, true, $bits); |
|
| 1086 | + $width = imagesx($im); |
|
| 1087 | + $height = imagesy($im); |
|
| 1088 | + $colorsNum = imagecolorstotal($im); |
|
| 1089 | + $rgbQuad = ''; |
|
| 1090 | + if ($bit <= 8) { |
|
| 1091 | + for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1092 | + $colors = imagecolorsforindex($im, $i); |
|
| 1093 | + $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1094 | + } |
|
| 1095 | + $bmpData = ''; |
|
| 1096 | + if ($compression == 0 || $bit < 8) { |
|
| 1097 | + $compression = 0; |
|
| 1098 | + $extra = ''; |
|
| 1099 | + $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1100 | + if ($padding % 4 != 0) { |
|
| 1101 | + $extra = str_repeat("\0", $padding); |
|
| 1102 | + } |
|
| 1103 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1104 | + $i = 0; |
|
| 1105 | + while ($i < $width) { |
|
| 1106 | + $bin = 0; |
|
| 1107 | + $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1108 | + for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1109 | + $index = imagecolorat($im, $i, $j); |
|
| 1110 | + $bin |= $index << $k; |
|
| 1111 | + $i++; |
|
| 1112 | + } |
|
| 1113 | + $bmpData .= chr($bin); |
|
| 1114 | + } |
|
| 1115 | + $bmpData .= $extra; |
|
| 1116 | + } |
|
| 1117 | + } // RLE8 |
|
| 1118 | + else if ($compression == 1 && $bit == 8) { |
|
| 1119 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1120 | + $lastIndex = "\0"; |
|
| 1121 | + $sameNum = 0; |
|
| 1122 | + for ($i = 0; $i <= $width; $i++) { |
|
| 1123 | + $index = imagecolorat($im, $i, $j); |
|
| 1124 | + if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1125 | + if ($sameNum != 0) { |
|
| 1126 | + $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1127 | + } |
|
| 1128 | + $lastIndex = $index; |
|
| 1129 | + $sameNum = 1; |
|
| 1130 | + } else { |
|
| 1131 | + $sameNum++; |
|
| 1132 | + } |
|
| 1133 | + } |
|
| 1134 | + $bmpData .= "\0\0"; |
|
| 1135 | + } |
|
| 1136 | + $bmpData .= "\0\1"; |
|
| 1137 | + } |
|
| 1138 | + $sizeQuad = strlen($rgbQuad); |
|
| 1139 | + $sizeData = strlen($bmpData); |
|
| 1140 | + } else { |
|
| 1141 | + $extra = ''; |
|
| 1142 | + $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1143 | + if ($padding % 4 != 0) { |
|
| 1144 | + $extra = str_repeat("\0", $padding); |
|
| 1145 | + } |
|
| 1146 | + $bmpData = ''; |
|
| 1147 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1148 | + for ($i = 0; $i < $width; $i++) { |
|
| 1149 | + $index = imagecolorat($im, $i, $j); |
|
| 1150 | + $colors = imagecolorsforindex($im, $index); |
|
| 1151 | + if ($bit == 16) { |
|
| 1152 | + $bin = 0 << $bit; |
|
| 1153 | + $bin |= ($colors['red'] >> 3) << 10; |
|
| 1154 | + $bin |= ($colors['green'] >> 3) << 5; |
|
| 1155 | + $bin |= $colors['blue'] >> 3; |
|
| 1156 | + $bmpData .= pack("v", $bin); |
|
| 1157 | + } else { |
|
| 1158 | + $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1159 | + } |
|
| 1160 | + } |
|
| 1161 | + $bmpData .= $extra; |
|
| 1162 | + } |
|
| 1163 | + $sizeQuad = 0; |
|
| 1164 | + $sizeData = strlen($bmpData); |
|
| 1165 | + $colorsNum = 0; |
|
| 1166 | + } |
|
| 1167 | + $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1168 | + $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1169 | + if ($fileName != '') { |
|
| 1170 | + $fp = fopen($fileName, 'wb'); |
|
| 1171 | + fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1172 | + fclose($fp); |
|
| 1173 | + return true; |
|
| 1174 | + } |
|
| 1175 | + echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1176 | + return true; |
|
| 1177 | + } |
|
| 1178 | 1178 | } |
| 1179 | 1179 | |
| 1180 | 1180 | if (!function_exists('exif_imagetype')) { |
| 1181 | - /** |
|
| 1182 | - * Workaround if exif_imagetype does not exist |
|
| 1183 | - * |
|
| 1184 | - * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1185 | - * @param string $fileName |
|
| 1186 | - * @return string|boolean |
|
| 1187 | - */ |
|
| 1188 | - function exif_imagetype($fileName) { |
|
| 1189 | - if (($info = getimagesize($fileName)) !== false) { |
|
| 1190 | - return $info[2]; |
|
| 1191 | - } |
|
| 1192 | - return false; |
|
| 1193 | - } |
|
| 1181 | + /** |
|
| 1182 | + * Workaround if exif_imagetype does not exist |
|
| 1183 | + * |
|
| 1184 | + * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1185 | + * @param string $fileName |
|
| 1186 | + * @return string|boolean |
|
| 1187 | + */ |
|
| 1188 | + function exif_imagetype($fileName) { |
|
| 1189 | + if (($info = getimagesize($fileName)) !== false) { |
|
| 1190 | + return $info[2]; |
|
| 1191 | + } |
|
| 1192 | + return false; |
|
| 1193 | + } |
|
| 1194 | 1194 | } |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function widthTopLeft() { |
| 135 | 135 | $o = $this->getOrientation(); |
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 137 | 137 | switch ($o) { |
| 138 | 138 | case -1: |
| 139 | 139 | case 1: |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function heightTopLeft() { |
| 159 | 159 | $o = $this->getOrientation(); |
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 161 | 161 | switch ($o) { |
| 162 | 162 | case -1: |
| 163 | 163 | case 1: |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | if ($mimeType === null) { |
| 185 | 185 | $mimeType = $this->mimeType(); |
| 186 | 186 | } |
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 187 | + header('Content-Type: '.$mimeType); |
|
| 188 | 188 | return $this->_output(null, $mimeType); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | } |
| 203 | 203 | if ($filePath === null) { |
| 204 | 204 | if ($this->filePath === null) { |
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 205 | + $this->logger->error(__METHOD__.'(): called with no path.', array('app' => 'core')); |
|
| 206 | 206 | return false; |
| 207 | 207 | } else { |
| 208 | 208 | $filePath = $this->filePath; |
@@ -226,10 +226,10 @@ discard block |
||
| 226 | 226 | } |
| 227 | 227 | $isWritable = is_writable(dirname($filePath)); |
| 228 | 228 | if (!$isWritable) { |
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 229 | + $this->logger->error(__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.', array('app' => 'core')); |
|
| 230 | 230 | return false; |
| 231 | 231 | } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 232 | + $this->logger->error(__METHOD__.'(): File \''.$filePath.'\' is not writable.', array('app' => 'core')); |
|
| 233 | 233 | return false; |
| 234 | 234 | } |
| 235 | 235 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $imageType = IMAGETYPE_BMP; |
| 258 | 258 | break; |
| 259 | 259 | default: |
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 260 | + throw new Exception('\OC_Image::_output(): "'.$mimeType.'" is not supported when forcing a specific output format'); |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | return; |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,'.base64_encode($data)); |
|
| 440 | 440 | if (!$exif) { |
| 441 | 441 | return; |
| 442 | 442 | } |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | */ |
| 455 | 455 | public function fixOrientation() { |
| 456 | 456 | $o = $this->getOrientation(); |
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: '.$o, array('app' => 'core')); |
|
| 458 | 458 | $rotate = 0; |
| 459 | 459 | $flip = false; |
| 460 | 460 | switch ($o) { |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | $rotate = 90; |
| 490 | 490 | break; |
| 491 | 491 | } |
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 492 | + if ($flip && function_exists('imageflip')) { |
|
| 493 | 493 | imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
| 494 | 494 | } |
| 495 | 495 | if ($rotate) { |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | imagealphablending($this->resource, true); |
| 552 | 552 | imagesavealpha($this->resource, true); |
| 553 | 553 | } else { |
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: '.$imagePath, array('app' => 'core')); |
|
| 555 | 555 | } |
| 556 | 556 | break; |
| 557 | 557 | case IMAGETYPE_JPEG: |
@@ -559,10 +559,10 @@ discard block |
||
| 559 | 559 | if (getimagesize($imagePath) !== false) { |
| 560 | 560 | $this->resource = @imagecreatefromjpeg($imagePath); |
| 561 | 561 | } else { |
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: '.$imagePath, array('app' => 'core')); |
|
| 563 | 563 | } |
| 564 | 564 | } else { |
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 566 | 566 | } |
| 567 | 567 | break; |
| 568 | 568 | case IMAGETYPE_PNG: |
@@ -572,21 +572,21 @@ discard block |
||
| 572 | 572 | imagealphablending($this->resource, true); |
| 573 | 573 | imagesavealpha($this->resource, true); |
| 574 | 574 | } else { |
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 576 | 576 | } |
| 577 | 577 | break; |
| 578 | 578 | case IMAGETYPE_XBM: |
| 579 | 579 | if (imagetypes() & IMG_XPM) { |
| 580 | 580 | $this->resource = @imagecreatefromxbm($imagePath); |
| 581 | 581 | } else { |
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath, array('app' => 'core')); |
|
| 583 | 583 | } |
| 584 | 584 | break; |
| 585 | 585 | case IMAGETYPE_WBMP: |
| 586 | 586 | if (imagetypes() & IMG_WBMP) { |
| 587 | 587 | $this->resource = @imagecreatefromwbmp($imagePath); |
| 588 | 588 | } else { |
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: '.$imagePath, array('app' => 'core')); |
|
| 590 | 590 | } |
| 591 | 591 | break; |
| 592 | 592 | case IMAGETYPE_BMP: |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | */ |
| 697 | 697 | private function imagecreatefrombmp($fileName) { |
| 698 | 698 | if (!($fh = fopen($fileName, 'rb'))) { |
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName, array('app' => 'core')); |
|
| 700 | 700 | return false; |
| 701 | 701 | } |
| 702 | 702 | // read file header |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | // check for bitmap |
| 705 | 705 | if ($meta['type'] != 19778) { |
| 706 | 706 | fclose($fh); |
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 708 | 708 | return false; |
| 709 | 709 | } |
| 710 | 710 | // read image header |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
| 729 | 729 | if ($meta['imagesize'] < 1) { |
| 730 | 730 | fclose($fh); |
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 732 | 732 | return false; |
| 733 | 733 | } |
| 734 | 734 | } |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | // read color palette |
| 739 | 739 | $palette = array(); |
| 740 | 740 | if ($meta['bits'] < 16) { |
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 741 | + $palette = unpack('l'.$meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | 742 | // in rare cases the color value is signed |
| 743 | 743 | if ($palette[1] < 0) { |
| 744 | 744 | foreach ($palette as $i => $color) { |
@@ -751,7 +751,7 @@ discard block |
||
| 751 | 751 | if ($im == false) { |
| 752 | 752 | fclose($fh); |
| 753 | 753 | $this->logger->warning( |
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "'.$fileName.'" with dimensions '.$meta['width'].'x'.$meta['height'], |
|
| 755 | 755 | array('app' => 'core')); |
| 756 | 756 | return false; |
| 757 | 757 | } |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | $p = 0; |
| 761 | 761 | $vide = chr(0); |
| 762 | 762 | $y = $meta['height'] - 1; |
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 763 | + $error = 'imagecreatefrombmp: '.$fileName.' has not enough data!'; |
|
| 764 | 764 | // loop through the image data beginning with the lower left corner |
| 765 | 765 | while ($y >= 0) { |
| 766 | 766 | $x = 0; |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | $this->logger->warning($error, array('app' => 'core')); |
| 773 | 773 | return $im; |
| 774 | 774 | } |
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 775 | + $color = @unpack('V', $part.$vide); |
|
| 776 | 776 | break; |
| 777 | 777 | case 16: |
| 778 | 778 | if (!($part = substr($data, $p, 2))) { |
@@ -784,16 +784,16 @@ discard block |
||
| 784 | 784 | $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
| 785 | 785 | break; |
| 786 | 786 | case 8: |
| 787 | - $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 787 | + $color = @unpack('n', $vide.($data[$p] ?? '')); |
|
| 788 | 788 | $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
| 789 | 789 | break; |
| 790 | 790 | case 4: |
| 791 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 791 | + $color = @unpack('n', $vide.($data[floor($p)] ?? '')); |
|
| 792 | 792 | $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
| 793 | 793 | $color[1] = isset($palette[$color[1] + 1]) ? $palette[$color[1] + 1] : $palette[1]; |
| 794 | 794 | break; |
| 795 | 795 | case 1: |
| 796 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 796 | + $color = @unpack('n', $vide.($data[floor($p)] ?? '')); |
|
| 797 | 797 | switch (($p * 8) % 8) { |
| 798 | 798 | case 0: |
| 799 | 799 | $color[1] = $color[1] >> 7; |
@@ -824,7 +824,7 @@ discard block |
||
| 824 | 824 | break; |
| 825 | 825 | default: |
| 826 | 826 | fclose($fh); |
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: '.$fileName.' has '.$meta['bits'].' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | 828 | return false; |
| 829 | 829 | } |
| 830 | 830 | imagesetpixel($im, $x, $y, $color[1]); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | */ |
| 847 | 847 | public function resize($maxSize) { |
| 848 | 848 | if (!$this->valid()) { |
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 849 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 850 | 850 | return false; |
| 851 | 851 | } |
| 852 | 852 | $widthOrig = imagesx($this->resource); |
@@ -861,7 +861,7 @@ discard block |
||
| 861 | 861 | $newHeight = $maxSize; |
| 862 | 862 | } |
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 864 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 865 | 865 | return true; |
| 866 | 866 | } |
| 867 | 867 | |
@@ -872,14 +872,14 @@ discard block |
||
| 872 | 872 | */ |
| 873 | 873 | public function preciseResize(int $width, int $height): bool { |
| 874 | 874 | if (!$this->valid()) { |
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 875 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 876 | 876 | return false; |
| 877 | 877 | } |
| 878 | 878 | $widthOrig = imagesx($this->resource); |
| 879 | 879 | $heightOrig = imagesy($this->resource); |
| 880 | 880 | $process = imagecreatetruecolor($width, $height); |
| 881 | 881 | if ($process === false) { |
| 882 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 882 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 883 | 883 | return false; |
| 884 | 884 | } |
| 885 | 885 | |
@@ -892,7 +892,7 @@ discard block |
||
| 892 | 892 | |
| 893 | 893 | $res = imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
| 894 | 894 | if ($res === false) { |
| 895 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 895 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image', array('app' => 'core')); |
|
| 896 | 896 | imagedestroy($process); |
| 897 | 897 | return false; |
| 898 | 898 | } |
@@ -950,7 +950,7 @@ discard block |
||
| 950 | 950 | |
| 951 | 951 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
| 952 | 952 | if ($process == false) { |
| 953 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 953 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image '.$width.'x'.$height, array('app' => 'core')); |
|
| 954 | 954 | imagedestroy($process); |
| 955 | 955 | return false; |
| 956 | 956 | } |
@@ -970,12 +970,12 @@ discard block |
||
| 970 | 970 | */ |
| 971 | 971 | public function crop(int $x, int $y, int $w, int $h): bool { |
| 972 | 972 | if (!$this->valid()) { |
| 973 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 973 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 974 | 974 | return false; |
| 975 | 975 | } |
| 976 | 976 | $process = imagecreatetruecolor($w, $h); |
| 977 | 977 | if ($process == false) { |
| 978 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 978 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 979 | 979 | imagedestroy($process); |
| 980 | 980 | return false; |
| 981 | 981 | } |
@@ -989,7 +989,7 @@ discard block |
||
| 989 | 989 | |
| 990 | 990 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
| 991 | 991 | if ($process == false) { |
| 992 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 992 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image '.$w.'x'.$h, array('app' => 'core')); |
|
| 993 | 993 | imagedestroy($process); |
| 994 | 994 | return false; |
| 995 | 995 | } |
@@ -1009,7 +1009,7 @@ discard block |
||
| 1009 | 1009 | */ |
| 1010 | 1010 | public function fitIn($maxWidth, $maxHeight) { |
| 1011 | 1011 | if (!$this->valid()) { |
| 1012 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1012 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1013 | 1013 | return false; |
| 1014 | 1014 | } |
| 1015 | 1015 | $widthOrig = imagesx($this->resource); |
@@ -1019,7 +1019,7 @@ discard block |
||
| 1019 | 1019 | $newWidth = min($maxWidth, $ratio * $maxHeight); |
| 1020 | 1020 | $newHeight = min($maxHeight, $maxWidth / $ratio); |
| 1021 | 1021 | |
| 1022 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1022 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 1023 | 1023 | return true; |
| 1024 | 1024 | } |
| 1025 | 1025 | |
@@ -1032,7 +1032,7 @@ discard block |
||
| 1032 | 1032 | */ |
| 1033 | 1033 | public function scaleDownToFit($maxWidth, $maxHeight) { |
| 1034 | 1034 | if (!$this->valid()) { |
| 1035 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1035 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1036 | 1036 | return false; |
| 1037 | 1037 | } |
| 1038 | 1038 | $widthOrig = imagesx($this->resource); |
@@ -1090,7 +1090,7 @@ discard block |
||
| 1090 | 1090 | if ($bit <= 8) { |
| 1091 | 1091 | for ($i = 0; $i < $colorsNum; $i++) { |
| 1092 | 1092 | $colors = imagecolorsforindex($im, $i); |
| 1093 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1093 | + $rgbQuad .= chr($colors['blue']).chr($colors['green']).chr($colors['red'])."\0"; |
|
| 1094 | 1094 | } |
| 1095 | 1095 | $bmpData = ''; |
| 1096 | 1096 | if ($compression == 0 || $bit < 8) { |
@@ -1123,7 +1123,7 @@ discard block |
||
| 1123 | 1123 | $index = imagecolorat($im, $i, $j); |
| 1124 | 1124 | if ($index !== $lastIndex || $sameNum > 255) { |
| 1125 | 1125 | if ($sameNum != 0) { |
| 1126 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1126 | + $bmpData .= chr($sameNum).chr($lastIndex); |
|
| 1127 | 1127 | } |
| 1128 | 1128 | $lastIndex = $index; |
| 1129 | 1129 | $sameNum = 1; |
@@ -1164,15 +1164,15 @@ discard block |
||
| 1164 | 1164 | $sizeData = strlen($bmpData); |
| 1165 | 1165 | $colorsNum = 0; |
| 1166 | 1166 | } |
| 1167 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1167 | + $fileHeader = 'BM'.pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1168 | 1168 | $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
| 1169 | 1169 | if ($fileName != '') { |
| 1170 | 1170 | $fp = fopen($fileName, 'wb'); |
| 1171 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1171 | + fwrite($fp, $fileHeader.$infoHeader.$rgbQuad.$bmpData); |
|
| 1172 | 1172 | fclose($fp); |
| 1173 | 1173 | return true; |
| 1174 | 1174 | } |
| 1175 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1175 | + echo $fileHeader.$infoHeader.$rgbQuad.$bmpData; |
|
| 1176 | 1176 | return true; |
| 1177 | 1177 | } |
| 1178 | 1178 | } |