3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class AvatarAction extends SettingsAction {
24 function show_form($msg=NULL, $success=false) {
25 common_show_header(_t('Avatar'));
26 $this->settings_menu();
27 $this->message($msg, $success);
29 $user = common_current_user();
30 $profile = $user->getProfile();
31 $original = $profile->getOriginal();
34 common_element('img', array('src' => $original->url,
35 'class' => 'avatar original',
36 'width' => $original->width,
37 'height' => $original->height));
40 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
43 common_element('img', array('src' => $avatar->url,
44 'class' => 'avatar profile',
45 'width' => AVATAR_PROFILE_SIZE,
46 'height' => AVATAR_PROFILE_SIZE));
49 common_start_element('form', array('enctype' => 'multipart/form-data',
53 common_local_url('avatar')));
54 common_element('input', array('name' => 'MAX_FILE_SIZE',
56 'id' => 'MAX_FILE_SIZE',
57 'value' => MAX_AVATAR_SIZE));
58 common_element('input', array('name' => 'avatarfile',
60 'id' => 'avatarfile'));
61 common_element('input', array('name' => 'submit',
67 function handle_post() {
69 switch ($_FILES['avatarfile']['error']) {
70 case UPLOAD_ERR_OK: # success, jump out
72 case UPLOAD_ERR_INI_SIZE:
73 case UPLOAD_ERR_FORM_SIZE:
74 $this->show_form(_t('That file is too big.'));
76 case UPLOAD_ERR_PARTIAL:
77 @unlink($_FILES['avatarfile']['tmp_name']);
78 $this->show_form(_t('Partial upload.'));
81 $this->show_form(_t('System error uploading file.'));
85 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
88 @unlink($_FILES['avatarfile']['tmp_name']);
89 $this->show_form(_t('Not an image or corrupt file.'));
99 $this->show_form(_t('Unsupported image file format.'));
103 $user = common_current_user();
105 $filename = common_avatar_filename($user, image_type_to_extension($info[2]));
106 $filepath = common_avatar_path($filename);
108 if (!move_uploaded_file($_FILES['avatarfile']['tmp_name'], $filepath)) {
109 @unlink($_FILES['avatarfile']['tmp_name']);
110 $this->show_form(_t('System error uploading file.'));
114 $avatar = DB_DataObject::factory('avatar');
116 $avatar->profile_id = $user->id;
117 $avatar->width = $info[0];
118 $avatar->height = $info[1];
119 $avatar->mediatype = image_type_to_mime_type($info[2]);
120 $avatar->filename = $filename;
121 $avatar->original = true;
122 $avatar->url = common_avatar_url($filename);
124 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
125 $scaled[] = $this->scale_avatar($user, $avatar, $size);
128 # XXX: start a transaction here
130 if (!$this->delete_old_avatars($user)) {
132 common_server_error(_t('Error deleting old avatars.'));
136 if (!$avatar->insert()) {
138 common_server_error(_t('Error inserting avatar.'));
142 foreach ($scaled as $s) {
144 common_server_error(_t('Error inserting scaled avatar.'));
149 # XXX: end transaction here
151 $this->show_form(_t('Avatar updated.'), true);
154 function scale_avatar($user, $avatar, $size) {
155 $image_s = imagecreatetruecolor($size, $size);
156 $image_a = $this->avatar_to_image($avatar);
158 $square = min($avatar->width, $avatar->height);
160 imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
161 $size, $size, $square, $square);
163 $ext = ($avatar->mediattype == 'image/jpeg') ? ".jpg" : ".png";
165 $filename = common_avatar_filename($user, $ext, $size);
167 if ($avatar->mediatype == 'image/jpeg') {
168 imagejpeg($image_s, common_avatar_path($filename));
170 imagepng($image_s, common_avatar_path($filename));
173 $scaled = DB_DataObject::factory('avatar');
174 $scaled->profile_id = $avatar->profile_id;
175 $scaled->width = $size;
176 $scaled->height = $size;
177 $scaled->original = false;
178 $scaled->mediatype = ($avatar->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
179 $scaled->filename = $filename;
180 $scaled->url = common_avatar_url($filename);
185 function avatar_to_image($avatar) {
186 $filepath = common_avatar_path($avatar->filename);
187 if ($avatar->mediatype == 'image/gif') {
188 return imagecreatefromgif($filepath);
189 } else if ($avatar->mediatype == 'image/jpeg') {
190 return imagecreatefromjpeg($filepath);
191 } else if ($avatar->mediatype == 'image/png') {
192 return imagecreatefrompng($filepath);
194 common_server_error(_t('Unsupported image type:') . $avatar->mediatype);
199 function delete_old_avatars($user) {
200 $avatar = DB_DataObject::factory('avatar');
201 $avatar->profile_id = $user->id;
203 while ($avatar->fetch()) {