Commit e9a826a9 authored by Nicolas Delaby's avatar Nicolas Delaby

Refactoring of Image conversion.

  * use DEFAULT_QUALITY constant
  * Postpone the test off relevance conversion parameters in order to check
    if resizing is not needed to fit display ratio.
    If test succeed then call convert, otherwise return raw image (getData).
  * remove _getDisplayPhoto and merge is code inside _makeDisplayPhoto
    because this method is called from only one statement and factorisation
    is null



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35240 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 84f8d28a
...@@ -44,6 +44,7 @@ from Products.ERP5Type import Permissions, PropertySheet ...@@ -44,6 +44,7 @@ from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.Document.File import File from Products.ERP5.Document.File import File
from Products.ERP5.Document.Document import Document, ConversionError,\ from Products.ERP5.Document.Document import Document, ConversionError,\
VALID_TEXT_FORMAT_LIST VALID_TEXT_FORMAT_LIST
from os.path import splitext
from OFS.Image import Image as OFSImage from OFS.Image import Image as OFSImage
from OFS.Image import getImageInfo from OFS.Image import getImageInfo
from zLOG import LOG, WARNING from zLOG import LOG, WARNING
...@@ -191,7 +192,7 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -191,7 +192,7 @@ class Image(TextConvertableMixin, File, OFSImage):
security.declareProtected('View', 'tag') security.declareProtected('View', 'tag')
def tag(self, display=None, height=None, width=None, cookie=0, def tag(self, display=None, height=None, width=None, cookie=0,
alt=None, css_class=None, format='', quality=75, alt=None, css_class=None, format=None, quality=DEFAULT_QUALITY,
resolution=None, frame=None, **kw): resolution=None, frame=None, **kw):
"""Return HTML img tag.""" """Return HTML img tag."""
self._upradeImage() self._upradeImage()
...@@ -202,24 +203,18 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -202,24 +203,18 @@ class Image(TextConvertableMixin, File, OFSImage):
# display may be set from a cookie. # display may be set from a cookie.
image_size = self.getSizeFromImageDisplay(display) image_size = self.getSizeFromImageDisplay(display)
if (display is not None or resolution is not None or quality!=75 or format != ''\ convert_kw = dict(format=format, quality=quality, resolution=resolution,
or frame is not None) and image_size: frame=frame, image_size=image_size)
kw = dict(display=display, format=format, quality=quality, try:
resolution=resolution, frame=frame, image_size=image_size) mime, image = self.getConversion(**convert_kw)
try: except KeyError:
mime, image = self.getConversion(**kw) # Generate photo on-the-fly
except KeyError: mime, image = self._makeDisplayPhoto(image_size, **convert_kw)
# Generate photo on-the-fly self.setConversion(image, mime, **convert_kw)
mime, image = self._makeDisplayPhoto(**kw) width, height = image.width, image.height
self.setConversion(image, mime, **kw) # Set cookie for chosen size
width, height = (image.width, image.height) if cookie:
# Set cookie for chosen size self.REQUEST.RESPONSE.setCookie('display', display, path="/")
if cookie:
self.REQUEST.RESPONSE.setCookie('display', display, path="/")
else:
# TODO: Add support for on-the-fly resize?
height = self.getHeight()
width = self.getWidth()
if display: if display:
result = '<img src="%s?display=%s"' % (self.absolute_url(), display) result = '<img src="%s?display=%s"' % (self.absolute_url(), display)
...@@ -280,11 +275,13 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -280,11 +275,13 @@ class Image(TextConvertableMixin, File, OFSImage):
return links return links
security.declareProtected('Access contents information', 'displayMap') security.declareProtected('Access contents information', 'displayMap')
def displayMap(self, exclude=None, format='', quality=75, resolution=None): def displayMap(self, exclude=None, format='', quality=DEFAULT_QUALITY,
resolution=None):
"""Return list of displays with size info.""" """Return list of displays with size info."""
displays = [] displays = []
for id in self.displayIds(exclude): for id in self.displayIds(exclude):
if self._isGenerated(id, format=format, quality=quality, resolution=resolution): if self._isGenerated(id, format=format, quality=quality,\
resolution=resolution):
photo_width = self._photos[(id,format)].width photo_width = self._photos[(id,format)].width
photo_height = self._photos[(id,format)].height photo_height = self._photos[(id,format)].height
bytes = self._photos[(id,format)]._size() bytes = self._photos[(id,format)]._size()
...@@ -327,7 +324,7 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -327,7 +324,7 @@ class Image(TextConvertableMixin, File, OFSImage):
# Conversion API # Conversion API
security.declareProtected(Permissions.AccessContentsInformation, 'convert') security.declareProtected(Permissions.AccessContentsInformation, 'convert')
def convert(self, format, display=None, quality=75, resolution=None, frame=None, **kw): def convert(self, format, **kw):
""" """
Implementation of conversion for Image files Implementation of conversion for Image files
""" """
...@@ -339,18 +336,21 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -339,18 +336,21 @@ class Image(TextConvertableMixin, File, OFSImage):
data = aq_base(data) data = aq_base(data)
self.setConversion(data, mime=mime_type, format=format) self.setConversion(data, mime=mime_type, format=format)
return mime_type, data return mime_type, data
image_size = self.getSizeFromImageDisplay(display) image_size = self.getSizeFromImageDisplay(kw.get('display'))
if (display is not None or resolution is not None or quality != 75 or format != ''\ # store all keys usefull to convert or resize an image
or frame is not None) and image_size: # 'display' parameter can be discarded
kw = dict(display=display, format=format, quality=quality, convert_kw = {'quality': kw.get('quality', DEFAULT_QUALITY),
resolution=resolution, frame=frame, image_size=image_size) 'resolution': kw.get('resolution'),
try: 'frame': kw.get('frame'),
mime, image = self.getConversion(**kw) 'image_size': image_size,
except KeyError: 'format': format,
mime, image = self._makeDisplayPhoto(**kw) }
self.setConversion(image, mime, **kw) try:
return mime, image.data mime, image = self.getConversion(**convert_kw)
return self.getContentType(), self.getData() except KeyError:
mime, image = self._makeDisplayPhoto(**convert_kw)
self.setConversion(image, mime, **convert_kw)
return mime, image.data
# Display # Display
security.declareProtected('View', 'index_html') security.declareProtected('View', 'index_html')
...@@ -366,8 +366,7 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -366,8 +366,7 @@ class Image(TextConvertableMixin, File, OFSImage):
# Photo processing # Photo processing
# #
def _resize(self, display, width, height, quality=75, format='', def _resize(self, quality, width, height, format, resolution, frame):
resolution=None, frame=None):
"""Resize and resample photo.""" """Resize and resample photo."""
newimg = StringIO() newimg = StringIO()
...@@ -413,43 +412,26 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -413,43 +412,26 @@ class Image(TextConvertableMixin, File, OFSImage):
newimg.seek(0) newimg.seek(0)
return newimg return newimg
def _getDisplayData(self, display, format='', quality=75, resolution=None, frame=None, def _getDisplayData(self, format, quality, resolution, frame, image_size):
image_size=None):
"""Return raw photo data for given display.""" """Return raw photo data for given display."""
if display is None: width, height = self._getAspectRatioSize(*image_size)
(width, height) = (self.getWidth(), self.getHeight()) if ((width, height) == image_size or (width, height) == (0, 0))\
elif image_size is None: and quality == DEFAULT_QUALITY and resolution is None and frame is None\
(width, height) = self.getSizeFromImageDisplay(display) and not format:
else: # No resizing, no conversion, return raw image
(width, height) = image_size return self.getData()
if width == 0 and height == 0: return self._resize(quality, width, height, format, resolution, frame)
width = self.getWidth()
height = self.getHeight() def _makeDisplayPhoto(self, format=None, quality=DEFAULT_QUALITY,
(width, height) = self._getAspectRatioSize(width, height) resolution=None, frame=None, image_size=None):
if (width, height) == (0, 0):return self.getData()
return self._resize(display, width, height, quality, format=format,
resolution=resolution, frame=frame)
def _getDisplayPhoto(self, display, format='', quality=75, resolution=None, frame=None,
image_size=None):
"""Return photo object for given display."""
try:
base, ext = string.split(self.id, '.')
id = base + '_' + display + '.' + ext
except ValueError:
id = self.id +'_'+ display
image = OFSImage(id, self.getTitle(), self._getDisplayData(display, format=format,
quality=quality, resolution=resolution, frame=frame,
image_size=image_size))
return image
def _makeDisplayPhoto(self, display, format='', quality=75, resolution=None, frame=None,
image_size=None):
"""Create given display.""" """Create given display."""
image = self._getDisplayPhoto(display, format=format, quality=quality, width, height = image_size
resolution=resolution, frame=frame, base, ext = splitext(self.id)
image_size=image_size) id = '%s_%s_%s.%s'% (base, width, height, ext,)
return (image.content_type, aq_base(image)) image = OFSImage(id, self.getTitle(),
self._getDisplayData(format, quality, resolution,
frame, image_size))
return image.content_type, aq_base(image)
def _getAspectRatioSize(self, width, height): def _getAspectRatioSize(self, width, height):
"""Return proportional dimensions within desired size.""" """Return proportional dimensions within desired size."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment