/** * 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('bg'); $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' => 1280, 'height' => 1280, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $result['url'] = File::url($mediafile->filename); Profile_prefs::setData($profile, 'qvitter', 'background_image', $result['url']); $this->initDocument('json'); $this->showJsonObjects($result); $this->endDocument('json'); }
/** * 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 } }
/** * Generate and store a thumbnail image for the uploaded file, if applicable. * * @return File_thumbnail or null */ function storeThumbnail() { if (substr($this->mimetype, 0, strlen('image/')) != 'image/') { // @fixme video thumbs would be nice! return null; } try { $image = new ImageFile($this->fileRecord->id, File::path($this->filename)); } catch (Exception $e) { // Unsupported image type. return null; } $outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype); $outpath = File::path($outname); $maxWidth = common_config('attachments', 'thumb_width'); $maxHeight = common_config('attachments', 'thumb_height'); list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight); $image->resizeTo($outpath, $width, $height); File_thumbnail::saveThumbnail($this->fileRecord->id, File::url($outname), $width, $height); }
/** * 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; }
/** * Handle the request * * @return void */ protected function handle() { parent::handle(); $profile = $this->user->getProfile(); // see if we have regular uploaded image data try { $mediafile = MediaFile::fromUpload('banner', $profile); } catch (NoUploadedMediaException $e) { // if not we may have base64 data $img = $this->img; if (stristr($img, 'image/jpeg')) { $img_mime = 'image/jpeg'; } elseif (stristr($img, 'image/png')) { // should convert to jpg here!! $img_mime = 'image/png'; } // i don't remember why we had to do this $img = str_replace('data:image/jpeg;base64,', '', $img); $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $img = base64_decode($img, true); try { $img_filename = File::filename($profile, 'cover', $img_mime); $img_path = File::path($img_filename); $img_success = file_put_contents($img_path, $img); $img_mimetype = MediaFile::getUploadedMimeType($img_path, $img_filename); $mediafile = new MediaFile($profile, $img_filename, $img_mimetype); } catch (Exception $e) { $this->clientError($e, 400); } } if (!$mediafile instanceof MediaFile) { $this->clientError(_('Could not process image data.'), 400); } // maybe resize $width = $this->cropW; $height = $this->cropH; $scale = 1; if ($width > 1200) { $scale = 1200 / $width; } elseif ($height > 600) { $scale = 600 / $height; } $width = round($width * $scale); $height = round($height * $scale); // crop try { $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $width, 'height' => $height, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $result['url'] = File::url($mediafile->filename); } catch (Exception $e) { $this->clientError(_('The image could not be resized and cropped. ' . $e), 422); } // save in profile_prefs try { Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']); } catch (ServerException $e) { $this->clientError(_('The image could not be resized and cropped. ' . $e), 422); } // return json $this->initDocument('json'); $this->showJsonObjects($result); $this->endDocument('json'); }
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); }