From 3ee673ac912965f43e7d05480d8787184899a2fc Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 12 Mar 2015 20:47:07 +0100 Subject: [PATCH] ImageFile->resize now totally replaced with resizeTo --- actions/avatarsettings.php | 26 ++++++++++++------- actions/grouplogo.php | 23 ++++++++-------- classes/Avatar.php | 9 +++++-- classes/User_group.php | 14 +++++++--- lib/imagefile.php | 25 ------------------ .../actions/facebookfinishlogin.php | 11 +++++--- 6 files changed, 55 insertions(+), 53 deletions(-) diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index 2b22966a4f..1f31cbdafe 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -28,13 +28,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} - - - -define('MAX_ORIGINAL', 480); +if (!defined('GNUSOCIAL')) { exit(1); } /** * Upload an avatar @@ -369,13 +363,27 @@ class AvatarsettingsAction extends SettingsAction $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, MAX_ORIGINAL)); + $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 = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h); + $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']); diff --git a/actions/grouplogo.php b/actions/grouplogo.php index 3dc9891e0b..0d9c135785 100644 --- a/actions/grouplogo.php +++ b/actions/grouplogo.php @@ -28,13 +28,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} - - - -define('MAX_ORIGINAL', 480); +if (!defined('GNUSOCIAL')) { exit(1); } /** * Upload an avatar @@ -390,13 +384,20 @@ class GrouplogoAction extends GroupAction $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); - $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size; + $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 = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h); + $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()), + $size, common_timestamp()); - if ($this->group->setOriginal($filename)) { + $imagefile->resizeTo(Avatar::path($filename), $box); + + if ($profile->setOriginal($filename)) { @unlink($filedata['filepath']); unset($_SESSION['FILEDATA']); $this->mode = 'upload'; diff --git a/classes/Avatar.php b/classes/Avatar.php index 3ef5d667c8..1722b85b6b 100644 --- a/classes/Avatar.php +++ b/classes/Avatar.php @@ -241,16 +241,21 @@ class Avatar extends Managed_DataObject // 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 = $imagefile->resize($width); + $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 = $width; + $scaled->height = $height; $scaled->url = Avatar::url($filename); $scaled->filename = $filename; $scaled->created = common_sql_now(); diff --git a/classes/User_group.php b/classes/User_group.php index 009abe6a6d..df54b7987c 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -312,13 +312,21 @@ class User_group extends Managed_DataObject 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); - $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE)); - $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE)); - $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE)); + 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); } diff --git a/lib/imagefile.php b/lib/imagefile.php index 7a545ad093..2d1a3af02e 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -206,31 +206,6 @@ class ImageFile return new ImageFile(null, $_FILES[$param]['tmp_name']); } - /** - * Compat interface for old code generating avatar thumbnails... - * Saves the scaled file directly into the avatar area. - * - * @param int $size target width & height -- must be square - * @param int $x (default 0) upper-left corner to crop from - * @param int $y (default 0) upper-left corner to crop from - * @param int $w (default full) width of image area to crop - * @param int $h (default full) height of image area to crop - * @return string filename - */ - function resize($size, $x = 0, $y = 0, $w = null, $h = null) - { - $targetType = $this->preferredType(); - $outname = Avatar::filename($this->id, - image_type_to_extension($targetType), - $size, - common_timestamp()); - $outpath = Avatar::path($outname); - $this->resizeTo($outpath, array('width'=>$size, 'height'=>$size, - 'x'=>$x, 'y'=>$y, - 'w'=>$w, 'h'=>$h)); - return $outname; - } - /** * Copy the image file to the given destination. * diff --git a/plugins/FacebookBridge/actions/facebookfinishlogin.php b/plugins/FacebookBridge/actions/facebookfinishlogin.php index 03bcf6cfcd..080c59612c 100644 --- a/plugins/FacebookBridge/actions/facebookfinishlogin.php +++ b/plugins/FacebookBridge/actions/facebookfinishlogin.php @@ -436,8 +436,14 @@ class FacebookfinishloginAction extends Action } else { // save it as an avatar - $file = new ImageFile(null, Avatar::path($tmpname)); - $filename = $file->resize(180); // size of the biggest img we get from Facebook + $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(); @@ -457,7 +463,6 @@ class FacebookfinishloginAction extends Action ); // clean up tmp file - @unlink(Avatar::path($tmpname)); } }