| Conditions | 9 |
| Paths | 18 |
| Total Lines | 77 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 82 | public function resize(string $path, int $size): string |
||
| 83 | { |
||
| 84 | $this->path = $path; |
||
| 85 | $this->size = $size; |
||
| 86 | $this->source = $path; |
||
| 87 | |||
| 88 | $this->prepare(); |
||
| 89 | |||
| 90 | // is size is already OK? |
||
| 91 | list($width, $height) = getimagesize($this->source); |
||
| 92 | if ($width <= $this->size && $height <= $this->size) { |
||
| 93 | return $this->path; |
||
| 94 | } |
||
| 95 | |||
| 96 | // if GD extension is not installed: can't process |
||
| 97 | if (!extension_loaded('gd')) { |
||
| 98 | return $this->path; |
||
| 99 | } |
||
| 100 | |||
| 101 | // ie: .cache/images/thumbs/300 |
||
| 102 | $this->thumbsDir = $this->cacheDir |
||
| 103 | .'/'.(string) $this->config->get('cache.images.thumbs.dir').'/'.$this->size; |
||
| 104 | // ie: .cache/images/thumbs/300/img/logo.png |
||
| 105 | $this->imageRelPath = $this->thumbsDir.'/'.ltrim($this->path, '/'); |
||
| 106 | // where to write the file |
||
| 107 | $this->destination = $this->config->getDestinationDir().'/'.$this->imageRelPath; |
||
| 108 | if ($this->config->isCacheDirIsAbsolute()) { |
||
| 109 | $this->destination = $this->imageRelPath; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (Util::getFS()->exists($this->destination)) { |
||
| 113 | return $this->path; |
||
| 114 | } |
||
| 115 | |||
| 116 | // Image object |
||
| 117 | try { |
||
| 118 | $img = ImageManager::make($this->source); |
||
| 119 | |||
| 120 | // DEBUG |
||
| 121 | var_dump($img->width()); |
||
| 122 | var_dump($img->height()); |
||
| 123 | |||
| 124 | $img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) { |
||
| 125 | $constraint->aspectRatio(); |
||
| 126 | $constraint->upsize(); |
||
| 127 | }); |
||
| 128 | |||
| 129 | // DEBUG |
||
| 130 | var_dump($img->width()); |
||
| 131 | var_dump($img->height()); |
||
| 132 | die(); |
||
|
1 ignored issue
–
show
|
|||
| 133 | } catch (NotReadableException $e) { |
||
| 134 | throw new Exception(sprintf('Cannot get image "%s"', $this->path)); |
||
| 135 | } |
||
| 136 | |||
| 137 | // return data:image |
||
| 138 | if (!$this->local) { |
||
| 139 | return (string) $img->encode('data-url'); |
||
| 140 | } |
||
| 141 | |||
| 142 | // save/write file |
||
| 143 | // is a sub dir is necessary? |
||
| 144 | $imageSubDir = Util::getFS()->makePathRelative( |
||
| 145 | '/'.dirname($this->imageRelPath), |
||
| 146 | '/'.$this->thumbsDir.'/' |
||
| 147 | ); |
||
| 148 | if (!empty($imageSubDir)) { |
||
| 149 | $destDir = $this->config->getCacheImagesThumbsPath().'/'.$this->size.'/'.$imageSubDir; |
||
| 150 | Util::getFS()->mkdir($destDir); |
||
| 151 | } |
||
| 152 | $img->save($this->destination); |
||
| 153 | |||
| 154 | // return relative path |
||
| 155 | return '/'.$this->config->get('cache.images.dir') |
||
| 156 | .'/'.(string) $this->config->get('cache.images.thumbs.dir') |
||
| 157 | .'/'.$this->size |
||
| 158 | .'/'.ltrim($this->path, '/'); |
||
| 159 | } |
||
| 175 |