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
from Products.ERP5.Document.File import File
from Products.ERP5.Document.Document import Document, ConversionError,\
VALID_TEXT_FORMAT_LIST
from os.path import splitext
from OFS.Image import Image as OFSImage
from OFS.Image import getImageInfo
from zLOG import LOG, WARNING
......@@ -191,7 +192,7 @@ class Image(TextConvertableMixin, File, OFSImage):
security.declareProtected('View', 'tag')
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):
"""Return HTML img tag."""
self._upradeImage()
......@@ -202,24 +203,18 @@ class Image(TextConvertableMixin, File, OFSImage):
# display may be set from a cookie.
image_size = self.getSizeFromImageDisplay(display)
if (display is not None or resolution is not None or quality!=75 or format != ''\
or frame is not None) and image_size:
kw = dict(display=display, format=format, quality=quality,
resolution=resolution, frame=frame, image_size=image_size)
convert_kw = dict(format=format, quality=quality, resolution=resolution,
frame=frame, image_size=image_size)
try:
mime, image = self.getConversion(**kw)
mime, image = self.getConversion(**convert_kw)
except KeyError:
# Generate photo on-the-fly
mime, image = self._makeDisplayPhoto(**kw)
self.setConversion(image, mime, **kw)
width, height = (image.width, image.height)
mime, image = self._makeDisplayPhoto(image_size, **convert_kw)
self.setConversion(image, mime, **convert_kw)
width, height = image.width, image.height
# Set cookie for chosen size
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:
result = '<img src="%s?display=%s"' % (self.absolute_url(), display)
......@@ -280,11 +275,13 @@ class Image(TextConvertableMixin, File, OFSImage):
return links
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."""
displays = []
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_height = self._photos[(id,format)].height
bytes = self._photos[(id,format)]._size()
......@@ -327,7 +324,7 @@ class Image(TextConvertableMixin, File, OFSImage):
# Conversion API
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
"""
......@@ -339,18 +336,21 @@ class Image(TextConvertableMixin, File, OFSImage):
data = aq_base(data)
self.setConversion(data, mime=mime_type, format=format)
return mime_type, data
image_size = self.getSizeFromImageDisplay(display)
if (display is not None or resolution is not None or quality != 75 or format != ''\
or frame is not None) and image_size:
kw = dict(display=display, format=format, quality=quality,
resolution=resolution, frame=frame, image_size=image_size)
image_size = self.getSizeFromImageDisplay(kw.get('display'))
# store all keys usefull to convert or resize an image
# 'display' parameter can be discarded
convert_kw = {'quality': kw.get('quality', DEFAULT_QUALITY),
'resolution': kw.get('resolution'),
'frame': kw.get('frame'),
'image_size': image_size,
'format': format,
}
try:
mime, image = self.getConversion(**kw)
mime, image = self.getConversion(**convert_kw)
except KeyError:
mime, image = self._makeDisplayPhoto(**kw)
self.setConversion(image, mime, **kw)
mime, image = self._makeDisplayPhoto(**convert_kw)
self.setConversion(image, mime, **convert_kw)
return mime, image.data
return self.getContentType(), self.getData()
# Display
security.declareProtected('View', 'index_html')
......@@ -366,8 +366,7 @@ class Image(TextConvertableMixin, File, OFSImage):
# Photo processing
#
def _resize(self, display, width, height, quality=75, format='',
resolution=None, frame=None):
def _resize(self, quality, width, height, format, resolution, frame):
"""Resize and resample photo."""
newimg = StringIO()
......@@ -413,43 +412,26 @@ class Image(TextConvertableMixin, File, OFSImage):
newimg.seek(0)
return newimg
def _getDisplayData(self, display, format='', quality=75, resolution=None, frame=None,
image_size=None):
def _getDisplayData(self, format, quality, resolution, frame, image_size):
"""Return raw photo data for given display."""
if display is None:
(width, height) = (self.getWidth(), self.getHeight())
elif image_size is None:
(width, height) = self.getSizeFromImageDisplay(display)
else:
(width, height) = image_size
if width == 0 and height == 0:
width = self.getWidth()
height = self.getHeight()
(width, height) = self._getAspectRatioSize(width, height)
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):
width, height = self._getAspectRatioSize(*image_size)
if ((width, height) == image_size or (width, height) == (0, 0))\
and quality == DEFAULT_QUALITY and resolution is None and frame is None\
and not format:
# No resizing, no conversion, return raw image
return self.getData()
return self._resize(quality, width, height, format, resolution, frame)
def _makeDisplayPhoto(self, format=None, quality=DEFAULT_QUALITY,
resolution=None, frame=None, image_size=None):
"""Create given display."""
image = self._getDisplayPhoto(display, format=format, quality=quality,
resolution=resolution, frame=frame,
image_size=image_size)
return (image.content_type, aq_base(image))
width, height = image_size
base, ext = splitext(self.id)
id = '%s_%s_%s.%s'% (base, width, height, ext,)
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):
"""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