]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
Avatar resizing improvements and better code reuse
[quix0rs-gnu-social.git] / classes / Avatar.php
1 <?php
2 /**
3  * Table Definition for avatar
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Avatar extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'avatar';                          // table name
13     public $profile_id;                      // int(4)  primary_key not_null
14     public $original;                        // tinyint(1)
15     public $width;                           // int(4)  primary_key not_null
16     public $height;                          // int(4)  primary_key not_null
17     public $mediatype;                       // varchar(32)   not_null
18     public $filename;                        // varchar(255)
19     public $url;                             // varchar(255)  unique_key
20     public $created;                         // datetime()   not_null
21     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
22
23     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25         
26     public static function schemaDef()
27     {
28         return array(
29             'fields' => array(
30                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31                 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
32                 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
33                 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
34                 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
35                 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
36                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
37                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
38                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
39             ),
40             'primary key' => array('profile_id', 'width', 'height'),
41             'unique keys' => array(
42                 'avatar_url_key' => array('url'),
43             ),
44             'foreign keys' => array(
45                 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
46             ),
47             'indexes' => array(
48                 'avatar_profile_id_idx' => array('profile_id'),
49             ),
50         );
51     }
52     // We clean up the file, too
53
54     function delete()
55     {
56         $filename = $this->filename;
57         if (parent::delete()) {
58             @unlink(Avatar::path($filename));
59         }
60     }
61
62     public static function deleteFromProfile(Profile $target) {
63         $avatars = Avatar::getProfileAvatars($target->id);
64         foreach ($avatars as $avatar) {
65             $avatar->delete();
66         }
67     }
68
69     public static function getOriginal(Profile $target)
70     {
71         $avatar = new Avatar();
72         $avatar->profile_id = $target->id;
73         $avatar->original = true;
74         if (!$avatar->find(true)) {
75             throw new NoResultException($avatar);
76         }
77         return $avatar;
78     }
79
80     public static function getProfileAvatars(Profile $target) {
81         $avatar = new Avatar();
82         $avatar->profile_id = $target->id;
83         return $avatar->fetchAll();
84     }
85
86     /**
87      * Where should the avatar go for this user?
88      */
89     static function filename($id, $extension, $size=null, $extra=null)
90     {
91         if ($size) {
92             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
93         } else {
94             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
95         }
96     }
97
98     static function path($filename)
99     {
100         $dir = common_config('avatar', 'dir');
101
102         if ($dir[strlen($dir)-1] != '/') {
103             $dir .= '/';
104         }
105
106         return $dir . $filename;
107     }
108
109     static function url($filename)
110     {
111         $path = common_config('avatar', 'path');
112
113         if ($path[strlen($path)-1] != '/') {
114             $path .= '/';
115         }
116
117         if ($path[0] != '/') {
118             $path = '/'.$path;
119         }
120
121         $server = common_config('avatar', 'server');
122
123         if (empty($server)) {
124             $server = common_config('site', 'server');
125         }
126
127         $ssl = common_config('avatar', 'ssl');
128
129         if (is_null($ssl)) { // null -> guess
130             if (common_config('site', 'ssl') == 'always' &&
131                 !common_config('avatar', 'server')) {
132                 $ssl = true;
133             } else {
134                 $ssl = false;
135             }
136         }
137
138         $protocol = ($ssl) ? 'https' : 'http';
139
140         return $protocol.'://'.$server.$path.$filename;
141     }
142
143     function displayUrl()
144     {
145         $server = common_config('avatar', 'server');
146         if ($server && !empty($this->filename)) {
147             return Avatar::url($this->filename);
148         } else {
149             return $this->url;
150         }
151     }
152
153     static function defaultImage($size)
154     {
155         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
156                                   AVATAR_STREAM_SIZE => 'stream',
157                                   AVATAR_MINI_SIZE => 'mini');
158         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
159     }
160
161     static function newSize(Profile $target, $size) {
162         $size = floor($size);
163         if ($size <1 || $size > 999) {
164             // TRANS: An error message when avatar size is unreasonable
165             throw new Exception(_m('Unreasonable avatar size'));
166         }
167
168         $original = Avatar::getOriginal($target);
169
170         $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
171         $filename = $imagefile->resize($size);
172
173         $scaled = clone($original);
174         $scaled->original = false;
175         $scaled->width = $size;
176         $scaled->height = $size;
177         $scaled->url = Avatar::url($filename);
178         $scaled->created = DB_DataObject_Cast::dateTime();
179
180         if (!$scaled->insert()) {
181             // TRANS: An error message when unable to insert avatar data into the db
182             throw new Exception(_m('Could not insert new avatar data to database'));
183         }
184
185         // Return the new avatar object
186         return $scaled;
187     }
188 }