| Conditions | 11 |
| Paths | 32 |
| Total Lines | 66 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 50 | public function resize(string $path, int $size): string |
||
| 51 | { |
||
| 52 | // is not a local image? |
||
| 53 | if (Util::isExternalUrl($path)) { |
||
| 54 | $this->local = false; |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->path = '/'.ltrim($path, '/'); |
||
| 58 | $this->size = $size; |
||
| 59 | $returnPath = '/'.self::CACHE_IMAGES_THUMBS_DIR.'/'.$this->size.$this->path; |
||
| 60 | |||
| 61 | // source file |
||
| 62 | if ($this->local) { |
||
| 63 | $this->source = $this->config->getStaticPath().$this->path; |
||
| 64 | if (!Util::getFS()->exists($this->source)) { |
||
| 65 | throw new Exception(sprintf('Can\'t process "%s": file doesn\'t exists.', $this->source)); |
||
| 66 | } |
||
| 67 | } else { |
||
| 68 | $this->source = $path; |
||
| 69 | if (!Util::isUrlFileExists($this->source)) { |
||
| 70 | throw new Exception(sprintf('Can\'t process "%s": remonte file doesn\'t exists.', $this->source)); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | // images cache path |
||
| 75 | $this->cachePath = $this->config->getCachePath().'/'.self::CACHE_IMAGES_THUMBS_DIR.'/'; |
||
|
|
|||
| 76 | |||
| 77 | // is size is already OK? |
||
| 78 | list($width, $height) = getimagesize($this->source); |
||
| 79 | if ($width <= $this->size && $height <= $this->size) { |
||
| 80 | return $this->path; |
||
| 81 | } |
||
| 82 | |||
| 83 | // if GD extension is not installed: can't process |
||
| 84 | if (!extension_loaded('gd')) { |
||
| 85 | throw new Exception('GD extension is required to use images resize.'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->destination = $this->cachePath.$this->size.$this->path; |
||
| 89 | |||
| 90 | if (Util::getFS()->exists($this->destination)) { |
||
| 91 | return $returnPath; |
||
| 92 | } |
||
| 93 | |||
| 94 | // image object |
||
| 95 | try { |
||
| 96 | $img = ImageManager::make($this->source); |
||
| 97 | $img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) { |
||
| 98 | $constraint->aspectRatio(); |
||
| 99 | $constraint->upsize(); |
||
| 100 | }); |
||
| 101 | } catch (NotReadableException $e) { |
||
| 102 | throw new Exception(sprintf('Cannot get image "%s"', $this->path)); |
||
| 103 | } |
||
| 104 | |||
| 105 | // return data:image for external image |
||
| 106 | if (!$this->local) { |
||
| 107 | return (string) $img->encode('data-url'); |
||
| 108 | } |
||
| 109 | |||
| 110 | // save file |
||
| 111 | Util::getFS()->mkdir(dirname($this->destination)); |
||
| 112 | $img->save($this->destination); |
||
| 113 | |||
| 114 | // return new path |
||
| 115 | return $returnPath; |
||
| 116 | } |
||
| 118 |