Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
Archetypes.Field.ScalableImage Class Reference
Collaboration diagram for Archetypes.Field.ScalableImage:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def Variants
def getPhoto
def getDisplays
def checkForVariant
def index_html
def tag
def update_data
def ZCacheable_setManagerId

Public Attributes

 displays

Static Public Attributes

string meta_type = 'Scalable Image'
 isBinary = lambdaself:True
tuple security = ClassSecurityInfo()
tuple variant = DynVariantWrapper()

Private Member Functions

def _resize

Private Attributes

 _photos

Static Private Attributes

 __implements__ = BaseImage.__implements__

Detailed Description

A scalable image class.

Definition at line 2530 of file Field.py.


Constructor & Destructor Documentation

def Archetypes.Field.ScalableImage.__init__ (   self,
  id,
  title = '',
  file = '',
  displays = {} 
)

Definition at line 2541 of file Field.py.

02541 
02542     def __init__(self, id, title='', file='', displays={}):
02543         deprecated('ScalableImage (for PhotoField) is deprecated after work '
02544                    'done on ImageField and ATImage. It will be removed in '
02545                    'Archetypes 1.5. If someone like to keep the code please '
02546                    'move it over to an own Product in MoreFieldsAndWidgets '
02547                    'repository.'
02548         )        
02549         BaseImage.__init__(self, id, title, file)
02550         self._photos = OOBTree()
02551         self.displays = displays

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def Archetypes.Field.ScalableImage._resize (   self,
  size,
  quality = 100 
) [private]
Resize and resample photo.

Definition at line 2692 of file Field.py.

02692 
02693     def _resize(self, size, quality=100):
02694         """Resize and resample photo."""
02695         image = StringIO()
02696 
02697         width, height = size
02698 
02699         try:
02700             if HAS_PIL:
02701                 img = PIL.Image.open(StringIO(str(self.data)))
02702                 fmt = img.format
02703                 # Resize photo
02704                 img.thumbnail((width, height))
02705                 # Store copy in image buffer
02706                 img.save(image, fmt, quality=quality)
02707             else:
02708                 if sys.platform == 'win32':
02709                     from win32pipe import popen2
02710                     imgin, imgout = popen2(('convert -quality %s '
02711                                             '-geometry %sx%s - -'
02712                                             % (quality, width, height), 'b'))
02713                 else:
02714                     from popen2 import Popen3
02715                     convert=Popen3(('convert -quality %s '
02716                                     '-geometry %sx%s - -'
02717                                     % (quality, width, height)))
02718                     imgout=convert.fromchild
02719                     imgin=convert.tochild
02720 
02721                 imgin.write(str(self.data))
02722                 imgin.close()
02723                 image.write(imgout.read())
02724                 imgout.close()
02725 
02726                 # Wait for process to close if unix.
02727                 # Should check returnvalue for wait
02728                 if sys.platform !='win32':
02729                     convert.wait()
02730 
02731                 image.seek(0)
02732 
02733         except (ConflictError, KeyboardInterrupt):
02734             raise
02735         except Exception, e:
02736             log_exc('Error while resizing image')
02737 
02738         return image

Here is the call graph for this function:

Here is the caller graph for this function:

Create variant if not there.

Definition at line 2581 of file Field.py.

02581 
02582     def checkForVariant(self, size):
02583         """Create variant if not there."""
02584         if size in self.displays.keys():
02585             # Create resized copy, if it doesnt already exist
02586             if not self._photos.has_key(size):
02587                 self._photos[size] = BaseImage(
02588                     size, size, self._resize(self.displays.get(size, (0,0)))
02589                     )
02590             # a copy with a content type other than image/* exists, this
02591             # probably means that the last resize process failed. retry
02592             elif not self._photos[size].getContentType().startswith('image'):
02593                 self._photos[size] = BaseImage(
02594                     size, size, self._resize(self.displays.get(size, (0,0)))
02595                     )
02596             return True
02597         else:
02598             return False

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2566 of file Field.py.

02566 
02567     def getDisplays(self):
02568         result = []
02569         for name, size in self.displays.items():
02570             result.append({'name':name, 'label':'%s (%dx%d)' % (
02571                 name, size[0], size[1]),'size':size}
02572                 )
02573 
02574         #sort ascending by size
02575         result.sort(lambda d1,d2: cmp(
02576             d1['size'][0]*d1['size'][0],
02577             d2['size'][1]*d2['size'][1])
02578             )
02579         return result

def Archetypes.Field.ScalableImage.getPhoto (   self,
  size 
)
returns the Photo of the specified size

Definition at line 2561 of file Field.py.

02561 
02562     def getPhoto(self,size):
02563         '''returns the Photo of the specified size'''
02564         return self._photos[size]

Here is the caller graph for this function:

def Archetypes.Field.ScalableImage.index_html (   self,
  REQUEST,
  RESPONSE,
  size = None 
)
Return the image data.

Definition at line 2600 of file Field.py.

02600 
02601     def index_html(self, REQUEST, RESPONSE, size=None):
02602         """Return the image data."""
02603         if self.checkForVariant(size):
02604             return self.getPhoto(size).index_html(REQUEST, RESPONSE)
02605         return BaseImage.index_html(self, REQUEST, RESPONSE)

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Field.ScalableImage.tag (   self,
  height = None,
  width = None,
  alt = None,
  scale = False,
  xscale = False,
  yscale = False,
  css_class = None,
  title = None,
  size = 'original',
  args 
)
Return an HTML img tag (See OFS.Image)

Definition at line 2609 of file Field.py.

02609 
02610             title=None, size='original', **args):
02611         """Return an HTML img tag (See OFS.Image)"""
02612 
02613         # Default values
02614         w=self.width
02615         h=self.height
02616 
02617         if height is None or width is None:
02618 
02619             if size in self.displays.keys():
02620                 if not self._photos.has_key(size):
02621                     # This resized image isn't created yet.
02622                     # Calculate a size for it
02623                     x,y = self.displays.get(size)
02624                     try:
02625                         if self.width > self.height:
02626                             w=x
02627                             h=int(round(1.0/(float(self.width)/w/self.height)))
02628                         else:
02629                             h=y
02630                             w=int(round(1.0/(float(self.height)/x/self.width)))
02631                     except ValueError:
02632                         # OFS.Image only knows about png, jpeg and gif.
02633                         # Other images like bmp will not have height and
02634                         # width set, and will generate a ValueError here.
02635                         # Everything will work, but the image-tag will render
02636                         # with height and width attributes.
02637                         w=None
02638                         h=None
02639                 else:
02640                     # The resized image exist, get it's size
02641                     photo = self._photos.get(size)
02642                     w=photo.width
02643                     h=photo.height
02644 
02645         if height is None: height=h
02646         if width is None:  width=w
02647 
02648         # Auto-scaling support
02649         xdelta = xscale or scale
02650         ydelta = yscale or scale
02651 
02652         if xdelta and width:
02653             width =  str(int(round(int(width) * xdelta)))
02654         if ydelta and height:
02655             height = str(int(round(int(height) * ydelta)))
02656 
02657         result='<img src="%s/variant/%s"' % (self.absolute_url(), escape(size))
02658 
02659         if alt is None:
02660             alt=getattr(self, 'title', '')
02661         result = '%s alt="%s"' % (result, escape(alt, 1))
02662 
02663         if title is None:
02664             title=getattr(self, 'title', '')
02665         result = '%s title="%s"' % (result, escape(title, 1))
02666 
02667         if height:
02668             result = '%s height="%s"' % (result, height)
02669 
02670         if width:
02671             result = '%s width="%s"' % (result, width)
02672 
02673         if not 'border' in [ x.lower() for x in args.keys()]:
02674             result = '%s border="0"' % result
02675 
02676         if css_class is not None:
02677             result = '%s class="%s"' % (result, css_class)
02678 
02679         for key in args.keys():
02680             value = args.get(key)
02681             result = '%s %s="%s"' % (result, key, value)
02682 
02683         return '%s />' % result

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Field.ScalableImage.update_data (   self,
  data,
  content_type = None,
  size = None 
)
    Update/upload image -> remove all copies

Definition at line 2685 of file Field.py.

02685 
02686     def update_data(self, data, content_type=None, size=None):
02687         """
02688             Update/upload image -> remove all copies
02689         """
02690         BaseImage.update_data(self, data, content_type, size)
02691         self._photos = OOBTree()

Here is the caller graph for this function:

Definition at line 2556 of file Field.py.

02556 
02557     def Variants(self):
02558         # Returns a variants wrapper instance
02559         return DynVariant().__of__(self)

def Archetypes.Field.ScalableImage.ZCacheable_setManagerId (   self,
  manager_id,
  REQUEST = None 
)
Changes the manager_id for this object.
   overridden because we must propagate the change to all variants

Definition at line 2741 of file Field.py.

02741 
02742     def ZCacheable_setManagerId(self, manager_id, REQUEST=None):
02743         '''Changes the manager_id for this object.
02744            overridden because we must propagate the change to all variants'''
02745         for size in self._photos.keys():
02746             variant = self.getPhoto(size).__of__(self)
02747             variant.ZCacheable_setManagerId(manager_id)
02748         inherited_attr = ScalableImage.inheritedAttribute('ZCacheable_setManagerId')
02749         return inherited_attr(self, manager_id, REQUEST)
02750 
02751 
02752 InitializeClass(ScalableImage)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Archetypes.Field.ScalableImage.__implements__ = BaseImage.__implements__ [static, private]

Definition at line 2533 of file Field.py.

Definition at line 2549 of file Field.py.

Definition at line 2550 of file Field.py.

Archetypes.Field.ScalableImage.isBinary = lambdaself:True [static]

Definition at line 2537 of file Field.py.

string Archetypes.Field.ScalableImage.meta_type = 'Scalable Image' [static]

Definition at line 2535 of file Field.py.

tuple Archetypes.Field.ScalableImage.security = ClassSecurityInfo() [static]

Definition at line 2539 of file Field.py.

Definition at line 2553 of file Field.py.


The documentation for this class was generated from the following file: