AnsweredAssumed Answered

preserve avatar aspect ratio

Question asked by lemonyogster on Jun 6, 2011
Latest reply on Jun 6, 2011 by Wolf

I thought it was funny when the avatars would be stretched or flattened on the contact list window so I made a patch to preserve the aspect ratio.  Any chance this could be put into future builds?

 

 

### Eclipse Workspace Patch 1.0
#P Spark
Index: src/java/org/jivesoftware/spark/util/GraphicUtils.java
===================================================================
--- src/java/org/jivesoftware/spark/util/GraphicUtils.java    (revision 12345)
+++ src/java/org/jivesoftware/spark/util/GraphicUtils.java    (working copy)
@@ -797,9 +797,36 @@
      * @return the icon.
      */
     public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) {
-    Image img = icon.getImage();
-    img = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
-    return new ImageIcon(img);
+        Image img = icon.getImage();
+        int height = icon.getIconHeight();
+        int width = icon.getIconWidth();
+        boolean scaleHeight=height*newWidth>width*newHeight;
+        if(height > newHeight) {
+            //Too tall
+            if(width <= newWidth || scaleHeight) {
+                //Width is okay or height is limiting factor due to aspect ratio
+                height = newHeight;
+                width = -1;
+            } else {
+                //Width is limiting factor due to aspect ratio
+                height = -1;
+                width = newWidth;
+            }
+        } else if(width > newWidth) {
+            //Too wide and height is okay
+            height = -1;
+            width = newWidth;
+        } else if(scaleHeight) {
+            //Height is limiting factor due to aspect ratio
+            height = newHeight;
+            width = -1;
+        } else {
+            //Width is limiting factor due to aspect ratio
+            height = -1;
+            width = newWidth;
+        }
+        img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
+        return new ImageIcon(img);
     }

     /**

Outcomes