[Checkins] SVN: z3c.image/trunk/src/z3c/image/proc/ fixed size calculation

Bernd Dorn bernd.dorn at fhv.at
Wed Aug 30 12:28:47 EDT 2006


Log message for revision 69894:
  fixed size calculation

Changed:
  U   z3c.image/trunk/src/z3c/image/proc/browser.py
  U   z3c.image/trunk/src/z3c/image/proc/tests.py

-=-
Modified: z3c.image/trunk/src/z3c/image/proc/browser.py
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/browser.py	2006-08-30 16:27:55 UTC (rev 69893)
+++ z3c.image/trunk/src/z3c/image/proc/browser.py	2006-08-30 16:28:47 UTC (rev 69894)
@@ -10,23 +10,35 @@
 from types import StringType
 from zope.security.proxy import isinstance
 
-def _getNewSize(image_size, desired_size, keep_aspect):
-    """Resizes image_size to desired_size, optionally keeping the
-    aspect ratio.
+def getMaxSize(image_size, desired_size):
+    """returns the maximum size of image_size to fit into the
+    rectangualar defined by desired_size 
+
+    >>> getMaxSize((100,200),(100,100))
+    (50, 100)
+
+    >>> getMaxSize((100,300),(100,100))
+    (33, 100)
+
+    >>> getMaxSize((300,100),(100,100))
+    (100, 33)
+
+    >>> getMaxSize((300,100),(400,200))
+    (400, 133)
+
+
     """
-    if keep_aspect:
-        x_ratio = float(desired_size[0])/image_size[0]
-        y_ratio = float(desired_size[1])/image_size[1]
-        if x_ratio < y_ratio:
-            new_size = (round(x_ratio*image_size[0]),
-                        round(x_ratio*image_size[1]))
-        else:
-            new_size = (round(y_ratio*image_size[0]),
-                        round(y_ratio*image_size[1]))
-        return new_size
+    x_ratio = float(desired_size[0])/image_size[0]
+    y_ratio = float(desired_size[1])/image_size[1]
+    if x_ratio < y_ratio:
+        new_size = (round(x_ratio*image_size[0]),
+                    round(x_ratio*image_size[1]))
     else:
-        return desired_size
+        new_size = (round(y_ratio*image_size[0]),
+                    round(y_ratio*image_size[1]))
+    return tuple(map(int, new_size))
 
+
 class ImageProcessorView(BrowserView):
 
     """image processor"""
@@ -99,8 +111,10 @@
 
     def __init__(self,context,request):
         super(ResizedImageView,self).__init__(context,request)
+        data = context.data
         if not isinstance(context.data, str):
-            data = self.context.data.read(256)
+            data.seek(0)
+            data = data.read(1024*16)
         else:
             data = self.context.data
         t,w,h = getImageInfo(data)
@@ -109,11 +123,8 @@
         self.height = self.request.form.get('h',self.size[1])
 
     def _process(self):
-
+        new_size = getMaxSize(self.size, (self.width, self.height))
         pimg = IProcessableImage(self.context)
-        
-        new_size = _getNewSize(self.size, (self.width, self.height),
-                               keep_aspect=True)
         if new_size != self.size:
             pimg.resize(new_size)
         return pimg.process()

Modified: z3c.image/trunk/src/z3c/image/proc/tests.py
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/tests.py	2006-08-30 16:27:55 UTC (rev 69893)
+++ z3c.image/trunk/src/z3c/image/proc/tests.py	2006-08-30 16:28:47 UTC (rev 69894)
@@ -19,6 +19,9 @@
         DocFileSuite('README.txt',
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      ),
+        DocTestSuite('z3c.image.proc.browser',
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
         ))
 
 



More information about the Checkins mailing list