/** * Handle the results of jcrop. * * @return void */ function cropAvatar() { $filedata = $_SESSION['FILEDATA']; if (!$filedata) { $this->serverError(_('图片文件信息丢失')); return; } $file_d = $filedata['width'] > $filedata['height'] ? $filedata['height'] : $filedata['width']; $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x') : 0; $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y') : 0; $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w') : $file_d; $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $file_d; $size = min($dest_w, $dest_h, MAX_ORIGINAL); $user = common_current_user(); $profile = $user->getProfile(); $imagefile = new ImageFile($user->id, $filedata['filepath']); $filename = $imagefile->resizeAvatar($size, $dest_x, $dest_y, $dest_w, $dest_h); if ($profile->setOriginal($filename)) { @unlink($filedata['filepath']); unset($_SESSION['FILEDATA']); $this->mode = 'upload'; $this->showForm(_('头像已完成修改'), true); common_broadcast_profile($profile); } else { $this->showForm(_('头像修改失败,请重试')); } }
function setOriginal($filename) { $imagefile = new ImageFile($this->id, Avatar::path($filename)); $avatar = new Avatar(); $avatar->profile_id = $this->id; $avatar->width = $imagefile->width; $avatar->height = $imagefile->height; $avatar->mediatype = image_type_to_mime_type($imagefile->type); $avatar->filename = $filename; $avatar->original = true; $avatar->url = Avatar::url($filename); $avatar->created = DB_DataObject_Cast::dateTime(); # current time # XXX: start a transaction here if (!$this->delete_avatars() || !$avatar->insert()) { @unlink(Avatar::path($filename)); return null; } foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) { # We don't do a scaled one if original is our scaled size if (!($avatar->width == $size && $avatar->height == $size)) { $scaled_filename = $imagefile->resizeAvatar($size); //$scaled = DB_DataObject::factory('avatar'); $scaled = new Avatar(); $scaled->profile_id = $this->id; $scaled->width = $size; $scaled->height = $size; $scaled->original = false; $scaled->mediatype = image_type_to_mime_type($imagefile->type); $scaled->filename = $scaled_filename; $scaled->url = Avatar::url($scaled_filename); $scaled->created = DB_DataObject_Cast::dateTime(); # current time if (!$scaled->insert()) { return null; } } } return $avatar; }