]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
Merge branch '0.7.x' of git@gitorious.org:laconica/dev into 0.7.x
[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)
25     { return Memcached_DataObject::staticGet('Avatar',$k,$v); }
26
27     /* the code above is auto generated do not remove the tag below */
28     ###END_AUTOCODE
29
30     # We clean up the file, too
31
32     function delete()
33     {
34         $filename = $this->filename;
35         if (parent::delete()) {
36             @unlink(common_avatar_path($filename));
37         }
38     }
39
40     # Create and save scaled version of this avatar
41     # XXX: maybe break into different methods
42
43     function scale($size)
44     {
45
46         $image_s = imagecreatetruecolor($size, $size);
47         $image_a = $this->to_image();
48         $square = min($this->width, $this->height);
49         imagecolortransparent($image_s, imagecolorallocate($image_s, 0, 0, 0));
50         imagealphablending($image_s, false);
51         imagesavealpha($image_s, true);
52         imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
53                            $size, $size, $square, $square);
54
55         $ext = ($this->mediattype == 'image/jpeg') ? ".jpeg" : ".png";
56
57         $filename = common_avatar_filename($this->profile_id, $ext, $size, common_timestamp());
58
59         if ($this->mediatype == 'image/jpeg') {
60             imagejpeg($image_s, common_avatar_path($filename));
61         } else {
62             imagepng($image_s, common_avatar_path($filename));
63         }
64
65         $scaled = DB_DataObject::factory('avatar');
66         $scaled->profile_id = $this->profile_id;
67         $scaled->width = $size;
68         $scaled->height = $size;
69         $scaled->original = false;
70         $scaled->mediatype = ($this->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
71         $scaled->filename = $filename;
72         $scaled->url = common_avatar_url($filename);
73         $scaled->created = DB_DataObject_Cast::dateTime(); # current time
74
75         if ($scaled->insert()) {
76             return $scaled;
77         } else {
78             return null;
79         }
80     }
81
82     function scale_and_crop($size, $x, $y, $w, $h) 
83     {
84
85         $image_s = imagecreatetruecolor($size, $size);
86         $image_a = $this->to_image();
87
88         # Retain alpha channel info if possible for .pngs
89         $background = imagecolorallocate($image_s, 0, 0, 0);
90         ImageColorTransparent($image_s, $background);
91         imagealphablending($image_s, false);
92
93         imagecopyresized($image_s, $image_a, 0, 0, $x, $y, $size, $size, $w, $h);
94
95         $ext = ($this->mediattype == 'image/jpeg') ? ".jpeg" : ".png";
96
97         $filename = common_avatar_filename($this->profile_id, $ext, $size, common_timestamp());
98
99         if ($this->mediatype == 'image/jpeg') {
100             imagejpeg($image_s, common_avatar_path($filename));
101         } else {
102             imagepng($image_s, common_avatar_path($filename));
103         }
104
105         $cropped = DB_DataObject::factory('avatar');
106         $cropped->profile_id = $this->profile_id;
107         $cropped->width = $size;
108         $cropped->height = $size;
109         $cropped->original = false;
110         $cropped->mediatype = ($this->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
111         $cropped->filename = $filename;
112         $cropped->url = common_avatar_url($filename);
113         $cropped->created = DB_DataObject_Cast::dateTime(); # current time
114
115         if ($cropped->insert()) {
116             return $cropped;
117         } else {
118             return NULL;
119         }
120     }
121
122     function to_image() 
123     {
124         $filepath = common_avatar_path($this->filename);
125         if ($this->mediatype == 'image/gif') {
126             return imagecreatefromgif($filepath);
127         } else if ($this->mediatype == 'image/jpeg') {
128             return imagecreatefromjpeg($filepath);
129         } else if ($this->mediatype == 'image/png') {
130             return imagecreatefrompng($filepath);
131         } else {
132             return NULL;
133         }
134     }
135     
136     function &pkeyGet($kv) 
137     {
138         return Memcached_DataObject::pkeyGet('Avatar', $kv);
139     }
140
141 }