/** * Handle the request * * @return void */ protected function handle() { parent::handle(); $profile = $this->user->getProfile(); $base64img = $this->img; if (stristr($base64img, 'image/jpeg')) { $base64img_mime = 'image/jpeg'; } elseif (stristr($base64img, 'image/png')) { // should convert to jpg here!! $base64img_mime = 'image/png'; } $base64img = str_replace('data:image/jpeg;base64,', '', $base64img); $base64img = str_replace('data:image/png;base64,', '', $base64img); $base64img = str_replace(' ', '+', $base64img); $base64img_hash = md5($base64img); $base64img = base64_decode($base64img); $base64img_basename = basename('avatar'); $base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime); $base64img_path = File::path($base64img_filename); $base64img_success = file_put_contents($base64img_path, $base64img); $base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename); $mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype); $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $this->cropW, 'height' => $this->cropH, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $type = $imagefile->preferredType(); $filename = Avatar::filename($profile->id, image_type_to_extension($type), null, common_timestamp()); $filepath = Avatar::path($filename); $imagefile->copyTo($filepath); $profile = $this->user->getProfile(); $profile->setOriginal($filename); $mediafile->delete(); $twitter_user = $this->twitterUserArray($profile, true); $this->initDocument('json'); $this->showJsonObjects($twitter_user); $this->endDocument('json'); }
function setAvatar($user) { try { $picUrl = sprintf('http://graph.facebook.com/%d/picture?type=large', $this->fbuser->id); // fetch the picture from Facebook $client = new HTTPClient(); // fetch the actual picture $response = $client->get($picUrl); if ($response->isOk()) { // seems to always be jpeg, but not sure $tmpname = "facebook-avatar-tmp-" . common_random_hexstr(4); $ok = file_put_contents(Avatar::path($tmpname), $response->getBody()); if (!$ok) { common_log(LOG_WARNING, 'Couldn\'t save tmp Facebook avatar: ' . $tmpname, __FILE__); } else { // save it as an avatar $imagefile = new ImageFile(null, Avatar::path($tmpname)); $filename = Avatar::filename($user->id, image_type_to_extension($imagefile->preferredType()), 180, common_timestamp()); // Previous docs said 180 is the "biggest img we get from Facebook" $imagefile->resizeTo(Avatar::path($filename, array('width' => 180, 'height' => 180))); // No need to keep the temporary file around... @unlink(Avatar::path($tmpname)); $profile = $user->getProfile(); if ($profile->setOriginal($filename)) { common_log(LOG_INFO, sprintf('Saved avatar for %s (%d) from Facebook picture for ' . '%s (fbuid %d), filename = %s', $user->nickname, $user->id, $this->fbuser->name, $this->fbuid, $filename), __FILE__); // clean up tmp file } } } } catch (Exception $e) { common_log(LOG_WARNING, 'Couldn\'t save Facebook avatar: ' . $e->getMessage(), __FILE__); // error isn't fatal, continue } }
/** * Handle the results of jcrop. * * @return void */ function cropLogo() { $filedata = $_SESSION['FILEDATA']; if (!$filedata) { // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present. $this->serverError(_('Lost our file data.')); } // If image is not being cropped assume pos & dimentions of original $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') : $filedata['width']; $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $filedata['height']; $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize')); $box = array('width' => $size, 'height' => $size, 'x' => $dest_x, 'y' => $dest_y, 'w' => $dest_w, 'h' => $dest_h); $profile = $this->group->getProfile(); $imagefile = new ImageFile(null, $filedata['filepath']); $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()), $size, common_timestamp()); $imagefile->resizeTo(Avatar::path($filename), $box); if ($profile->setOriginal($filename)) { @unlink($filedata['filepath']); unset($_SESSION['FILEDATA']); $this->mode = 'upload'; // TRANS: Form success message after updating a group logo. $this->showForm(_('Logo updated.'), true); } else { // TRANS: Form failure message after failing to update a group logo. $this->showForm(_('Failed updating logo.')); } }
/** * Handle the results of jcrop. * * @return void */ public function cropAvatar() { $filedata = $_SESSION['FILEDATA']; if (!$filedata) { // TRANS: Server error displayed if an avatar upload went wrong somehow server side. $this->serverError(_('Lost our file data.')); } $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 = intval(min($dest_w, $dest_h, common_config('avatar', 'maxsize'))); $box = array('width' => $size, 'height' => $size, 'x' => $dest_x, 'y' => $dest_y, 'w' => $dest_w, 'h' => $dest_h); $user = common_current_user(); $profile = $user->getProfile(); $imagefile = new ImageFile(null, $filedata['filepath']); $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()), $size, common_timestamp()); try { $imagefile->resizeTo(Avatar::path($filename), $box); } catch (UseFileAsThumbnailException $e) { common_debug('Using uploaded avatar directly without resizing, copying it to: ' . $filename); if (!copy($filedata['filepath'], Avatar::path($filename))) { common_debug('Tried to copy image file ' . $filedata['filepath'] . ' to destination ' . Avatar::path($filename)); throw new ServerException('Could not copy file to destination.'); } } if ($profile->setOriginal($filename)) { @unlink($filedata['filepath']); unset($_SESSION['FILEDATA']); $this->mode = 'upload'; // TRANS: Success message for having updated a user avatar. $this->showForm(_('Avatar updated.'), true); } else { // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason. $this->showForm(_('Failed updating avatar.')); } }
static function newSize(Profile $target, $width) { $width = intval($width); if ($width < 1 || $width > common_config('avatar', 'maxsize')) { // TRANS: An error message when avatar size is unreasonable throw new Exception(_m('Avatar size too large')); } // So far we only have square avatars and I don't have time to // rewrite support for non-square ones right now ;) $height = $width; $original = Avatar::getUploaded($target); $imagefile = new ImageFile(null, Avatar::path($original->filename)); $filename = Avatar::filename($target->getID(), image_type_to_extension($imagefile->preferredType()), $width, common_timestamp()); $imagefile->resizeTo(Avatar::path($filename), array('width' => $width, 'height' => $height)); $scaled = clone $original; $scaled->original = false; $scaled->width = $width; $scaled->height = $height; $scaled->filename = $filename; $scaled->created = common_sql_now(); if (!$scaled->insert()) { // TRANS: An error message when unable to insert avatar data into the db throw new Exception(_m('Could not insert new avatar data to database')); } // Return the new avatar object return $scaled; }
function setOriginal($filename) { // This should be handled by the Profile->setOriginal function so user and group avatars are handled the same $imagefile = new ImageFile(null, Avatar::path($filename)); $sizes = array('homepage_logo' => AVATAR_PROFILE_SIZE, 'stream_logo' => AVATAR_STREAM_SIZE, 'mini_logo' => AVATAR_MINI_SIZE); $orig = clone $this; $this->original_logo = Avatar::url($filename); foreach ($sizes as $name => $size) { $filename = Avatar::filename($this->profile_id, image_type_to_extension($imagefile->preferredType()), $size, common_timestamp()); $imagefile->resizeTo(Avatar::path($filename), array('width' => $size, 'height' => $size)); $this->{$name} = Avatar::url($filename); } common_debug(common_log_objstring($this)); return $this->update($orig); }