From 79824a362588d480bf668fc957101de9f8fa45e2 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Tue, 17 Jun 2014 11:50:42 +0200 Subject: [PATCH] Could not update avatar due to Bad Thumbnail parameters --- actions/avatarsettings.php | 2 +- classes/File.php | 22 ++++++++++++--- lib/imagefile.php | 55 +++++++++++++++++++++----------------- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index 0a5f79e763..7373e77047 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -329,7 +329,7 @@ class AvatarsettingsAction extends SettingsAction 'tmp'.common_timestamp()); $filepath = Avatar::path($filename); - $imagefile->copyTo($filepath); + $imagefile = $imagefile->copyTo($filepath); $filedata = array('filename' => $filename, 'filepath' => $filepath, diff --git a/classes/File.php b/classes/File.php index b5bcec4041..acc90d495f 100644 --- a/classes/File.php +++ b/classes/File.php @@ -415,7 +415,7 @@ class File extends Managed_DataObject // We have to do it through an ImageFile object because of orientation etc. // Only other solution would've been to rotate + rewrite uploaded files. $image = ImageFile::fromFileObject($this); - list($width, $height, $x, $y, $w2, $h2) = + list($width, $height, $x, $y, $w, $h) = $image->scaleToFit($width, $height, $crop); $params = array('file_id'=> $this->id, @@ -430,9 +430,23 @@ class File extends Managed_DataObject $outname = "thumb-{$width}x{$height}-" . $this->filename; $outpath = self::path($outname); - $image->resizeTo($outpath, array('width'=>$width, 'height'=>$height, - 'x'=>$x, 'y'=>$y, - 'w'=>$w2, 'h'=>$h2)); + // The boundary box for our resizing + $box = array('width'=>$width, 'height'=>$height, + 'x'=>$x, 'y'=>$y, + '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 if ($image->getPath() != $this->getPath()) { diff --git a/lib/imagefile.php b/lib/imagefile.php index c051fc3ac9..8a0f652327 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -204,16 +204,16 @@ class ImageFile /** * 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 - * @return string filename + * @return ImageFile the image stored at target path */ function copyTo($outpath) { - return $this->resizeTo($outpath, array('width' =>$this->width, - 'height'=>$this->height)); + return new ImageFile(null, $this->resizeTo($outpath)); } /** @@ -232,34 +232,39 @@ class ImageFile $box['w'] = isset($box['w']) ? intval($box['w']) : $this->width; $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)) { // TRANS: Exception thrown during resize when image has been registered as present, but is no longer there. 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 - && $box['height'] === $this->height - && $box['x'] === 0 - && $box['y'] === 0 - && $box['w'] === $this->width - && $box['h'] === $this->height - && $this->type == $this->preferredType()) { - - @copy($this->filepath, $outpath); - return $outpath; + && $box['height'] === $this->height + && $box['x'] === 0 + && $box['y'] === 0 + && $box['w'] === $this->width + && $box['h'] === $this->height + && $this->type == $this->preferredType()) { + if ($this->rotate == 0) { + // No rotational difference, just copy it as-is + @copy($this->filepath, $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))) { $this->resizeToFile($outpath, $box); } -- 2.39.2