]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
901c47c51e8e4c039f15daaeaefe1dcad7d27397
[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 Memcached_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     /* Static get */
24     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Avatar',$k,$v); }
25
26     /* the code above is auto generated do not remove the tag below */
27     ###END_AUTOCODE
28
29         # We clean up the file, too
30
31         function delete() {
32                 $filename = $this->filename;
33                 if (parent::delete()) {
34                         @unlink(common_avatar_path($filename));
35                 }
36         }
37
38         # Create and save scaled version of this avatar
39         # XXX: maybe break into different methods
40
41         function scale($size) {
42
43                 $image_s = imagecreatetruecolor($size, $size);
44                 $image_a = $this->to_image();
45                 $square = min($this->width, $this->height);
46         imagecolortransparent($image_s, imagecolorallocate($image_s, 0, 0, 0));
47         imagealphablending($image_s, false);
48         imagesavealpha($image_s, true);
49                 imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
50                                                    $size, $size, $square, $square);
51
52                 $ext = ($this->mediattype == 'image/jpeg') ? ".jpeg" : ".png";
53
54                 $filename = common_avatar_filename($this->profile_id, $ext, $size, common_timestamp());
55
56                 if ($this->mediatype == 'image/jpeg') {
57                         imagejpeg($image_s, common_avatar_path($filename));
58                 } else {
59                         imagepng($image_s, common_avatar_path($filename));
60                 }
61
62                 $scaled = DB_DataObject::factory('avatar');
63                 $scaled->profile_id = $this->profile_id;
64                 $scaled->width = $size;
65                 $scaled->height = $size;
66                 $scaled->original = false;
67                 $scaled->mediatype = ($this->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
68                 $scaled->filename = $filename;
69                 $scaled->url = common_avatar_url($filename);
70                 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
71
72                 if ($scaled->insert()) {
73                         return $scaled;
74                 } else {
75                         return NULL;
76                 }
77         }
78
79         function to_image() {
80                 $filepath = common_avatar_path($this->filename);
81                 if ($this->mediatype == 'image/gif') {
82                         return imagecreatefromgif($filepath);
83                 } else if ($this->mediatype == 'image/jpeg') {
84                         return imagecreatefromjpeg($filepath);
85                 } else if ($this->mediatype == 'image/png') {
86                         return imagecreatefrompng($filepath);
87                 } else {
88                         return NULL;
89                 }
90         }
91         
92         function &pkeyGet($kv) {
93                 return Memcached_DataObject::pkeyGet('Avatar', $kv);
94         }
95 }