3 if (!defined('GNUSOCIAL')) { exit(1); }
6 * Table Definition for avatar
9 class Avatar extends Managed_DataObject
11 public $__table = 'avatar'; // table name
12 public $profile_id; // int(4) primary_key not_null
13 public $original; // tinyint(1)
14 public $width; // int(4) primary_key not_null
15 public $height; // int(4) primary_key not_null
16 public $mediatype; // varchar(32) not_null
17 public $filename; // varchar(191) not 255 because utf8mb4 takes more space
18 public $created; // datetime() not_null
19 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
21 public static function schemaDef()
25 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
26 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
27 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
28 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
29 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
30 'filename' => array('type' => 'varchar', 'length' => 191, 'description' => 'local filename, if local'),
31 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
32 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
34 'primary key' => array('profile_id', 'width', 'height'),
35 'unique keys' => array(
36 // 'avatar_filename_key' => array('filename'),
38 'foreign keys' => array(
39 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
42 'avatar_profile_id_idx' => array('profile_id'),
47 // We clean up the file, too
48 function delete($useWhere=false)
50 $filename = $this->filename;
51 if (file_exists(Avatar::path($filename))) {
52 @unlink(Avatar::path($filename));
55 return parent::delete($useWhere);
59 * Deletes all avatars (but may spare the original) from a profile.
61 * @param Profile $target The profile we're deleting avatars of.
62 * @param boolean $original Whether original should be removed or not.
64 public static function deleteFromProfile(Profile $target, $original=true) {
66 $avatars = self::getProfileAvatars($target);
67 foreach ($avatars as $avatar) {
68 if ($avatar->original && !$original) {
73 } catch (NoAvatarException $e) {
74 // There are no avatars to delete, a sort of success.
80 static protected $_avatars = array();
83 * Get an avatar by profile. Currently can't call newSize with $height
85 public static function byProfile(Profile $target, $width=null, $height=null)
87 $width = intval($width);
88 $height = !is_null($height) ? intval($height) : null;
89 if (is_null($height)) {
93 $size = "{$width}x{$height}";
94 if (!isset(self::$_avatars[$target->id])) {
95 self::$_avatars[$target->id] = array();
96 } elseif (isset(self::$_avatars[$target->id][$size])){
97 return self::$_avatars[$target->id][$size];
101 if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
102 $avatar = self::pkeyGet(
104 'profile_id' => $target->id,
109 Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
112 if (is_null($avatar)) {
113 // Obviously we can't find an avatar, so let's resize the original!
114 $avatar = Avatar::newSize($target, $width);
115 } elseif (!($avatar instanceof Avatar)) {
116 throw new NoAvatarException($target, $avatar);
119 self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
123 public static function getUploaded(Profile $target)
125 $avatar = new Avatar();
126 $avatar->profile_id = $target->id;
127 $avatar->original = true;
128 if (!$avatar->find(true)) {
129 throw new NoAvatarException($target, $avatar);
131 if (!file_exists(Avatar::path($avatar->filename))) {
132 // The delete call may be odd for, say, unmounted filesystems
133 // that cause a file to currently not exist, but actually it does...
135 throw new NoAvatarException($target, $avatar);
140 public static function getProfileAvatars(Profile $target) {
141 $avatar = new Avatar();
142 $avatar->profile_id = $target->id;
143 if (!$avatar->find()) {
144 throw new NoAvatarException($target, $avatar);
146 return $avatar->fetchAll();
150 * Where should the avatar go for this user?
152 static function filename($id, $extension, $size=null, $extra=null)
155 return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
157 return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
161 static function path($filename)
163 $dir = common_config('avatar', 'dir');
165 if ($dir[strlen($dir)-1] != '/') {
169 return $dir . $filename;
172 static function url($filename)
174 $path = common_config('avatar', 'path');
176 if ($path[strlen($path)-1] != '/') {
180 if ($path[0] != '/') {
184 $server = common_config('avatar', 'server');
186 if (empty($server)) {
187 $server = common_config('site', 'server');
190 $ssl = (common_config('avatar', 'ssl') || GNUsocial::useHTTPS());
192 $protocol = ($ssl) ? 'https' : 'http';
194 return $protocol.'://'.$server.$path.$filename;
197 function displayUrl()
199 return Avatar::url($this->filename);
202 static function urlByProfile(Profile $target, $width=null, $height=null) {
204 return self::byProfile($target, $width, $height)->displayUrl();
205 } catch (Exception $e) {
206 return self::defaultImage($width);
210 static function defaultImage($size=null)
212 if (is_null($size)) {
213 $size = AVATAR_PROFILE_SIZE;
215 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
216 AVATAR_STREAM_SIZE => 'stream',
217 AVATAR_MINI_SIZE => 'mini');
218 return Theme::path('default-avatar-'.$sizenames[$size].'.png');
221 static function newSize(Profile $target, $width) {
222 $width = intval($width);
223 if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
224 // TRANS: An error message when avatar size is unreasonable
225 throw new Exception(_m('Avatar size too large'));
227 // So far we only have square avatars and I don't have time to
228 // rewrite support for non-square ones right now ;)
231 $original = Avatar::getUploaded($target);
233 $imagefile = new ImageFile(null, Avatar::path($original->filename));
234 $filename = Avatar::filename($target->getID(), image_type_to_extension($imagefile->preferredType()),
235 $width, common_timestamp());
236 $imagefile->resizeTo(Avatar::path($filename), array('width'=>$width, 'height'=>$height));
238 $scaled = clone($original);
239 $scaled->original = false;
240 $scaled->width = $width;
241 $scaled->height = $height;
242 $scaled->filename = $filename;
243 $scaled->created = common_sql_now();
245 if (!$scaled->insert()) {
246 // TRANS: An error message when unable to insert avatar data into the db
247 throw new Exception(_m('Could not insert new avatar data to database'));
250 // Return the new avatar object