| Conditions | 15 |
| Paths | 192 |
| Total Lines | 152 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 121 | public function display_smiley() |
||
| 122 | { |
||
| 123 | $text = $this->request->variable('text', '', true); |
||
| 124 | $smiley = $this->request->variable('smiley', 0); |
||
| 125 | $fontcolor = $this->request->variable('fontcolor', ''); |
||
| 126 | $shadowcolor = $this->request->variable('shadowcolor', ''); |
||
| 127 | $shieldshadow = $this->request->variable('shieldshadow', 0); |
||
| 128 | $fontwidth = 6; |
||
| 129 | $fontheight = 11; |
||
| 130 | $this->language->add_lang('smilie_creator', 'sylver35/smilecreator'); |
||
| 131 | |||
| 132 | // We have a random smilie ? |
||
| 133 | if ($smiley === 0) |
||
| 134 | { |
||
| 135 | $smiley = mt_rand(1, (int) $this->config['smiliecreator_count'] - 1); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Clean the text before |
||
| 139 | $text = $this->clean_text($text); |
||
| 140 | |||
| 141 | $output = []; |
||
| 142 | $nb = mb_strlen($text); |
||
| 143 | if ($nb > 40) |
||
| 144 | { |
||
| 145 | $output[0] = substr($text, 0, 40); |
||
| 146 | if ($nb > 120) |
||
| 147 | { |
||
| 148 | $output[1] = substr($text, 40, 40); |
||
| 149 | $output[2] = substr($text, 80, 37) . '...'; |
||
| 150 | } |
||
| 151 | else if ($nb > 80) |
||
| 152 | { |
||
| 153 | $output[1] = substr($text, 40, 40); |
||
| 154 | $output[2] = substr($text, 80, 40); |
||
| 155 | } |
||
| 156 | else |
||
| 157 | { |
||
| 158 | $output[1] = substr($text, 40, 40); |
||
| 159 | } |
||
| 160 | $char_count = 40; |
||
| 161 | } |
||
| 162 | else |
||
| 163 | { |
||
| 164 | $char_count = $nb; |
||
| 165 | $output[0] = $text; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Maybe we have to tweak here a bit. Depends on the font... |
||
| 169 | $in = ($char_count * $fontwidth) + 14; |
||
| 170 | $width = ($in < 60) ? 60 : $in; |
||
| 171 | $out = sizeof($output); |
||
| 172 | $height = ($out * $fontheight) + 35; |
||
| 173 | |||
| 174 | // Main work here |
||
| 175 | $img = @imagecreate($width, $height); |
||
| 176 | if ($img === false) |
||
| 177 | { |
||
| 178 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 179 | } |
||
| 180 | $smiley = @imagecreatefrompng($this->ext_path . 'images/smilie' . $smiley . '.png'); |
||
| 181 | if ($smiley === false) |
||
| 182 | { |
||
| 183 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 184 | } |
||
| 185 | $schild = @imagecreatefrompng($this->ext_path . 'images/schild.png'); |
||
| 186 | if ($schild === false) |
||
| 187 | { |
||
| 188 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 189 | } |
||
| 190 | |||
| 191 | $r1 = (int) hexdec(substr($fontcolor, 0, 2)); |
||
| 192 | $g1 = (int) hexdec(substr($fontcolor, 2, 2)); |
||
| 193 | $b1 = (int) hexdec(substr($fontcolor, 4, 2)); |
||
| 194 | $r2 = (int) hexdec(substr($shadowcolor, 0, 2)); |
||
| 195 | $g2 = (int) hexdec(substr($shadowcolor, 2, 2)); |
||
| 196 | $b2 = (int) hexdec(substr($shadowcolor, 4, 2)); |
||
| 197 | $bgcolor = imagecolorallocate($img, 111, 252, 134); |
||
| 198 | $txtcolor = imagecolorallocate($img, $r1, $g1, $b1); |
||
| 199 | $txt2color = imagecolorallocate($img, $r2, $g2, $b2); |
||
| 200 | $bocolor = imagecolorallocate($img, 0, 0, 0); |
||
| 201 | $schcolor = imagecolorallocate($img, 255, 255, 255); |
||
| 202 | $shadow1color = imagecolorallocate($img, 235, 235, 235); |
||
| 203 | $shadow2color = imagecolorallocate($img, 219, 219, 219); |
||
| 204 | $smileycolor = imagecolorsforindex($smiley, imagecolorat($smiley, 5, 14)); |
||
| 205 | |||
| 206 | imagesetpixel($schild, 1, 14, imagecolorallocate($schild, ($smileycolor["red"] + 52), ($smileycolor["green"] + 59), ($smileycolor["blue"] + 11))); |
||
| 207 | imagesetpixel($schild, 2, 14, imagecolorallocate($schild, ($smileycolor["red"] + 50), ($smileycolor["green"] + 52), ($smileycolor["blue"] + 50))); |
||
| 208 | imagesetpixel($schild, 1, 15, imagecolorallocate($schild, ($smileycolor["red"] + 50), ($smileycolor["green"] + 52), ($smileycolor["blue"] + 50))); |
||
| 209 | imagesetpixel($schild, 2, 15, imagecolorallocate($schild, ($smileycolor["red"] + 22), ($smileycolor["green"] + 21), ($smileycolor["blue"] + 35))); |
||
| 210 | imagesetpixel($schild, 1, 16, imagecolorat($smiley, 5, 14)); |
||
| 211 | imagesetpixel($schild, 2, 16, imagecolorat($smiley, 5, 14)); |
||
| 212 | imagesetpixel($schild, 5, 16, imagecolorallocate($schild, ($smileycolor["red"] + 22), ($smileycolor["green"] + 21), ($smileycolor["blue"] + 35))); |
||
| 213 | imagesetpixel($schild, 6, 16, imagecolorat($smiley, 5, 14)); |
||
| 214 | imagesetpixel($schild, 5, 15, imagecolorallocate($schild, ($smileycolor["red"] + 52), ($smileycolor["green"] + 59), ($smileycolor["blue"] + 11))); |
||
| 215 | imagesetpixel($schild, 6, 15, imagecolorallocate($schild, ($smileycolor["red"] + 50), ($smileycolor["green"] + 52), ($smileycolor["blue"] + 50))); |
||
| 216 | |||
| 217 | if (@imagecopy($img, $schild, ($width / 2 - 3), 0, 0, 0, 6, 4) === false) |
||
| 218 | { |
||
| 219 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 220 | } |
||
| 221 | if (@imagecopy($img, $schild, ($width / 2 - 3), ($height - 24), 0, 5, 9, 17) === false) |
||
| 222 | { |
||
| 223 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 224 | } |
||
| 225 | if (@imagecopy($img, $smiley, ($width / 2 + 6), ($height - 24), 0, 0, 23, 23) === false) |
||
| 226 | { |
||
| 227 | throw new http_exception(400, 'CREATE_ERROR'); |
||
| 228 | } |
||
| 229 | |||
| 230 | imagefilledrectangle($img, 0, 4, $width, ($height - 25), $bocolor); |
||
| 231 | imagefilledrectangle($img, 1, 5, ($width - 2), ($height - 26), $schcolor); |
||
| 232 | |||
| 233 | if ($shieldshadow) |
||
| 234 | { |
||
| 235 | imagefilledpolygon($img, array((($width - 2) / 2 + ((($width - 2) / 4) - 3)), 5, (($width - 2) / 2 + ((($width - 2) / 4) + 3)), 5, (($width - 2) / 2 - ((($width - 2) / 4) - 3)), ($height - 26), (($width - 2) / 2 - ((($width - 2) / 4) + 3)), ($height - 26)), 4, $shadow1color); |
||
| 236 | imagefilledpolygon($img, array((($width - 2) / 2 + ((($width - 2) / 4) + 4)), 5, ($width - 2), 5, ($width - 2), ($height - 26), (($width - 2) / 2 - ((($width - 2) / 4) - 4)), ($height - 26)), 4, $shadow2color); |
||
| 237 | } |
||
| 238 | |||
| 239 | $i = 0; |
||
| 240 | while ($i < sizeof($output)) |
||
| 241 | { |
||
| 242 | if ($shadowcolor) |
||
| 243 | { |
||
| 244 | imagestring($img, 2, (($width - (strlen(trim($output[$i])) * $fontwidth) - 2) / 2 + 1), ($i * $fontheight + 6), trim($output[$i]), $txt2color); |
||
| 245 | } |
||
| 246 | imagestring($img, 2, (($width - (strlen(trim($output[$i])) * $fontwidth) - 2) / 2), ($i * $fontheight + 5), trim($output[$i]), $txtcolor); |
||
| 247 | $i++; |
||
| 248 | } |
||
| 249 | |||
| 250 | imagecolortransparent($img, $bgcolor); |
||
| 251 | imageinterlace($img, 1); |
||
| 252 | |||
| 253 | // Send the image to the browser |
||
| 254 | header('Pragma: public'); |
||
| 255 | header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
||
| 256 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT'); |
||
| 257 | header('Cache-Control: maxage=10'); |
||
| 258 | header('Content-Type: image/png'); |
||
| 259 | header('Content-Disposition: inline; filename="smilecreator-' . $text . '.png"'); |
||
| 260 | $pic = imagepng($img, NULL, -1, PNG_ALL_FILTERS); |
||
| 261 | header('Content-Length: ' . filesize($pic)); |
||
| 262 | |||
| 263 | $this->template->assign_vars(array( |
||
| 264 | 'SMILEY' => $pic, |
||
| 265 | )); |
||
| 266 | |||
| 267 | $this->template->set_filenames(array( |
||
| 268 | 'body' => '@sylver35_smilecreator/smiley.html' |
||
| 269 | )); |
||
| 270 | |||
| 271 | garbage_collection(); |
||
| 272 | exit_handler(); |
||
| 273 | } |
||
| 321 |