[Checkins] SVN: z3c.image/trunk/ - keeping aspect ratio if resizing an image after crop

Bernd Roessl bernd.roessl at lovelysystems.com
Tue Jan 15 18:58:06 EST 2008


Log message for revision 82906:
  - keeping aspect ratio if resizing an image after crop
  

Changed:
  U   z3c.image/trunk/CHANGES.txt
  U   z3c.image/trunk/setup.py
  U   z3c.image/trunk/src/z3c/image/proc/BROWSER.txt
  U   z3c.image/trunk/src/z3c/image/proc/browser.py

-=-
Modified: z3c.image/trunk/CHANGES.txt
===================================================================
--- z3c.image/trunk/CHANGES.txt	2008-01-15 18:16:35 UTC (rev 82905)
+++ z3c.image/trunk/CHANGES.txt	2008-01-15 23:58:06 UTC (rev 82906)
@@ -2,6 +2,12 @@
 Changes for z3c.image
 =====================
 
+2008/01/16 0.1.4:
+=================
+
+- keeping aspect ratio if resizing an image after crop
+
+
 2007/11/09 0.1.3:
 =================
 

Modified: z3c.image/trunk/setup.py
===================================================================
--- z3c.image/trunk/setup.py	2008-01-15 18:16:35 UTC (rev 82905)
+++ z3c.image/trunk/setup.py	2008-01-15 23:58:06 UTC (rev 82906)
@@ -2,7 +2,7 @@
 
 setup(
     name = "z3c.image",
-    version = "0.1.3",
+    version = "0.1.4",
     author = "Zope Contributors",
     author_email = "office at mopacreative.com",
     description = "Image utils for zope3",

Modified: z3c.image/trunk/src/z3c/image/proc/BROWSER.txt
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/BROWSER.txt	2008-01-15 18:16:35 UTC (rev 82905)
+++ z3c.image/trunk/src/z3c/image/proc/BROWSER.txt	2008-01-15 23:58:06 UTC (rev 82906)
@@ -91,6 +91,29 @@
    >>> getImageInfo(browser.contents)
    ('image/jpeg', 5, 5)
 
+It is also possible to omit one dimension of afterSize to keep the resulting
+ratio of the image. If the image get cropped the resulting ratio will be
+apointed by the crop.::
+
+   >>> browser.open('http://localhost/flower.jpg/@@processed?' +
+   ...     'local.crop.h=10&' +
+   ...     'local.crop.w=20&' +
+   ...     'local.crop.x=0&' +
+   ...     'local.crop.y=0&' +
+   ...     'after.size.w=40&'
+   ...     )
+   >>> getImageInfo(browser.contents)
+   ('image/jpeg', 40, 20)
+
+If no crop will be done the ratio will be apointed by the original
+dimensions of the image::
+
+   >>> browser.open('http://localhost/flower.jpg/@@processed?' +
+   ...     'after.size.h=50&'
+   ...     )
+   >>> getImageInfo(browser.contents)
+   ('image/jpeg', 44, 50)
+
 We can use 'If-Modified-Since' in the request and get a 304 status.
 
    >>> browser.addHeader('If-Modified-Since',browser.headers.getheader('Last-Modified'))

Modified: z3c.image/trunk/src/z3c/image/proc/browser.py
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/browser.py	2008-01-15 18:16:35 UTC (rev 82905)
+++ z3c.image/trunk/src/z3c/image/proc/browser.py	2008-01-15 23:58:06 UTC (rev 82906)
@@ -6,7 +6,7 @@
 
 import zope.datetime
 
-from zope.security.proxy import isinstance
+from zope.security.proxy import isinstance, removeSecurityProxy
 from zope.publisher.browser import BrowserView
 from zope.dublincore.interfaces import IZopeDublinCore
 
@@ -17,7 +17,7 @@
 
 def getMaxSize(image_size, desired_size):
     """returns the maximum size of image_size to fit into the
-    rectangualar defined by desired_size 
+    rectangualar defined by desired_size
 
     >>> getMaxSize((100,200),(100,100))
     (50, 100)
@@ -58,11 +58,30 @@
         self.cropW = self.request.form.get('local.crop.w',None)
         self.cropY = self.request.form.get('local.crop.y',None)
         self.cropH = self.request.form.get('local.crop.h',None)
-        
+
         self.afterSizeW = int(self.request.form.get('after.size.w',0))
         self.afterSizeH = int(self.request.form.get('after.size.h',0))
 
         self.size = (self.width,self.height)
+        self._calcAfterSize()
+
+    def _resultingRatio(self):
+        if self.cropW is not None and self.cropH is not None and \
+               self.cropW != 0 and self.cropH != 0:
+            ratio = float(self.cropW) / float(self.cropH)
+        else:
+            context = removeSecurityProxy(self.context)
+            ratio = float(context._width) / float(context._height)
+        return ratio
+
+    def _calcAfterSize(self):
+        if (self.afterSizeW == 0 or self.afterSizeH == 0) and \
+           self.afterSizeW != self.afterSizeH:
+            ratio = self._resultingRatio()
+            if self.afterSizeH == 0:
+                self.afterSizeH = int(round(self.afterSizeW / ratio))
+            if self.afterSizeW == 0:
+                self.afterSizeW = int(round(self.afterSizeH * ratio))
         self.afterSize = (self.afterSizeW,self.afterSizeH)
 
     def _process(self):



More information about the Checkins mailing list