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