3 * Table Definition for avatar
5 require_once 'DB/DataObject.php';
7 class Avatar extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
24 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Avatar',$k,$v); }
26 /* the code above is auto generated do not remove the tag below */
29 # We clean up the file, too
32 $filename = $this->filename;
33 if (parent::delete()) {
34 @unlink(common_avatar_path($filename));
38 # Create and save scaled version of this avatar
39 # XXX: maybe break into different methods
41 function scale($size) {
43 $image_s = imagecreatetruecolor($size, $size);
44 $image_a = $this->to_image();
46 $square = min($this->width, $this->height);
48 imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
49 $size, $size, $square, $square);
51 $ext = ($this->mediattype == 'image/jpeg') ? ".jpeg" : ".png";
53 $filename = common_avatar_filename($this->profile_id, $ext, $size, common_timestamp());
55 if ($this->mediatype == 'image/jpeg') {
56 imagejpeg($image_s, common_avatar_path($filename));
58 imagepng($image_s, common_avatar_path($filename));
61 $scaled = DB_DataObject::factory('avatar');
62 $scaled->profile_id = $this->profile_id;
63 $scaled->width = $size;
64 $scaled->height = $size;
65 $scaled->original = false;
66 $scaled->mediatype = ($this->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
67 $scaled->filename = $filename;
68 $scaled->url = common_avatar_url($filename);
69 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
71 if ($scaled->insert()) {
79 $filepath = common_avatar_path($this->filename);
80 if ($this->mediatype == 'image/gif') {
81 return imagecreatefromgif($filepath);
82 } else if ($this->mediatype == 'image/jpeg') {
83 return imagecreatefromjpeg($filepath);
84 } else if ($this->mediatype == 'image/png') {
85 return imagecreatefrompng($filepath);