Reduced number of avatar requests / improved retina support
authorMarcel Werk <burntime@woltlab.com>
Sun, 7 Sep 2014 11:58:27 +0000 (13:58 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sun, 7 Sep 2014 11:58:27 +0000 (13:58 +0200)
wcfsetup/install/files/lib/data/user/avatar/DefaultAvatar.class.php
wcfsetup/install/files/lib/data/user/avatar/Gravatar.class.php
wcfsetup/install/files/lib/data/user/avatar/UserAvatar.class.php

index 0413ec696fff6b8e8e660b34c275a0cabc7d57d9..4b4a62ccbca64d1a83a34cc0d953aa0ebb7b9037 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\user\avatar;
 use wcf\system\WCF;
+use wcf\util\StringUtil;
 
 /**
  * Represents a default avatar.
@@ -34,7 +35,7 @@ class DefaultAvatar implements IUserAvatar {
        public function getImageTag($size = null) {
                if ($size === null) $size = $this->size;
                
-               return '<img src="'.$this->getURL($size).'" style="width: '.$size.'px; height: '.$size.'px" alt="'.WCF::getLanguage()->get('wcf.user.avatar.alt').'" class="userAvatarImage" />';
+               return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" style="width: '.$size.'px; height: '.$size.'px" alt="'.WCF::getLanguage()->get('wcf.user.avatar.alt').'" class="userAvatarImage" />';
        }
        
        /**
index 5c4d5badafa3555ce9f49711da9c568583cc47da..a3a5ac4458cc12f5cee9df274fc27199c5d50e22 100644 (file)
@@ -4,6 +4,7 @@ use wcf\system\exception\SystemException;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
+use wcf\util\StringUtil;
 
 /**
  * Represents a gravatar.
@@ -69,6 +70,17 @@ class Gravatar extends DefaultAvatar {
         */
        public function getURL($size = null) {
                if ($size === null) $size = $this->size;
+               else {
+                       switch ($size) {
+                               case 16:
+                               case 24:
+                                       $size = 32;
+                                       break;
+                               case 48:
+                                       $size = 96;
+                                       break;
+                       }
+               }
                
                if (!isset($this->url[$size])) {
                        // try to use cached gravatar
@@ -104,6 +116,30 @@ class Gravatar extends DefaultAvatar {
                }
        }
        
+       /**
+        * @see \wcf\data\user\avatar\IUserAvatar::getImageTag()
+        */
+       public function getImageTag($size = null) {
+               if ($size === null) $size = $this->size;
+       
+               $retinaSize = null;
+               switch ($size) {
+                       case 16:
+                               $retinaSize = 32;
+                               break;
+                       case 24:
+                       case 32:
+                       case 48:
+                               $retinaSize = 96;
+                               break;
+                       case 96:
+                               $retinaSize = 128;
+                               break;
+               }
+               
+               return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" '.($retinaSize !== null ? ('srcset="'.StringUtil::encodeHTML($this->getURL($retinaSize)).' 2x" ') : '').'style="width: '.$size.'px; height: '.$size.'px" alt="'.WCF::getLanguage()->get('wcf.user.avatar.alt').'" class="userAvatarImage" />';
+       }
+       
        /**
         * @see \wcf\data\user\avatar\IUserAvatar::canCrop()
         */
index c8f06c2e144c635ed0bd36b56b03512dc413a717..06fefa0bb3ef96a830fa3760c2237cddf73ab6dd 100644 (file)
@@ -19,7 +19,7 @@ class UserAvatar extends DatabaseObject implements IUserAvatar {
         * needed avatar thumbnail sizes
         * @var array<integer>
         */
-       public static $avatarThumbnailSizes = array(16, 24, 32, 48, 96, 128);
+       public static $avatarThumbnailSizes = array(32, 96, 128);
        
        /**
         * @see \wcf\data\DatabaseObject::$databaseTableName
@@ -54,6 +54,16 @@ class UserAvatar extends DatabaseObject implements IUserAvatar {
         * @return      string
         */
        public function getFilename($size = null) {
+               switch ($size) {
+                       case 16:
+                       case 24:
+                               $size = 32;
+                               break;
+                       case 48:
+                               $size = 96;
+                               break;
+               }
+               
                return substr($this->fileHash, 0, 2) . '/' . ($this->avatarID) . '-' . $this->fileHash . ($size !== null ? ('-' . $size) : '') . '.' . $this->avatarExtension;
        }
        
@@ -93,7 +103,22 @@ class UserAvatar extends DatabaseObject implements IUserAvatar {
                        }
                }
                
-               return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" style="width: '.$width.'px; height: '.$height.'px" alt="'.WCF::getLanguage()->get('wcf.user.avatar.alt').'" class="userAvatarImage" />';
+               $retinaSize = null;
+               switch ($size) {
+                       case 16:
+                               $retinaSize = 32;
+                               break;
+                       case 24:
+                       case 32:
+                       case 48:
+                               $retinaSize = 96;
+                               break;
+                       case 96:
+                               $retinaSize = 128;
+                               break;
+               }
+               
+               return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" '.($retinaSize !== null ? ('srcset="'.StringUtil::encodeHTML($this->getURL($retinaSize)).' 2x" ') : '').'style="width: '.$width.'px; height: '.$height.'px" alt="'.WCF::getLanguage()->get('wcf.user.avatar.alt').'" class="userAvatarImage" />';
        }
        
        /**