]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Could not update avatar due to Bad Thumbnail parameters
authorMikael Nordfeldth <mmn@hethane.se>
Tue, 17 Jun 2014 09:50:42 +0000 (11:50 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Tue, 17 Jun 2014 10:48:10 +0000 (12:48 +0200)
actions/avatarsettings.php
classes/File.php
lib/imagefile.php

index 0a5f79e763136a6616eeb97a36b5651bf1af3fe8..7373e770477b4a8def01b0ea7ac9db5cdeca3ea6 100644 (file)
@@ -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,
index b5bcec404168b8bbf9fbabfc3d58b69d6257ef93..acc90d495f7920cdec132ad17ec8c792df4bb5ce 100644 (file)
@@ -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()) {
index c051fc3ac9bce5118c75242d9f4b1b228833487d..8a0f65232793a962716b49f74dc1fe4f27b3fa94 100644 (file)
@@ -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);
         }