Total Complexity | 48 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like Resizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Resizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Resizer |
||
27 | { |
||
28 | public $sourceFile = ''; |
||
29 | public $endFile = ''; |
||
30 | public $maxWidth = 0; |
||
31 | public $maxHeight = 0; |
||
32 | public $imageMimetype = ''; |
||
33 | public $jpgQuality = 90; |
||
34 | public $mergeType = 0; |
||
35 | public $mergePos = 0; |
||
36 | public $degrees = 0; |
||
37 | public $error = ''; |
||
38 | |||
39 | /** |
||
40 | * resize image if size exceed given width/height |
||
41 | * @return string|bool |
||
42 | */ |
||
43 | public function resizeImage() |
||
44 | { |
||
45 | // check file extension |
||
46 | switch ($this->imageMimetype) { |
||
47 | case 'image/png': |
||
48 | $img = \imagecreatefrompng($this->sourceFile); |
||
49 | break; |
||
50 | case 'image/jpeg': |
||
51 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
52 | if (!$img) { |
||
53 | $img = \imagecreatefromstring(\file_get_contents($this->sourceFile)); |
||
54 | } |
||
55 | break; |
||
56 | case 'image/gif': |
||
57 | $img = \imagecreatefromgif($this->sourceFile); |
||
58 | break; |
||
59 | default: |
||
60 | return 'Unsupported format'; |
||
61 | } |
||
62 | |||
63 | $width = \imagesx($img); |
||
64 | $height = \imagesy($img); |
||
65 | |||
66 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
67 | // recalc image size based on this->maxWidth/this->maxHeight |
||
68 | $new_width = 0; |
||
69 | $new_height = 0; |
||
70 | if ($width > $height) { |
||
71 | if ($width < $this->maxWidth) { |
||
72 | $new_width = $width; |
||
73 | } else { |
||
74 | $new_width = $this->maxWidth; |
||
75 | $divisor = $width / $new_width; |
||
76 | $new_height = \floor($height / $divisor); |
||
77 | } |
||
78 | } elseif ($height < $this->maxHeight) { |
||
79 | $new_height = $height; |
||
80 | } else { |
||
81 | $new_height = $this->maxHeight; |
||
82 | $divisor = $height / $new_height; |
||
83 | $new_width = \floor($width / $divisor); |
||
84 | } |
||
85 | |||
86 | // Create a new temporary image. |
||
87 | $tmpimg = \imagecreatetruecolor($new_width, $new_height); |
||
88 | \imagealphablending($tmpimg, false); |
||
89 | \imagesavealpha($tmpimg, true); |
||
90 | |||
91 | // Copy and resize old image into new image. |
||
92 | \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
||
93 | |||
94 | \unlink($this->endFile); |
||
95 | //compressing the file |
||
96 | switch ($this->imageMimetype) { |
||
97 | case 'image/png': |
||
98 | \imagepng($tmpimg, $this->endFile, 0); |
||
99 | break; |
||
100 | case 'image/jpeg': |
||
101 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
102 | break; |
||
103 | case 'image/gif': |
||
104 | \imagegif($tmpimg, $this->endFile); |
||
105 | break; |
||
106 | } |
||
107 | |||
108 | // release the memory |
||
109 | \imagedestroy($tmpimg); |
||
110 | } else { |
||
111 | return 'copy'; |
||
112 | } |
||
113 | \imagedestroy($img); |
||
114 | |||
115 | return true; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return bool|string |
||
120 | */ |
||
121 | public function resizeAndCrop() |
||
122 | { |
||
123 | // check file extension |
||
124 | switch ($this->imageMimetype) { |
||
125 | case 'image/png': |
||
126 | $original = \imagecreatefrompng($this->sourceFile); |
||
127 | break; |
||
128 | case 'image/jpeg': |
||
129 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
130 | break; |
||
131 | case 'image/gif': |
||
132 | $original = \imagecreatefromgif($this->sourceFile); |
||
133 | break; |
||
134 | default: |
||
135 | return 'Unsupported format'; |
||
136 | } |
||
137 | |||
138 | if (!$original) { |
||
139 | return false; |
||
140 | } |
||
141 | // GET ORIGINAL IMAGE DIMENSIONS |
||
142 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
143 | |||
144 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
145 | $max_width_resize = $this->maxWidth; |
||
146 | $max_height_resize = $this->maxHeight; |
||
147 | if ($original_w > $original_h) { |
||
148 | $max_height_ratio = $this->maxHeight / $original_h; |
||
149 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
150 | } else { |
||
151 | $max_width_ratio = $this->maxWidth / $original_w; |
||
152 | $max_height_resize = (int)\round($original_h * $max_width_ratio); |
||
153 | } |
||
154 | if ($max_width_resize < $this->maxWidth) { |
||
155 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
156 | $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio); |
||
157 | $max_width_resize = $this->maxWidth; |
||
158 | } |
||
159 | |||
160 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
161 | $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize); |
||
162 | if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) { |
||
163 | return false; |
||
164 | } |
||
165 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
166 | $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight); |
||
167 | |||
168 | $max_width_offset = 0; |
||
169 | $max_height_offset = 0; |
||
170 | if ($this->maxWidth < $max_width_resize) { |
||
171 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
172 | } else { |
||
173 | $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2); |
||
174 | } |
||
175 | |||
176 | if (!imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) { |
||
177 | return false; |
||
178 | } |
||
179 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
180 | if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) { |
||
181 | return false; |
||
182 | } |
||
183 | |||
184 | return true; |
||
185 | } |
||
186 | |||
187 | public function mergeImage() |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @return bool|string |
||
247 | */ |
||
248 | public function rotateImage() |
||
297 |