| Conditions | 11 |
| Paths | 88 |
| Total Lines | 64 |
| Code Lines | 42 |
| 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 declare(strict_types=1); |
||
| 115 | public function resizeAndCrop() |
||
| 116 | { |
||
| 117 | // check file extension |
||
| 118 | switch ($this->imageMimetype) { |
||
| 119 | case 'image/png': |
||
| 120 | $original = \imagecreatefrompng($this->sourceFile); |
||
| 121 | break; |
||
| 122 | case 'image/jpeg': |
||
| 123 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
| 124 | break; |
||
| 125 | case 'image/gif': |
||
| 126 | $original = \imagecreatefromgif($this->sourceFile); |
||
| 127 | break; |
||
| 128 | default: |
||
| 129 | return 'Unsupported format'; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!$original) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | // GET ORIGINAL IMAGE DIMENSIONS |
||
| 136 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
| 137 | |||
| 138 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
| 139 | $max_width_resize = $this->maxWidth; |
||
| 140 | $maxHeightResize = $this->maxHeight; |
||
| 141 | if ($original_w > $original_h) { |
||
| 142 | $max_height_ratio = $this->maxHeight / $original_h; |
||
| 143 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
| 144 | } else { |
||
| 145 | $max_width_ratio = $this->maxWidth / $original_w; |
||
| 146 | $maxHeightResize = (int)\round($original_h * $max_width_ratio); |
||
| 147 | } |
||
| 148 | if ($max_width_resize < $this->maxWidth) { |
||
| 149 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
| 150 | $maxHeightResize = (int)\round($this->maxHeight * $max_height_ratio); |
||
| 151 | $max_width_resize = $this->maxWidth; |
||
| 152 | } |
||
| 153 | |||
| 154 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
| 155 | $thumb = \imagecreatetruecolor($max_width_resize, $maxHeightResize); |
||
| 156 | if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $maxHeightResize, $original_w, $original_h)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
| 160 | $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight); |
||
| 161 | |||
| 162 | $max_width_offset = 0; |
||
| 163 | $max_height_offset = 0; |
||
| 164 | if ($this->maxWidth < $max_width_resize) { |
||
| 165 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
| 166 | } else { |
||
| 167 | $max_height_offset = (int)\round(($maxHeightResize - $this->maxHeight) / 2); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $maxHeightResize)) { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
| 174 | if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | |||
| 178 | return true; |
||
| 179 | } |
||
| 240 |