Commit c01ceb87 authored by Jean-Paul Smets's avatar Jean-Paul Smets

Added support for frame access. This is useful for example to render pages in...

Added support for frame access. This is useful for example to render pages in a PDF file and possibly pages in a fax file.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17509 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 00bea812
...@@ -185,7 +185,7 @@ class Image(File, OFSImage): ...@@ -185,7 +185,7 @@ class Image(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='', quality=75,
resolution=None, **kw): resolution=None, frame=None, **kw):
"""Return HTML img tag.""" """Return HTML img tag."""
self._upradeImage() self._upradeImage()
...@@ -194,14 +194,17 @@ class Image(File, OFSImage): ...@@ -194,14 +194,17 @@ class Image(File, OFSImage):
display = self.REQUEST.cookies.get('display', None) display = self.REQUEST.cookies.get('display', None)
# display may be set from a cookie. # display may be set from a cookie.
if (display is not None or resolution is not None or quality!=75 or format!='')\ if (display is not None or resolution is not None or quality!=75 or format != ''\
and defaultdisplays.has_key(display): or frame is not None) and defaultdisplays.has_key(display):
if not self.hasConversion(display=display, format=format, if not self.hasConversion(display=display, format=format,
quality=quality, resolution=resolution): quality=quality, resolution=resolution,
frame=frame):
# Generate photo on-the-fly # Generate photo on-the-fly
self._makeDisplayPhoto(display, format=format, quality=quality, resolution=resolution) self._makeDisplayPhoto(display, format=format, quality=quality,
resolution=resolution, frame=frame)
mime, image = self.getConversion(display=display, format=format, mime, image = self.getConversion(display=display, format=format,
quality=quality ,resolution=resolution) quality=quality ,resolution=resolution,
frame=frame)
width, height = (image.width, image.height) width, height = (image.width, image.height)
# Set cookie for chosen size # Set cookie for chosen size
if cookie: if cookie:
...@@ -291,42 +294,50 @@ class Image(File, OFSImage): ...@@ -291,42 +294,50 @@ class Image(File, OFSImage):
# Conversion API # Conversion API
security.declareProtected(Permissions.ModifyPortalContent, 'convert') security.declareProtected(Permissions.ModifyPortalContent, 'convert')
def convert(self, format, display=None, quality=75, resolution=None): def convert(self, format, display=None, quality=75, resolution=None, frame=None):
""" """
Implementation of conversion for PDF files Implementation of conversion for PDF files
""" """
if format in ('text', 'txt', 'html', 'base_html', 'stripped-html'): if format in ('text', 'txt', 'html', 'base_html', 'stripped-html'):
return None, None return None, None
if (display is not None or resolution is not None or quality != 75 or format != '')\ if (display is not None or resolution is not None or quality != 75 or format != ''\
and defaultdisplays.has_key(display): or frame is not None) and defaultdisplays.has_key(display):
if not self.hasConversion(display=display, format=format, if not self.hasConversion(display=display, format=format,
quality=quality,resolution=resolution): quality=quality, resolution=resolution,
frame=frame):
# Generate photo on-the-fly # Generate photo on-the-fly
self._makeDisplayPhoto(display, format=format, quality=quality,resolution=resolution) self._makeDisplayPhoto(display, format=format, quality=quality,
resolution=resolution, frame=frame)
# Return resized image # Return resized image
mime, image = self.getConversion(display=display, format=format, mime, image = self.getConversion(display=display, format=format,
quality=quality ,resolution=resolution) quality=quality ,resolution=resolution,
frame=frame)
return mime, image.data return mime, image.data
return self.getContentType(), self.getData() return self.getContentType(), self.getData()
# Display # Display
security.declareProtected('View', 'index_html') security.declareProtected('View', 'index_html')
def index_html(self, REQUEST, RESPONSE, display=None, format='', quality=75, resolution=None): def index_html(self, REQUEST, RESPONSE, display=None, format='', quality=75,
resolution=None, frame=None):
"""Return the image data.""" """Return the image data."""
self._upradeImage() self._upradeImage()
_setCacheHeaders(self, dict(display=display, format=format, quality=quality, resolution=resolution)) _setCacheHeaders(self, dict(display=display, format=format, quality=quality,
resolution=resolution, frame=frame))
# display may be set from a cookie (?) # display may be set from a cookie (?)
if (display is not None or resolution is not None or quality!=75 or format != '')\ if (display is not None or resolution is not None or quality != 75 or format != ''\
and defaultdisplays.has_key(display): or frame is not None) and defaultdisplays.has_key(display):
if not self.hasConversion(display=display, format=format, if not self.hasConversion(display=display, format=format,
quality=quality,resolution=resolution): quality=quality, resolution=resolution,
frame=frame):
# Generate photo on-the-fly # Generate photo on-the-fly
self._makeDisplayPhoto(display, format=format, quality=quality,resolution=resolution) self._makeDisplayPhoto(display, format=format, quality=quality,
resolution=resolution, frame=frame)
# Return resized image # Return resized image
mime, image = self.getConversion(display=display, format=format, mime, image = self.getConversion(display=display, format=format,
quality=quality ,resolution=resolution) quality=quality ,resolution=resolution,
frame=frame)
RESPONSE.setHeader('Content-Type', mime) RESPONSE.setHeader('Content-Type', mime)
return image.index_html(REQUEST, RESPONSE) return image.index_html(REQUEST, RESPONSE)
...@@ -338,37 +349,61 @@ class Image(File, OFSImage): ...@@ -338,37 +349,61 @@ class Image(File, OFSImage):
# Photo processing # Photo processing
# #
def _resize(self, display, width, height, quality=75, format='', resolution=None): def _resize(self, display, width, height, quality=75, format='',
resolution=None, frame=None):
"""Resize and resample photo.""" """Resize and resample photo."""
newimg = StringIO() newimg = StringIO()
# Prepare the format prefix
if format:
format = '%s:' % format
else:
format = ''
# Prepare the frame suffix
if frame is not None:
frame = '[%s]' % frame
else:
frame = ''
if sys.platform == 'win32': if sys.platform == 'win32':
# XXX - Does win32 support pipe ?
from win32pipe import popen2 from win32pipe import popen2
if resolution is None: if resolution is None:
imgin, imgout = popen2('convert -quality %s -geometry %sx%s - -' imgin, imgout = popen2('convert -quality %s -geometry %sx%s -%s %s-'
% (quality, width, height), 'b') % (quality, width, height, frame, format), 'b')
else: else:
imgin, imgout = popen2('convert -density %sx%s -quality %s -geometry %sx%s - -' imgin, imgout = popen2('convert -density %sx%s -quality %s -geometry %sx%s -%s %s-'
% (resolution, resolution, quality, width, height), 'b') % (resolution, resolution, quality, width, height, frame, format), 'b')
else: else:
from popen2 import popen2 from popen2 import popen2
if resolution is None: if resolution is None:
imgout, imgin = popen2('convert -quality %s -geometry %sx%s - -' imgout, imgin = popen2('convert -quality %s -geometry %sx%s -%s %s-'
% (quality, width, height)) % (quality, width, height, frame, format))
else: else:
# LOG('Resolution',0,str(resolution)) # LOG('Resolution',0,str(resolution))
cmd = 'convert -density %sx%s -quality %s -geometry %sx%s - -' % (resolution, cmd = 'convert -density %sx%s -quality %s -geometry %sx%s -%s %s-' % (
resolution, quality, width, height) resolution, resolution, quality, width, height, frame, format)
imgout, imgin = popen2(cmd) imgout, imgin = popen2(cmd)
imgin.write(str(self.getData())) def writeData(stream, data):
if isinstance(data, str):
stream.write(str(self.getData()))
else:
# Use PData structure to prevent
# consuming too much memory
while data is not None:
stream.write(data.data)
data = data.next
writeData(imgin, self.getData())
imgin.close() imgin.close()
newimg.write(imgout.read()) newimg.write(imgout.read())
newimg.seek(0) newimg.seek(0)
return newimg return newimg
def _getDisplayData(self, display, format='', quality=75, resolution=None): def _getDisplayData(self, display, format='', quality=75, resolution=None, frame=None):
"""Return raw photo data for given display.""" """Return raw photo data for given display."""
if display is None: if display is None:
(width, height) = (self.getWidth(), self.getHeight()) (width, height) = (self.getWidth(), self.getHeight())
...@@ -378,26 +413,30 @@ class Image(File, OFSImage): ...@@ -378,26 +413,30 @@ class Image(File, OFSImage):
width = self.getWidth() width = self.getWidth()
height = self.getHeight() height = self.getHeight()
(width, height) = self._getAspectRatioSize(width, height) (width, height) = self._getAspectRatioSize(width, height)
return self._resize(display, width, height, quality, format=format, resolution=resolution) return self._resize(display, width, height, quality, format=format,
resolution=resolution, frame=frame)
def _getDisplayPhoto(self, display, format='', quality=75, resolution=None): def _getDisplayPhoto(self, display, format='', quality=75, resolution=None, frame=None):
"""Return photo object for given display.""" """Return photo object for given display."""
try: try:
base, ext = string.split(self.id, '.') base, ext = string.split(self.id, '.')
id = base+'_'+display+'.'+ext id = base + '_' + display + '.' + ext
except ValueError: except ValueError:
id = self.id+'_'+display id = self.id +'_'+ display
image = OFSImage(id, self.getTitle(), self._getDisplayData(display, format=format, image = OFSImage(id, self.getTitle(), self._getDisplayData(display, format=format,
quality=quality,resolution=resolution)) quality=quality, resolution=resolution, frame=frame))
return image return image
def _makeDisplayPhoto(self, display, format='', quality=75, resolution=None): def _makeDisplayPhoto(self, display, format='', quality=75, resolution=None, frame=None):
"""Create given display.""" """Create given display."""
if not self.hasConversion(display=display, format=format, quality=quality,resolution=resolution): if not self.hasConversion(display=display, format=format, quality=quality,
image = self._getDisplayPhoto(display, format=format, quality=quality, resolution=resolution) resolution=resolution, frame=frame):
image = self._getDisplayPhoto(display, format=format, quality=quality,
resolution=resolution, frame=frame)
self.setConversion(image, mime=image.content_type, self.setConversion(image, mime=image.content_type,
display=display, format=format, display=display, format=format,
quality=quality ,resolution=resolution) quality=quality, resolution=resolution,
frame=frame)
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