ImageFile->resize now totally replaced with resizeTo

This commit is contained in:
Mikael Nordfeldth 2015-03-12 20:47:07 +01:00
parent 5c8e56e8d2
commit 3ee673ac91
6 changed files with 55 additions and 53 deletions

View File

@ -28,13 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
define('MAX_ORIGINAL', 480);
/** /**
* Upload an avatar * 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_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_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; $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(); $user = common_current_user();
$profile = $user->getProfile(); $profile = $user->getProfile();
$imagefile = new ImageFile(null, $filedata['filepath']); $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)) { if ($profile->setOriginal($filename)) {
@unlink($filedata['filepath']); @unlink($filedata['filepath']);

View File

@ -28,13 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
define('MAX_ORIGINAL', 480);
/** /**
* Upload an avatar * 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_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_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']; $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
$size = min($dest_w, $dest_h); $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
$size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size; $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']); $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']); @unlink($filedata['filepath']);
unset($_SESSION['FILEDATA']); unset($_SESSION['FILEDATA']);
$this->mode = 'upload'; $this->mode = 'upload';

View File

@ -241,16 +241,21 @@ class Avatar extends Managed_DataObject
// TRANS: An error message when avatar size is unreasonable // TRANS: An error message when avatar size is unreasonable
throw new Exception(_m('Avatar size too large')); 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); $original = Avatar::getUploaded($target);
$imagefile = new ImageFile(null, Avatar::path($original->filename)); $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 = clone($original);
$scaled->original = false; $scaled->original = false;
$scaled->width = $width; $scaled->width = $width;
$scaled->height = $width; $scaled->height = $height;
$scaled->url = Avatar::url($filename); $scaled->url = Avatar::url($filename);
$scaled->filename = $filename; $scaled->filename = $filename;
$scaled->created = common_sql_now(); $scaled->created = common_sql_now();

View File

@ -312,13 +312,21 @@ class User_group extends Managed_DataObject
function setOriginal($filename) 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)); $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); $orig = clone($this);
$this->original_logo = Avatar::url($filename); $this->original_logo = Avatar::url($filename);
$this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE)); foreach ($sizes as $name=>$size) {
$this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE)); $filename = Avatar::filename($this->profile_id, image_type_to_extension($imagefile->preferredType()),
$this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE)); $size, common_timestamp());
$imagefile->resizeTo(Avatar::path($filename), array('width'=>$size, 'height'=>$size));
$this->$name = Avatar::url($filename);
}
common_debug(common_log_objstring($this)); common_debug(common_log_objstring($this));
return $this->update($orig); return $this->update($orig);
} }

View File

@ -206,31 +206,6 @@ class ImageFile
return new ImageFile(null, $_FILES[$param]['tmp_name']); 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. * Copy the image file to the given destination.
* *

View File

@ -436,8 +436,14 @@ class FacebookfinishloginAction extends Action
} else { } else {
// save it as an avatar // save it as an avatar
$file = new ImageFile(null, Avatar::path($tmpname)); $imagefile = new ImageFile(null, Avatar::path($tmpname));
$filename = $file->resize(180); // size of the biggest img we get from Facebook $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(); $profile = $user->getProfile();
@ -457,7 +463,6 @@ class FacebookfinishloginAction extends Action
); );
// clean up tmp file // clean up tmp file
@unlink(Avatar::path($tmpname));
} }
} }