1 | <?php |
||
2 | /** |
||
3 | * Created by Gorlum 15.08.2019 6:16 |
||
4 | */ |
||
5 | |||
6 | namespace Old; |
||
7 | |||
8 | use Exception; |
||
9 | |||
10 | class Avatar { |
||
11 | |||
12 | public static function sys_avatar_upload($subject_id, &$avatar_field, $prefix = 'avatar') { |
||
13 | global $config, $lang, $user; |
||
14 | |||
15 | try { |
||
16 | $avatar_filename = $fullsize_filename = SN_PATH_AVATAR . $prefix . '_' . $subject_id; |
||
17 | $avatar_filename .= '.png'; |
||
18 | $fullsize_filename .= '_full.png'; |
||
19 | if (sys_get_param_int('avatar_remove')) { |
||
20 | if (file_exists($avatar_filename) && !unlink($avatar_filename)) { |
||
21 | throw new Exception($lang['opt_msg_avatar_error_delete'], ERR_ERROR); |
||
22 | } |
||
23 | $avatar_field = 0; |
||
24 | throw new Exception($lang['opt_msg_avatar_removed'], ERR_NONE); |
||
25 | } elseif ($_FILES['avatar']['size']) { |
||
26 | if (!in_array($_FILES['avatar']['type'], array('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png')) || $_FILES['avatar']['size'] > 204800) { |
||
27 | throw new Exception($lang['opt_msg_avatar_error_unsupported'], ERR_WARNING); |
||
28 | } |
||
29 | |||
30 | if ($_FILES['avatar']['error']) { |
||
31 | throw new Exception(sprintf($lang['opt_msg_avatar_error_upload'], $_FILES['avatar']['error']), ERR_ERROR); |
||
32 | } |
||
33 | |||
34 | if (!($avatar_image = imagecreatefromstring(file_get_contents($_FILES['avatar']['tmp_name'])))) { |
||
35 | throw new Exception($lang['opt_msg_avatar_error_unsupported'], ERR_WARNING); |
||
36 | } |
||
37 | |||
38 | $avatar_size = getimagesize($_FILES['avatar']['tmp_name']); |
||
39 | $avatar_max_width = $config->avatar_max_width; |
||
40 | $avatar_max_height = $config->avatar_max_height; |
||
41 | if ($avatar_size[0] > $avatar_max_width || $avatar_size[1] > $avatar_max_height) { |
||
42 | $aspect_ratio = min($avatar_max_width / $avatar_size[0], $avatar_max_height / $avatar_size[1]); |
||
43 | $avatar_image_new = imagecreatetruecolor($avatar_size[0] * $aspect_ratio, $avatar_size[0] * $aspect_ratio); |
||
44 | $result = imagecopyresized($avatar_image_new, $avatar_image, 0, 0, 0, 0, $avatar_size[0] * $aspect_ratio, $avatar_size[0] * $aspect_ratio, $avatar_size[0], $avatar_size[1]); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
45 | imagedestroy($avatar_image); |
||
46 | $avatar_image = $avatar_image_new; |
||
47 | } |
||
48 | |||
49 | if (file_exists($avatar_filename) && !unlink($avatar_filename)) { |
||
50 | throw new Exception($lang['opt_msg_avatar_error_delete'], ERR_ERROR); |
||
51 | } |
||
52 | |||
53 | if (!imagepng($avatar_image, $avatar_filename, 9)) { |
||
54 | throw new Exception($lang['opt_msg_avatar_error_writing'], ERR_ERROR); |
||
55 | } |
||
56 | |||
57 | $avatar_field = 1; |
||
58 | imagedestroy($avatar_image); |
||
59 | throw new Exception($lang['opt_msg_avatar_uploaded'], ERR_NONE); |
||
60 | } |
||
61 | } catch (Exception $e) { |
||
62 | return array( |
||
63 | 'STATUS' => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR, |
||
64 | 'MESSAGE' => $e->getMessage() |
||
65 | ); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | } |
||
70 |