| Conditions | 13 |
| Paths | 44 |
| Total Lines | 68 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 38 | public function resize(string $path, int $size): string |
||
| 39 | { |
||
| 40 | $external = false; |
||
| 41 | |||
| 42 | // is external image? |
||
| 43 | if (Util::isExternalUrl($path)) { |
||
| 44 | $external = true; |
||
| 45 | $source = $path; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!$external) { |
||
| 49 | // source |
||
| 50 | $source = $this->config->getStaticPath().'/'.$path; |
||
| 51 | if (!Util::getFS()->exists($source)) { |
||
| 52 | throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path)); |
||
| 53 | } |
||
| 54 | // destination |
||
| 55 | // ie: .cache/images/thumbs |
||
| 56 | $thumbsDir = (string) $this->config->get('cache.dir').'/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size; |
||
| 57 | // ie: .cache/images/thumbs/img/logo.png |
||
| 58 | $imageRelPath = $thumbsDir.'/'.ltrim($path, '/'); |
||
| 59 | // full absolute path |
||
| 60 | $destination = $this->config->getDestinationDir().'/'.$imageRelPath; |
||
| 61 | if ((bool) $this->config->get('cache.external')) { |
||
| 62 | $destination = $imageRelPath; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // is size is already OK? |
||
| 67 | list($width, $height) = getimagesize($external ? $path : $source); |
||
| 68 | if ($width <= $size && $height <= $size) { |
||
| 69 | return $path; |
||
| 70 | } |
||
| 71 | |||
| 72 | // if GD extension is not installed: can't process |
||
| 73 | if (!extension_loaded('gd')) { |
||
| 74 | return $path; |
||
| 75 | } |
||
| 76 | |||
| 77 | // external image: return data URL |
||
| 78 | if ($external) { |
||
| 79 | try { |
||
| 80 | $img = ImageManager::make($path); |
||
| 81 | } catch (NotReadableException $e) { |
||
| 82 | throw new Exception(sprintf('Cannot get image "%s"', $path)); |
||
| 83 | } |
||
| 84 | |||
| 85 | return (string) $img->encode('data-url'); |
||
| 86 | } |
||
| 87 | |||
| 88 | // resize |
||
| 89 | if (!Util::getFS()->exists($destination)) { |
||
| 90 | $img = ImageManager::make($source); |
||
| 91 | $img->resize($size, null, function (\Intervention\Image\Constraint $constraint) { |
||
| 92 | $constraint->aspectRatio(); |
||
| 93 | $constraint->upsize(); |
||
| 94 | }); |
||
| 95 | // is a sub dir is necessary? |
||
| 96 | $imageSubDir = Util::getFS()->makePathRelative('/'.dirname($imageRelPath), '/'.$thumbsDir.'/'); |
||
| 97 | if (!empty($imageSubDir)) { |
||
| 98 | $destDir = $this->config->getCacheImagesThumbsPath().'/'.$size.'/'.$imageSubDir; |
||
| 99 | Util::getFS()->mkdir($destDir); |
||
| 100 | } |
||
| 101 | $img->save($destination); |
||
| 102 | } |
||
| 103 | |||
| 104 | // return relative path |
||
| 105 | return '/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size.'/'.ltrim($path, '/'); |
||
| 106 | } |
||
| 108 |