Conditions | 15 |
Paths | 74 |
Total Lines | 78 |
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 |
||
147 | private function imageResizeMake($image, int $imagetype, string $imagePath, array $baseSize, array $imageSizeInfo): bool |
||
148 | { |
||
149 | // サイズ変更後の画像データを生成 |
||
150 | $campusX = $imageSizeInfo['reSizeX']; |
||
151 | $campusY = $imageSizeInfo['reSizeY']; |
||
152 | // くりぬきの場合(幅と高さが両方必要) |
||
153 | if ($imageSizeInfo['type'] == 'scoop' && !empty($baseSize['width']) && !empty($baseSize['height'])) { |
||
154 | $campusX = $baseSize['width']; |
||
155 | $campusY = $baseSize['height']; |
||
156 | } |
||
157 | $outImage = ImageCreateTrueColor($campusX, $campusY); |
||
158 | if (!$outImage) { |
||
159 | // リサイズ後の画像作成失敗 |
||
160 | return false; |
||
161 | } |
||
162 | |||
163 | switch ($imagetype) { |
||
164 | case IMAGETYPE_GIF: |
||
165 | //透過GIF対策 |
||
166 | $alpha = imagecolortransparent($image); // 元画像から透過色を取得する |
||
167 | imagefill($outImage, 0, 0, $alpha); // その色でキャンバスを塗りつぶす |
||
168 | imagecolortransparent($outImage, $alpha); // 塗りつぶした色を透過色として指定する |
||
169 | //!透過GIF対策 |
||
170 | break; |
||
171 | case IMAGETYPE_PNG: |
||
172 | //透過PNG対策 |
||
173 | //ブレンドモードを無効にする |
||
174 | imagealphablending($outImage, false); |
||
175 | //完全なアルファチャネル情報を保存するフラグをonにする |
||
176 | imagesavealpha($outImage, true); |
||
177 | //!透過PNG対策 |
||
178 | break; |
||
179 | default: |
||
180 | break; |
||
181 | } |
||
182 | |||
183 | $diffX = 0; |
||
184 | $diffY = 0; |
||
185 | // くりぬきの場合(幅と高さが両方必要) |
||
186 | if ($imageSizeInfo['type'] == 'scoop' && !empty($baseSize['width']) && !empty($baseSize['height'])) { |
||
187 | $diffX = ($imageSizeInfo['sizeX'] - ($baseSize['width'] * $imageSizeInfo['sizeX'] / $imageSizeInfo['reSizeX'])) / 2; |
||
188 | $diffY = ($imageSizeInfo['sizeY'] - ($baseSize['height'] * $imageSizeInfo['sizeY'] / $imageSizeInfo['reSizeY'])) / 2; |
||
189 | } |
||
190 | $ret = imagecopyresampled($outImage, $image, 0, 0, $diffX, $diffY, $imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY'], $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']); |
||
191 | if ($ret === false) { |
||
192 | // リサイズ失敗 |
||
193 | return false; |
||
194 | } |
||
195 | |||
196 | ImageDestroy($image); |
||
197 | |||
198 | // 画像保存 |
||
199 | $imagepathinfo = $this->getPathInfo($imagePath, $baseSize); |
||
200 | //resizeファイルを格納するディレクトリを作成 |
||
201 | if ( |
||
202 | !$this->mkdir($imagepathinfo['resize_dir'], 0777, true) |
||
203 | ) { |
||
204 | return false; |
||
205 | } |
||
206 | |||
207 | switch ($imagetype) { |
||
208 | case IMAGETYPE_GIF: |
||
209 | ImageGIF($outImage, $imagepathinfo['resize_filepath']); |
||
210 | break; |
||
211 | case IMAGETYPE_JPEG: |
||
212 | ImageJPEG($outImage, $imagepathinfo['resize_filepath'], 100); |
||
213 | break; |
||
214 | case IMAGETYPE_PNG: |
||
215 | ImagePNG($outImage, $imagepathinfo['resize_filepath']); |
||
216 | break; |
||
217 | default: |
||
218 | return false; |
||
219 | } |
||
220 | |||
221 | ImageDestroy($outImage); |
||
222 | |||
223 | return true; |
||
224 | } |
||
225 | |||
259 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.