]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatar.php
851ef23f4a257915a9b89ce6a2fbc68836f49066
[quix0rs-gnu-social.git] / actions / avatar.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class AvatarAction extends SettingsAction {
23
24         function show_form($msg=NULL, $success=false) {
25                 common_show_header(_t('Avatar'));
26                 $this->settings_menu();
27                 $this->message($msg, $success);
28
29                 $user = common_current_user();
30                 $profile = $user->getProfile();
31                 $original = $profile->getOriginal();
32
33                 if ($original) {
34                         common_element('img', array('src' => $original->url,
35                                                                                 'class' => 'avatar original',
36                                                                                 'width' => $original->width,
37                                                                                 'height' => $original->height));
38                 }
39
40                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
41
42                 if ($avatar) {
43                         common_element('img', array('src' => $avatar->url,
44                                                                                 'class' => 'avatar profile',
45                                                                                 'width' => AVATAR_PROFILE_SIZE,
46                                                                                 'height' => AVATAR_PROFILE_SIZE));
47                 }
48
49                 common_element_start('form', array('enctype' => 'multipart/form-data',
50                                                                                    'method' => 'POST',
51                                                                                    'id' => 'avatar',
52                                                                                    'action' =>
53                                                                                    common_local_url('avatar')));
54                 common_element('input', array('name' => 'MAX_FILE_SIZE',
55                                                                           'type' => 'hidden',
56                                                                           'id' => 'MAX_FILE_SIZE',
57                                                                           'value' => MAX_AVATAR_SIZE));
58                 common_element('input', array('name' => 'avatarfile',
59                                                                           'type' => 'file',
60                                                                           'id' => 'avatarfile'));
61                 common_element('input', array('name' => 'submit',
62                                                                           'type' => 'submit',
63                                                                           'id' => 'submit'),
64                                            _t('Upload'));
65                 common_element_end('form');
66         }
67
68         function handle_post() {
69
70                 switch ($_FILES['avatarfile']['error']) {
71                  case UPLOAD_ERR_OK: # success, jump out
72                         break;
73                  case UPLOAD_ERR_INI_SIZE:
74                  case UPLOAD_ERR_FORM_SIZE:
75                         $this->show_form(_t('That file is too big.'));
76                         return;
77                  case UPLOAD_ERR_PARTIAL:
78                         @unlink($_FILES['avatarfile']['tmp_name']);
79                         $this->show_form(_t('Partial upload.'));
80                         return;
81                  default:
82                         $this->show_form(_t('System error uploading file.'));
83                         return;
84                 }
85
86                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
87
88                 if (!$info) {
89                         @unlink($_FILES['avatarfile']['tmp_name']);
90                         $this->show_form(_t('Not an image or corrupt file.'));
91                         return;
92                 }
93
94                 switch ($info[2]) {
95                  case IMAGETYPE_GIF:
96                  case IMAGETYPE_JPEG:
97                  case IMAGETYPE_PNG:
98                         break;
99                  default:
100                         $this->show_form(_t('Unsupported image file format.'));
101                         return;
102                 }
103                 
104                 $user = common_current_user();
105
106                 $filename = common_avatar_filename($user, image_type_to_extension($info[2]));
107                 $filepath = common_avatar_path($filename);
108
109                 if (!move_uploaded_file($_FILES['avatarfile']['tmp_name'], $filepath)) {
110                         @unlink($_FILES['avatarfile']['tmp_name']);
111                         $this->show_form(_t('System error uploading file.'));
112                         return;
113                 }
114
115                 $avatar = DB_DataObject::factory('avatar');
116
117                 $avatar->profile_id = $user->id;
118                 $avatar->width = $info[0];
119                 $avatar->height = $info[1];
120                 $avatar->mediatype = image_type_to_mime_type($info[2]);
121                 $avatar->filename = $filename;
122                 $avatar->original = true;
123                 $avatar->url = common_avatar_url($filename);
124
125                 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
126                         $scaled[] = $this->scale_avatar($user, $avatar, $size);
127                 }
128
129                 # XXX: start a transaction here
130
131                 if (!$this->delete_old_avatars($user)) {
132                         @unlink($filepath);
133                         common_server_error(_t('Error deleting old avatars.'));
134                         return;
135                 }
136
137                 if (!$avatar->insert()) {
138                         @unlink($filepath);
139                         common_server_error(_t('Error inserting avatar.'));
140                         return;
141                 }
142
143                 foreach ($scaled as $s) {
144                         if (!$s->insert()) {
145                                 common_server_error(_t('Error inserting scaled avatar.'));
146                                 return;
147                         }
148                 }
149
150                 # XXX: end transaction here
151
152                 $this->show_form(_t('Avatar updated.'), true);
153         }
154         
155         function scale_avatar($user, $avatar, $size) {
156                 $image_s = imagecreatetruecolor($size, $size);
157                 $image_a = $this->avatar_to_image($avatar);
158                 
159                 $square = min($avatar->width, $avatar->height);
160                 
161                 imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
162                                                    $size, $size, $square, $square);
163
164                 $ext = ($avatar->mediattype == 'image/jpeg') ? ".jpg" : ".png";
165                 
166                 $filename = common_avatar_filename($user, $ext, $size);
167                 
168                 if ($avatar->mediatype == 'image/jpeg') {
169                         imagejpeg($image_s, common_avatar_path($filename));
170                 } else {
171                         imagepng($image_s, common_avatar_path($filename));
172                 }
173                 
174                 $scaled = DB_DataObject::factory('avatar');
175                 $scaled->profile_id = $avatar->profile_id;
176                 $scaled->width = $size;
177                 $scaled->height = $size;
178                 $scaled->original = false;
179                 $scaled->mediatype = ($avatar->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
180                 $scaled->filename = $filename;
181                 $scaled->url = common_avatar_url($filename);
182                 
183                 return $scaled;
184         }
185         
186         function avatar_to_image($avatar) {
187                 $filepath = common_avatar_path($avatar->filename);
188                 if ($avatar->mediatype == 'image/gif') {
189                         return imagecreatefromgif($filepath);
190                 } else if ($avatar->mediatype == 'image/jpeg') {
191                         return imagecreatefromjpeg($filepath);                  
192                 } else if ($avatar->mediatype == 'image/png') {
193                         return imagecreatefrompng($filepath);
194                 } else {
195                         common_server_error(_t('Unsupported image type:') . $avatar->mediatype);
196                         return NULL;
197                 }
198         }
199         
200         function delete_old_avatars($user) {
201                 $avatar = DB_DataObject::factory('avatar');
202                 $avatar->profile_id = $user->id;
203                 $avatar->find();
204                 while ($avatar->fetch()) {
205                         $avatar->delete();
206                 }
207         }
208 }
209