Could not update avatar due to Bad Thumbnail parameters

This commit is contained in:
Mikael Nordfeldth 2014-06-17 11:50:42 +02:00
parent 5a3d74d9a8
commit 79824a3625
3 changed files with 49 additions and 30 deletions

View File

@ -329,7 +329,7 @@ class AvatarsettingsAction extends SettingsAction
'tmp'.common_timestamp()); 'tmp'.common_timestamp());
$filepath = Avatar::path($filename); $filepath = Avatar::path($filename);
$imagefile->copyTo($filepath); $imagefile = $imagefile->copyTo($filepath);
$filedata = array('filename' => $filename, $filedata = array('filename' => $filename,
'filepath' => $filepath, 'filepath' => $filepath,

View File

@ -415,7 +415,7 @@ class File extends Managed_DataObject
// We have to do it through an ImageFile object because of orientation etc. // We have to do it through an ImageFile object because of orientation etc.
// Only other solution would've been to rotate + rewrite uploaded files. // Only other solution would've been to rotate + rewrite uploaded files.
$image = ImageFile::fromFileObject($this); $image = ImageFile::fromFileObject($this);
list($width, $height, $x, $y, $w2, $h2) = list($width, $height, $x, $y, $w, $h) =
$image->scaleToFit($width, $height, $crop); $image->scaleToFit($width, $height, $crop);
$params = array('file_id'=> $this->id, $params = array('file_id'=> $this->id,
@ -430,9 +430,23 @@ class File extends Managed_DataObject
$outname = "thumb-{$width}x{$height}-" . $this->filename; $outname = "thumb-{$width}x{$height}-" . $this->filename;
$outpath = self::path($outname); $outpath = self::path($outname);
$image->resizeTo($outpath, array('width'=>$width, 'height'=>$height, // The boundary box for our resizing
$box = array('width'=>$width, 'height'=>$height,
'x'=>$x, 'y'=>$y, 'x'=>$x, 'y'=>$y,
'w'=>$w2, 'h'=>$h2)); 'w'=>$w, 'h'=>$h);
// Doublecheck that parameters are sane and integers.
if ($box['width'] < 1 || $box['width'] > common_config('thumbnail', 'maxsize')
|| $box['height'] < 1 || $box['height'] > common_config('thumbnail', 'maxsize')
|| $box['w'] < 1 || $box['x'] >= $this->width
|| $box['h'] < 1 || $box['y'] >= $this->height) {
// Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit
common_debug("Boundary box parameters for resize of {$this->filepath} : ".var_export($box,true));
throw new ServerException('Bad thumbnail size parameters.');
}
// Perform resize and store into file
$image->resizeTo($outpath, $box);
// Avoid deleting the original // Avoid deleting the original
if ($image->getPath() != $this->getPath()) { if ($image->getPath() != $this->getPath()) {

View File

@ -204,16 +204,16 @@ class ImageFile
/** /**
* Copy the image file to the given destination. * Copy the image file to the given destination.
* For obscure formats, this will automatically convert to PNG; *
* otherwise the original file will be copied as-is. * This function may modify the resulting file. Please use the
* returned ImageFile object to read metadata (width, height etc.)
* *
* @param string $outpath * @param string $outpath
* @return string filename * @return ImageFile the image stored at target path
*/ */
function copyTo($outpath) function copyTo($outpath)
{ {
return $this->resizeTo($outpath, array('width' =>$this->width, return new ImageFile(null, $this->resizeTo($outpath));
'height'=>$this->height));
} }
/** /**
@ -232,22 +232,12 @@ class ImageFile
$box['w'] = isset($box['w']) ? intval($box['w']) : $this->width; $box['w'] = isset($box['w']) ? intval($box['w']) : $this->width;
$box['h'] = isset($box['h']) ? intval($box['h']) : $this->height; $box['h'] = isset($box['h']) ? intval($box['h']) : $this->height;
// Doublecheck that parameters are sane and integers.
if ($box['width'] < 1 || $box['width'] > common_config('thumbnail', 'maxsize')
|| $box['height'] < 1 || $box['height'] > common_config('thumbnail', 'maxsize')
|| $box['w'] < 1 || $box['x'] >= $this->width
|| $box['h'] < 1 || $box['y'] >= $this->height) {
// Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit
common_debug("Boundary box parameters for resize of {$this->filepath} : ".var_export($box,true));
throw new ServerException('Bad thumbnail size parameters.');
}
if (!file_exists($this->filepath)) { if (!file_exists($this->filepath)) {
// TRANS: Exception thrown during resize when image has been registered as present, but is no longer there. // TRANS: Exception thrown during resize when image has been registered as present, but is no longer there.
throw new Exception(_('Lost our file.')); throw new Exception(_('Lost our file.'));
} }
// Don't crop/scale if it isn't necessary // Don't rotate/crop/scale if it isn't necessary
if ($box['width'] === $this->width if ($box['width'] === $this->width
&& $box['height'] === $this->height && $box['height'] === $this->height
&& $box['x'] === 0 && $box['x'] === 0
@ -255,10 +245,25 @@ class ImageFile
&& $box['w'] === $this->width && $box['w'] === $this->width
&& $box['h'] === $this->height && $box['h'] === $this->height
&& $this->type == $this->preferredType()) { && $this->type == $this->preferredType()) {
if ($this->rotate == 0) {
// No rotational difference, just copy it as-is
@copy($this->filepath, $outpath); @copy($this->filepath, $outpath);
return $outpath; return $outpath;
} elseif (abs($this->rotate) == 90) {
// Box is rotated 90 degrees in either direction,
// so we have to redefine x to y and vice versa.
$tmp = $box['width'];
$box['width'] = $box['height'];
$box['height'] = $tmp;
$tmp = $box['x'];
$box['x'] = $box['y'];
$box['y'] = $tmp;
$tmp = $box['w'];
$box['w'] = $box['h'];
$box['h'] = $tmp;
} }
}
if (Event::handle('StartResizeImageFile', array($this, $outpath, $box))) { if (Event::handle('StartResizeImageFile', array($this, $outpath, $box))) {
$this->resizeToFile($outpath, $box); $this->resizeToFile($outpath, $box);