Conditions | 13 |
Paths | 92 |
Total Lines | 98 |
Code Lines | 48 |
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 |
||
57 | public function resize(string $path, int $size): string |
||
58 | { |
||
59 | /** |
||
60 | * path = /images/img-1.jpg |
||
61 | * path = https://example.com/img-2.png. |
||
62 | */ |
||
63 | $this->path = $path; |
||
64 | $this->size = $size; |
||
65 | |||
66 | // is not a local image? |
||
67 | if (Util::isExternalUrl($this->path)) { |
||
68 | $this->local = false; |
||
69 | } |
||
70 | |||
71 | // source file |
||
72 | if ($this->local) { |
||
73 | $this->source = $this->config->getStaticPath().'/'.ltrim($this->path, '/'); |
||
74 | if (!Util::getFS()->exists($this->source)) { |
||
75 | throw new Exception(sprintf('Can\'t process "%s": file doesn\'t exists.', $this->source)); |
||
76 | } |
||
77 | } else { |
||
78 | $this->source = $this->path; |
||
79 | if (!Util::isUrlFileExists($this->source)) { |
||
80 | throw new Exception(sprintf('Can\'t process "%s": file doesn\'t exists.', $this->source)); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // images cache path |
||
85 | $this->cachePath = $this->config->getCachePath().'/'.self::CACHE_IMAGES_DIR; |
||
|
|||
86 | |||
87 | // is size is already OK? |
||
88 | list($width, $height) = getimagesize($this->source); |
||
89 | if ($width <= $this->size && $height <= $this->size) { |
||
90 | return $this->path; |
||
91 | } |
||
92 | |||
93 | // if GD extension is not installed: can't process |
||
94 | if (!extension_loaded('gd')) { |
||
95 | return $this->path; |
||
96 | } |
||
97 | |||
98 | // ie: .cache/images/thumbs/300 |
||
99 | $this->thumbsDir = $this->cacheDir.'/'.self::CACHE_IMAGES_THUMBS_DIR.'/'.$this->size; |
||
100 | // ie: .cache/images/thumbs/300/img/logo.png |
||
101 | $this->imageRelPath = $this->thumbsDir.'/'.ltrim($this->path, '/'); |
||
102 | // where to write the file |
||
103 | $this->destination = $this->config->getDestinationDir().'/'.$this->imageRelPath; |
||
104 | if ($this->config->isCacheDirIsAbsolute()) { |
||
105 | $this->destination = $this->imageRelPath; |
||
106 | } |
||
107 | |||
108 | if (Util::getFS()->exists($this->destination)) { |
||
109 | return $this->path; |
||
110 | } |
||
111 | |||
112 | // Image object |
||
113 | try { |
||
114 | $img = ImageManager::make($this->source); |
||
115 | |||
116 | // DEBUG |
||
117 | var_dump($img->width()); |
||
118 | var_dump($img->height()); |
||
119 | |||
120 | $img->resize($this->size, null, function (\Intervention\Image\Constraint $constraint) { |
||
121 | $constraint->aspectRatio(); |
||
122 | $constraint->upsize(); |
||
123 | }); |
||
124 | |||
125 | // DEBUG |
||
126 | var_dump($img->width()); |
||
127 | var_dump($img->height()); |
||
128 | //die(); |
||
129 | } catch (NotReadableException $e) { |
||
130 | throw new Exception(sprintf('Cannot get image "%s"', $this->path)); |
||
131 | } |
||
132 | |||
133 | // return data:image |
||
134 | if (!$this->local) { |
||
135 | return (string) $img->encode('data-url'); |
||
136 | } |
||
137 | |||
138 | // save/write file |
||
139 | // is a sub dir is necessary? |
||
140 | $imageSubDir = Util::getFS()->makePathRelative( |
||
141 | '/'.dirname($this->imageRelPath), |
||
142 | '/'.$this->thumbsDir.'/' |
||
143 | ); |
||
144 | if (!empty($imageSubDir)) { |
||
145 | $destDir = $this->config->getCacheImagesThumbsPath().'/'.$this->size.'/'.$imageSubDir; |
||
146 | Util::getFS()->mkdir($destDir); |
||
147 | } |
||
148 | //$img->save($this->destination); |
||
149 | |||
150 | // return relative path |
||
151 | return '/'.$this->config->get('cache.images.dir') |
||
152 | .'/'.(string) $this->config->get('cache.images.thumbs.dir') |
||
153 | .'/'.$this->size |
||
154 | .'/'.ltrim($this->path, '/'); |
||
155 | } |
||
171 |