| Total Complexity | 126 |
| Total Lines | 665 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FileBehavior often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class FileBehavior extends Behavior |
||
| 12 | {
|
||
| 13 | |||
| 14 | /** |
||
| 15 | * @inheritdoc |
||
| 16 | */ |
||
| 17 | protected $_defaultConfig = [ |
||
| 18 | 'library' => 'gd', |
||
| 19 | 'types' => [ // Default allowed types |
||
| 20 | 'image/jpeg', |
||
| 21 | 'image/jpg', |
||
| 22 | 'image/pjpeg', |
||
| 23 | 'image/pjpg', |
||
| 24 | 'image/png', |
||
| 25 | 'image/x-png', |
||
| 26 | 'image/gif', |
||
| 27 | 'image/webp', |
||
| 28 | ], |
||
| 29 | 'extensions' => [ // Default allowed extensions |
||
| 30 | 'jpeg', |
||
| 31 | 'jpg', |
||
| 32 | 'pjpg', |
||
| 33 | 'pjpeg', |
||
| 34 | 'png', |
||
| 35 | 'gif', |
||
| 36 | 'webp', |
||
| 37 | ], |
||
| 38 | 'path' => 'files', |
||
| 39 | 'background' => [255, 255, 255, 127], |
||
| 40 | 'watermark' => '', |
||
| 41 | 'thumbs' => [], |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Array of files to upload |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $_files = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | public function initialize(array $config) |
||
| 55 | {
|
||
| 56 | $this->_config = []; |
||
| 57 | |||
| 58 | foreach ($config as $field => $fieldOptions) {
|
||
| 59 | if (is_array($fieldOptions)) {
|
||
| 60 | $this->_config[$this->getTable()->getAlias()][$field] = array_merge($this->_defaultConfig, $fieldOptions); |
||
| 61 | } else {
|
||
| 62 | $field = $fieldOptions; |
||
| 63 | |||
| 64 | $this->_config[$this->getTable()->getAlias()][$field] = $this->_defaultConfig; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public function beforeMarshal(Event $event, $data = [], $options = []) |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritdoc |
||
| 99 | */ |
||
| 100 | public function afterSave(Event $event, EntityInterface $entity, $options = []) |
||
| 101 | {
|
||
| 102 | $this->prepareFile($entity); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritdoc |
||
| 107 | */ |
||
| 108 | public function beforeDelete(Event $event, EntityInterface $entity) |
||
| 109 | {
|
||
| 110 | return $this->deleteFile($event); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Copy file to destination and if field (image) has configurations for thumbs, then create them. |
||
| 115 | * |
||
| 116 | * @param EntityInterface $entity Entity |
||
| 117 | */ |
||
| 118 | public function prepareFile(EntityInterface $entity) |
||
| 119 | {
|
||
| 120 | foreach ($this->_files as $fieldName => $fieldOptions) {
|
||
| 121 | // Path to default file |
||
| 122 | $fileName = $fieldOptions['path'] . DS . $this->_files[$fieldName]['name']; |
||
| 123 | |||
| 124 | if (move_uploaded_file($this->_files[$fieldName]['tmp_name'], $fileName) || (file_exists($this->_files[$fieldName]['tmp_name']) && rename($this->_files[$fieldName]['tmp_name'], $fileName))) {
|
||
| 125 | if (mb_strpos($this->_files[$fieldName]['type'], 'image/') !== false && in_array(mb_strtolower($this->_files[$fieldName]['type']), $this->_config[$this->getTable()->getAlias()][$fieldName]['types'])) {
|
||
| 126 | $this->prepareThumbs($fileName, $this->_config[$this->getTable()->getAlias()][$fieldName]); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Delete file with created thumbs |
||
| 134 | * |
||
| 135 | * @param Event $event Reference to event |
||
| 136 | * @return boolean True if is success |
||
| 137 | */ |
||
| 138 | public function deleteFile(Event $event) |
||
| 139 | {
|
||
| 140 | // Get field list of model schema |
||
| 141 | $modelSchema = $model->schema(); |
||
| 142 | |||
| 143 | foreach ($this->settings[$model->alias] as $fieldName => $fieldOptions) {
|
||
| 144 | // Check is field in model schema |
||
| 145 | if (isset($modelSchema[$fieldName])) {
|
||
| 146 | $dataField = $model->findById($model->id); |
||
| 147 | |||
| 148 | if (is_array($dataField) && !empty($dataField[$model->alias][$fieldName])) {
|
||
| 149 | // Pattern for original file with thumbs |
||
| 150 | $filePattern = $this->settings[$model->alias][$fieldName]['path'] . DS . substr($dataField[$model->alias][$fieldName], 0, 14); |
||
| 151 | |||
| 152 | foreach (glob($filePattern . '*') as $fileName) {
|
||
| 153 | // Remove file |
||
| 154 | @unlink($fileName); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return true; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Generate thumbs by names with parameters |
||
| 165 | * |
||
| 166 | * @param string $originalFile Path to original file |
||
| 167 | * @param array $thumbParams Settings for uploaded files |
||
| 168 | * @return boolean Output image to save file |
||
| 169 | */ |
||
| 170 | public function prepareThumbs($originalFile, $settingParams) |
||
| 171 | {
|
||
| 172 | if (is_file($originalFile) && is_array($settingParams)) {
|
||
| 173 | // Check image library |
||
| 174 | if (!extension_loaded($settingParams['library'])) {
|
||
| 175 | throw new LibraryException(__d('file', 'The library identified by {0} is not loaded!', $settingParams['library']));
|
||
| 176 | } |
||
| 177 | |||
| 178 | // Get extension from original file |
||
| 179 | $fileExtension = $this->getExtension($originalFile); |
||
| 180 | |||
| 181 | switch ($settingParams['library']) {
|
||
| 182 | // Get image resource |
||
| 183 | case 'gd': |
||
| 184 | switch ($fileExtension) {
|
||
| 185 | case 'gif': |
||
| 186 | $sourceImage = imagecreatefromgif($originalFile); |
||
| 187 | |||
| 188 | break; |
||
| 189 | case 'png': |
||
| 190 | $sourceImage = imagecreatefrompng($originalFile); |
||
| 191 | |||
| 192 | break; |
||
| 193 | case 'webp': |
||
| 194 | $sourceImage = imagecreatefromwebp($originalFile); |
||
| 195 | |||
| 196 | break; |
||
| 197 | default: |
||
| 198 | ini_set('gd.jpeg_ignore_warning', 1);
|
||
| 199 | |||
| 200 | $sourceImage = imagecreatefromjpeg($originalFile); |
||
| 201 | |||
| 202 | break; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Get original width and height |
||
| 206 | $originalWidth = imagesx($sourceImage); |
||
| 207 | $originalHeight = imagesy($sourceImage); |
||
| 208 | |||
| 209 | break; |
||
| 210 | case 'imagick': |
||
| 211 | $sourceImage = new \Imagick($originalFile); |
||
| 212 | |||
| 213 | // Get original width and height |
||
| 214 | $originalWidth = $sourceImage->getimagewidth(); |
||
| 215 | $originalHeight = $sourceImage->getimageheight(); |
||
| 216 | |||
| 217 | break; |
||
| 218 | default: |
||
| 219 | throw new LibraryException(__d('file', 'The library identified by {0} it is not known as image processing!', $settingParams['library']));
|
||
| 220 | } |
||
| 221 | |||
| 222 | $offsetX = 0; |
||
| 223 | $offsetY = 0; |
||
| 224 | |||
| 225 | $cropX = 0; |
||
| 226 | $cropY = 0; |
||
| 227 | |||
| 228 | foreach ($settingParams['thumbs'] as $thumbName => $thumbParam) {
|
||
| 229 | if (is_array($thumbParam)) {
|
||
| 230 | if (isset($thumbParam['width']) && is_array($thumbParam['width']) && count($thumbParam['width']) === 1) {
|
||
| 231 | list($newWidth, $newHeight) = $this->_byWidth($originalWidth, $originalHeight, $thumbParam['width'][0]); |
||
| 232 | } elseif (isset($thumbParam['height']) && is_array($thumbParam['height']) && count($thumbParam['height']) === 1) {
|
||
| 233 | list($newWidth, $newHeight) = $this->_byHeight($originalWidth, $originalHeight, $thumbParam['height'][0]); |
||
| 234 | } elseif (isset($thumbParam['shorter']) && is_array($thumbParam['shorter']) && count($thumbParam['shorter']) === 2) {
|
||
| 235 | list($newWidth, $newHeight) = $this->_byShorter($originalWidth, $originalHeight, $thumbParam['shorter'][0], $thumbParam['shorter'][1]); |
||
| 236 | } elseif (isset($thumbParam['longer']) && is_array($thumbParam['longer']) && count($thumbParam['longer']) === 2) {
|
||
| 237 | list($newWidth, $newHeight) = $this->_byLonger($originalWidth, $originalHeight, $thumbParam['longer'][0], $thumbParam['longer'][1]); |
||
| 238 | } elseif (isset($thumbParam['fit']) && is_array($thumbParam['fit']) && count($thumbParam['fit']) === 2) {
|
||
| 239 | list($newWidth, $newHeight, $offsetX, $offsetY, $cropX, $cropY) = $this->_byFit($originalWidth, $originalHeight, $thumbParam['fit'][0], $thumbParam['fit'][1]); |
||
| 240 | } elseif (isset($thumbParam['fit']) && is_array($thumbParam['fit']) && count($thumbParam['fit']) === 3) {
|
||
| 241 | list($newWidth, $newHeight, $offsetX, $offsetY, $cropX, $cropY) = $this->_byFit($originalWidth, $originalHeight, $thumbParam['fit'][0], $thumbParam['fit'][1], $thumbParam['fit'][2]); |
||
| 242 | } elseif (isset($thumbParam['square']) && is_array($thumbParam['square']) && count($thumbParam['square']) === 1) {
|
||
| 243 | list($newWidth, $newHeight, $offsetX, $offsetY, $cropX, $cropY) = $this->_bySquare($originalWidth, $originalHeight, $thumbParam['square'][0]); |
||
| 244 | } elseif (isset($thumbParam['square']) && is_array($thumbParam['square']) && count($thumbParam['square']) === 2) {
|
||
| 245 | list($newWidth, $newHeight, $offsetX, $offsetY, $cropX, $cropY) = $this->_bySquare($originalWidth, $originalHeight, $thumbParam['square'][0], $thumbParam['square'][1]); |
||
| 246 | } else {
|
||
| 247 | throw new ThumbsException(__d('file', 'Unknown type of creating thumbnails!'));
|
||
| 248 | } |
||
| 249 | |||
| 250 | $thumbFile = str_replace('default', $thumbName, $originalFile);
|
||
| 251 | |||
| 252 | switch ($settingParams['library']) {
|
||
| 253 | // Get image resource |
||
| 254 | case 'gd': |
||
| 255 | $newImage = imagecreatetruecolor($newWidth, $newHeight); |
||
| 256 | |||
| 257 | if (is_array($settingParams['background'])) {
|
||
| 258 | // Set background color and transparent indicates |
||
| 259 | imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, $settingParams['background'][0], $settingParams['background'][1], $settingParams['background'][2], $settingParams['background'][3])); |
||
| 260 | } |
||
| 261 | |||
| 262 | imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); |
||
| 263 | |||
| 264 | if ((isset($thumbParam['square']) && is_array($thumbParam['square'])) || (isset($thumbParam['fit']) && is_array($thumbParam['fit']))) {
|
||
| 265 | $fitImage = imagecreatetruecolor($newWidth + (2 * $offsetX) - (2 * $cropX), $newHeight + (2 * $offsetY) - (2 * $cropY)); |
||
| 266 | |||
| 267 | if (is_array($settingParams['background'])) {
|
||
| 268 | // Set background color and transparent indicates |
||
| 269 | imagefill($fitImage, 0, 0, imagecolorallocatealpha($fitImage, $settingParams['background'][0], $settingParams['background'][1], $settingParams['background'][2], $settingParams['background'][3])); |
||
| 270 | } |
||
| 271 | |||
| 272 | imagecopyresampled($fitImage, $newImage, $offsetX, $offsetY, $cropX, $cropY, $newWidth, $newHeight, $newWidth, $newHeight); |
||
| 273 | |||
| 274 | $newImage = $fitImage; |
||
| 275 | } |
||
| 276 | |||
| 277 | imagealphablending($newImage, false); |
||
| 278 | imagesavealpha($newImage, true); |
||
| 279 | |||
| 280 | // Watermark |
||
| 281 | if (isset($thumbParam['watermark']) && ($watermarkSource = file_get_contents($settingParams['watermark'])) !== false) {
|
||
| 282 | $watermarkImage = imagecreatefromstring($watermarkSource); |
||
| 283 | |||
| 284 | list($watermarkPositionX, $watermarkPositionY) = $this->getPosition(imagesx($newImage), imagesy($newImage), imagesx($watermarkImage), imagesy($watermarkImage), $offsetX, $offsetY, $thumbParam['watermark']); |
||
| 285 | |||
| 286 | // Set transparent |
||
| 287 | imagealphablending($newImage, true); |
||
| 288 | imagecopy($newImage, $watermarkImage, $watermarkPositionX, $watermarkPositionY, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage)); |
||
| 289 | } |
||
| 290 | |||
| 291 | // Set resource file type |
||
| 292 | switch ($fileExtension) {
|
||
| 293 | case 'gif': |
||
| 294 | imagegif($newImage, $thumbFile); |
||
| 295 | |||
| 296 | break; |
||
| 297 | case 'png': |
||
| 298 | imagepng($newImage, $thumbFile); |
||
| 299 | |||
| 300 | break; |
||
| 301 | case 'webp': |
||
| 302 | imagewebp($newImage, $thumbFile); |
||
| 303 | |||
| 304 | break; |
||
| 305 | default: |
||
| 306 | imagejpeg($newImage, $thumbFile, 100); |
||
| 307 | |||
| 308 | break; |
||
| 309 | } |
||
| 310 | |||
| 311 | break; |
||
| 312 | case 'imagick': |
||
| 313 | $newImage = $sourceImage->clone(); |
||
| 314 | |||
| 315 | $newImage->scaleimage($newWidth, $newHeight); |
||
| 316 | $newImage->setimagebackgroundcolor('transparent');
|
||
| 317 | $newImage->extentimage($newWidth + (2 * $offsetX), $newHeight + (2 * $offsetY), -$offsetX, -$offsetY); |
||
| 318 | |||
| 319 | if ((isset($thumbParam['square']) && is_array($thumbParam['square'])) || (isset($thumbParam['fit']) && is_array($thumbParam['fit']))) {
|
||
| 320 | $newImage->cropimage($newWidth + (2 * $offsetX) - (2 * $cropX), $newHeight + (2 * $offsetY) - (2 * $cropY), $cropX, $cropY); |
||
| 321 | } |
||
| 322 | |||
| 323 | // Watermark |
||
| 324 | if (isset($thumbParam['watermark']) && ($watermarkSource = file_get_contents($settingParams['watermark'])) !== false) {
|
||
| 325 | $watermarkImage = new \Imagick(); |
||
| 326 | $watermarkImage->readimageblob($watermarkSource); |
||
| 327 | |||
| 328 | list($watermarkPositionX, $watermarkPositionY) = $this->getPosition($newWidth, $newHeight, $watermarkImage->getimagewidth(), $watermarkImage->getimageheight(), $offsetX, $offsetY, $thumbParam['watermark']); |
||
| 329 | |||
| 330 | $newImage->compositeimage($watermarkImage, \Imagick::COMPOSITE_OVER, $watermarkPositionX, $watermarkPositionY); |
||
| 331 | } |
||
| 332 | |||
| 333 | // Set object file type |
||
| 334 | switch ($fileExtension) {
|
||
| 335 | case 'gif': |
||
| 336 | $newImage->setImageFormat('gif');
|
||
| 337 | |||
| 338 | break; |
||
| 339 | case 'png': |
||
| 340 | $newImage->setImageFormat('png');
|
||
| 341 | |||
| 342 | break; |
||
| 343 | case 'webp': |
||
| 344 | $newImage->setImageFormat('webp');
|
||
| 345 | |||
| 346 | break; |
||
| 347 | default: |
||
| 348 | $newImage->setImageFormat('jpg');
|
||
| 349 | |||
| 350 | break; |
||
| 351 | } |
||
| 352 | |||
| 353 | $newImage->writeimage($thumbFile); |
||
| 354 | $newImage->clear(); |
||
| 355 | |||
| 356 | break; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get extension from original name |
||
| 365 | * |
||
| 366 | * @param string $originalName Name of original file |
||
| 367 | * @return string Extension of uploaded file |
||
| 368 | */ |
||
| 369 | public function getExtension($originalName) |
||
| 370 | {
|
||
| 371 | $fileExtension = pathinfo(mb_strtolower($originalName), PATHINFO_EXTENSION); |
||
| 372 | |||
| 373 | switch ($fileExtension) {
|
||
| 374 | case 'jpg': |
||
| 375 | case 'jpeg': |
||
| 376 | case 'pjpg': |
||
| 377 | case 'pjpeg': |
||
| 378 | // Standarize JPEG image file extension |
||
| 379 | return 'jpg'; |
||
| 380 | |||
| 381 | break; |
||
| 382 | default: |
||
| 383 | return $fileExtension; |
||
| 384 | |||
| 385 | break; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get position of watermark image |
||
| 391 | * |
||
| 392 | * @param integer $newWidth New width of uploaded image |
||
| 393 | * @param integer $newHeight New height of uploaded image |
||
| 394 | * @param integer $watermarkWidth Original width of watermark image |
||
| 395 | * @param integer $watermarkHeight Original height of watermark image |
||
| 396 | * @param integer $offsetX Horizontal offset |
||
| 397 | * @param integer $offsetY Vertical offset |
||
| 398 | * @param integer $positionValue Value for position watermark, value between 1 and 9 |
||
| 399 | * @return array Coordinates of position watermark |
||
| 400 | */ |
||
| 401 | public function getPosition($newWidth, $newHeight, $watermarkWidth, $watermarkHeight, $offsetX = 0, $offsetY = 0, $positionValue = 1) |
||
| 402 | {
|
||
| 403 | switch (intval($positionValue)) {
|
||
| 404 | case 1: // Top left |
||
| 405 | return [$offsetX, $offsetY]; |
||
| 406 | |||
| 407 | break; |
||
| 408 | case 2: // Top center |
||
| 409 | return [($newWidth / 2) - ($watermarkWidth / 2), 0 + $offsetY]; |
||
| 410 | |||
| 411 | break; |
||
| 412 | case 3: // Top right |
||
| 413 | return [($newWidth - $watermarkWidth) - $offsetX, 0 + $offsetY]; |
||
| 414 | |||
| 415 | break; |
||
| 416 | case 4: // Middle left |
||
| 417 | return [$offsetX, ($newHeight / 2) - ($watermarkHeight / 2)]; |
||
| 418 | |||
| 419 | break; |
||
| 420 | case 5: // Middle center |
||
| 421 | return [($newWidth / 2) - ($watermarkWidth / 2), ($newHeight / 2) - ($watermarkHeight / 2)]; |
||
| 422 | |||
| 423 | break; |
||
| 424 | case 6: // Middle right |
||
| 425 | return [($newWidth - $watermarkWidth) - $offsetX, ($newHeight / 2) - ($watermarkHeight / 2)]; |
||
| 426 | |||
| 427 | break; |
||
| 428 | case 7: // Bottom left |
||
| 429 | return [$offsetX, ($newHeight - $watermarkHeight) - $offsetY]; |
||
| 430 | |||
| 431 | break; |
||
| 432 | case 8: // Bottom center |
||
| 433 | return [($newWidth / 2) - ($watermarkWidth / 2), ($newHeight - $watermarkHeight) - $offsetY]; |
||
| 434 | |||
| 435 | break; |
||
| 436 | case 9: // Bottom right |
||
| 437 | return [($newWidth - $watermarkWidth) - $offsetX, ($newHeight - $watermarkHeight) - $offsetY]; |
||
| 438 | |||
| 439 | break; |
||
| 440 | default: |
||
| 441 | return [$offsetX, $offsetY]; |
||
| 442 | |||
| 443 | break; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Generate random name of uploaded file. |
||
| 449 | * If action is for update with not used file then it will be removed. |
||
| 450 | * |
||
| 451 | * @todo Prepare method for working without primary key field |
||
| 452 | * @todo Generate names of files by user method |
||
| 453 | * @param array $data File data |
||
| 454 | * @param string $fieldName Name of file field name |
||
| 455 | * @return string New name of file |
||
| 456 | */ |
||
| 457 | protected function _prepareName($data, $fieldName) |
||
| 458 | {
|
||
| 459 | $name = Text::uuid() . '_default.' . $this->getExtension($this->_files[$fieldName]['name']); |
||
| 460 | |||
| 461 | return $name; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set path to directory for save uploaded files. |
||
| 466 | * If directory isn't exists, will be created with full privileges. |
||
| 467 | * |
||
| 468 | * @param string $dirPath Path to directory |
||
| 469 | * @return string Path to directory |
||
| 470 | */ |
||
| 471 | protected function _prepareDir($dirPath) |
||
| 472 | {
|
||
| 473 | $dirPath = WWW_ROOT . str_replace('/', DS, $dirPath);
|
||
| 474 | |||
| 475 | if (!is_dir($dirPath) && mb_strlen($dirPath) > 0) {
|
||
| 476 | mkdir($dirPath, 0777, true); |
||
| 477 | } |
||
| 478 | |||
| 479 | chmod($dirPath, 0777); |
||
| 480 | |||
| 481 | return $dirPath; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Create image dimension by new width. |
||
| 486 | * |
||
| 487 | * @param integer $originalWidth Original width of uploaded image |
||
| 488 | * @param integer $originalHeight Original height of uploaded image |
||
| 489 | * @param integer $newWidth Set new image width |
||
| 490 | * @return array New width and height |
||
| 491 | */ |
||
| 492 | protected function _byWidth($originalWidth, $originalHeight, $newWidth) |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create image dimension by new height. |
||
| 508 | * |
||
| 509 | * @param integer $originalWidth Original width of uploaded image |
||
| 510 | * @param integer $originalHeight Original height of uploaded image |
||
| 511 | * @param integer $newHeight Set new image height |
||
| 512 | * @return array New width and height |
||
| 513 | */ |
||
| 514 | protected function _byHeight($originalWidth, $originalHeight, $newHeight) |
||
| 515 | {
|
||
| 516 | $newHeight = intval($newHeight); |
||
| 517 | |||
| 518 | if ($newHeight > $originalHeight) {
|
||
| 519 | $newHeight = $originalHeight; |
||
| 520 | $newWidth = $originalWidth; |
||
| 521 | } else {
|
||
| 522 | $newWidth = intval($newHeight * ($originalWidth / $originalHeight)); |
||
| 523 | } |
||
| 524 | |||
| 525 | return [$newWidth, $newHeight]; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Create image dimension by shorter side. |
||
| 530 | * |
||
| 531 | * @param integer $originalWidth Original width of uploaded image |
||
| 532 | * @param integer $originalHeight Original height of uploaded image |
||
| 533 | * @param integer $newWidth Set new image min width |
||
| 534 | * @param integer $newHeight Set new image min height |
||
| 535 | * @return array New width and height |
||
| 536 | */ |
||
| 537 | protected function _byShorter($originalWidth, $originalHeight, $newWidth, $newHeight) |
||
| 538 | {
|
||
| 539 | $newWidth = intval($newWidth); |
||
| 540 | $newHeight = intval($newHeight); |
||
| 541 | |||
| 542 | if ($originalWidth < $originalHeight) {
|
||
| 543 | list($newWidth, $newHeight) = $this->_byWidth($originalWidth, $originalHeight, $newWidth); |
||
| 544 | } else {
|
||
| 545 | list($newWidth, $newHeight) = $this->_byHeight($originalWidth, $originalHeight, $newHeight); |
||
| 546 | } |
||
| 547 | |||
| 548 | return [$newWidth, $newHeight]; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Create image dimension by longer side. |
||
| 553 | * |
||
| 554 | * @param integer $originalWidth Original width of uploaded image |
||
| 555 | * @param integer $originalHeight Original height of uploaded image |
||
| 556 | * @param integer $newWidth Set new image max width |
||
| 557 | * @param integer $newHeight Set new image max height |
||
| 558 | * @return array New width and height |
||
| 559 | */ |
||
| 560 | protected function _byLonger($originalWidth, $originalHeight, $newWidth, $newHeight) |
||
| 561 | {
|
||
| 562 | $newWidth = intval($newWidth); |
||
| 563 | $newHeight = intval($newHeight); |
||
| 564 | |||
| 565 | if ($originalWidth > $originalHeight) {
|
||
| 566 | list($newWidth, $newHeight) = $this->_byWidth($originalWidth, $originalHeight, $newWidth); |
||
| 567 | } else {
|
||
| 568 | list($newWidth, $newHeight) = $this->_byHeight($originalWidth, $originalHeight, $newHeight); |
||
| 569 | } |
||
| 570 | |||
| 571 | return [$newWidth, $newHeight]; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Create image dimension by fit. |
||
| 576 | * |
||
| 577 | * @param integer $originalWidth Original width of uploaded image |
||
| 578 | * @param integer $originalHeight Original height of uploaded image |
||
| 579 | * @param integer $newWidth Set new image width |
||
| 580 | * @param integer $newHeight Set new image height |
||
| 581 | * @param boolean $originalKeep Save original shape |
||
| 582 | * @return array New width and height and offsets of position with keeping original shape |
||
| 583 | */ |
||
| 584 | protected function _byFit($originalWidth, $originalHeight, $newWidth, $newHeight, $originalKeep = false) |
||
| 585 | {
|
||
| 586 | $newWidth = intval($newWidth); |
||
| 587 | $newHeight = intval($newHeight); |
||
| 588 | |||
| 589 | $offsetX = 0; |
||
| 590 | $offsetY = 0; |
||
| 591 | $cropX = 0; |
||
| 592 | $cropY = 0; |
||
| 593 | |||
| 594 | if ($originalKeep === true) {
|
||
| 595 | if ($originalWidth == $originalHeight) {
|
||
| 596 | $newSizes = $this->_byLonger($originalWidth, $originalHeight, min($newWidth, $newHeight), min($newWidth, $newHeight)); |
||
| 597 | } else {
|
||
| 598 | $newSizes = $this->_byLonger($originalWidth, $originalHeight, $newWidth, $newHeight); |
||
| 599 | |||
| 600 | if ($newWidth < $newSizes[0] || $newHeight < $newSizes[1]) {
|
||
| 601 | $newSizes = $this->_byShorter($originalWidth, $originalHeight, $newWidth, $newHeight); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | } else {
|
||
| 605 | if ($originalWidth == $originalHeight) {
|
||
| 606 | $newSizes = $this->_byShorter($originalWidth, $originalHeight, max($newWidth, $newHeight), max($newWidth, $newHeight)); |
||
| 607 | } else {
|
||
| 608 | $newSizes = $this->_byShorter($originalWidth, $originalHeight, $newWidth, $newHeight); |
||
| 609 | |||
| 610 | if ($newWidth > $newSizes[0] || $newHeight > $newSizes[1]) {
|
||
| 611 | $newSizes = $this->_byLonger($originalWidth, $originalHeight, $newWidth, $newHeight); |
||
| 612 | } |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | if ($newWidth < $newSizes[0]) {
|
||
| 617 | $cropX = ($newSizes[0] - $newWidth) / 2; |
||
| 618 | } else {
|
||
| 619 | $offsetX = ($newWidth - $newSizes[0]) / 2; |
||
| 620 | } |
||
| 621 | |||
| 622 | if ($newHeight < $newSizes[1]) {
|
||
| 623 | $cropY = ($newSizes[1] - $newHeight) / 2; |
||
| 624 | } else {
|
||
| 625 | $offsetY = ($newHeight - $newSizes[1]) / 2; |
||
| 626 | } |
||
| 627 | |||
| 628 | return [$newSizes[0], $newSizes[1], $offsetX, $offsetY, $cropX, $cropY]; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Create image dimension to square |
||
| 633 | * |
||
| 634 | * @param integer $originalWidth Original width of uploaded image |
||
| 635 | * @param integer $originalHeight Original height of uploaded image |
||
| 636 | * @param integer $newSide Set new image side |
||
| 637 | * @param boolean $originalKeep Save original shape |
||
| 638 | * @return array New width and height with coordinates of crop or offsets of position |
||
| 639 | */ |
||
| 640 | protected function _bySquare($originalWidth, $originalHeight, $newSide, $originalKeep = false) |
||
| 676 | } |
||
| 677 | } |
||
| 678 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.