| Conditions | 57 |
| Paths | 2618 |
| Total Lines | 187 |
| Code Lines | 116 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 678 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.