]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
ImageFile->resize now totally replaced with resizeTo
authorMikael Nordfeldth <mmn@hethane.se>
Thu, 12 Mar 2015 19:47:07 +0000 (20:47 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Thu, 12 Mar 2015 19:47:07 +0000 (20:47 +0100)
actions/avatarsettings.php
actions/grouplogo.php
classes/Avatar.php
classes/User_group.php
lib/imagefile.php
plugins/FacebookBridge/actions/facebookfinishlogin.php

index 2b22966a4fef231336e6c281c86a11b630fb8187..1f31cbdafedc9975b80f0feb1b537d6206b33b64 100644 (file)
  * @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']);
index 3dc9891e0b93a559f78b7ec36c05fa81b838b29c..0d9c135785ad5264cccc398ae574fd0233746404 100644 (file)
  * @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());
+
+        $imagefile->resizeTo(Avatar::path($filename), $box);
 
-        if ($this->group->setOriginal($filename)) {
+        if ($profile->setOriginal($filename)) {
             @unlink($filedata['filepath']);
             unset($_SESSION['FILEDATA']);
             $this->mode = 'upload';
index 3ef5d667c879cdb529c09802514c66e9740d6fce..1722b85b6b926be5a1041b9f2ef981f2ac34e53c 100644 (file)
@@ -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();
index 009abe6a6de2c4e8d2c272ffc280943bd154ef5d..df54b7987c7d7228ca507cd9b842a159b095005a 100644 (file)
@@ -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);
     }
index 7a545ad09302ce6117bef7f73c5ac4063da2da4f..2d1a3af02efba2aa68f50ce796c9fa6d4078ae78 100644 (file)
@@ -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.
      *
index 03bcf6cfcd3002acbfdce31426618dda43e91f8c..080c59612cfaa9d29359661a81a5a91e1b711697 100644 (file)
@@ -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));
                     }
 
                 }