def get(self, instance, **kwargs):
        if not self.use_fstorage(instance):
            return ImageField.get(self, instance, **kwargs)

        request = instance.REQUEST
        if not isinstance(request, type('')) and \
                instance.REQUEST.get('%s_migrate' % self.getName(), ''):
            # migrate scales
            kwargs['migrate'] = True
            for n in self.getAvailableSizes(instance).keys():
                self.getScale(instance, scale=n, **kwargs)
        return ImageField.get(self, instance, **kwargs)
    def getScale(self, instance, scale=None, **kwargs):
        """ Scale getter
        """
        img = self.getAccessor(instance)()
        size = img.getSize()

        if not size:
            return None

        # BBB Backward compatible.
        # XXX This should be removed in Products.EEAContentTypes > 2.25
        if isinstance(img, ZODBImage):
            return ImageField.getScale(self, instance, scale, **kwargs)

        return super(ImageBlobField,
                     self).getScale(instance, scale, **kwargs)
    def getScale(self, instance, scale=None, **kwargs):
        """ Scale getter
        """
        img = self.getAccessor(instance)()
        size = img.getSize()

        if not size:
            return None

        # BBB Backward compatible.
        # XXX This should be removed in Products.EEAContentTypes > 2.25
        if isinstance(img, ZODBImage):
            return ImageField.getScale(self, instance, scale, **kwargs)

        return super(ImageBlobField,
                     self).getScale(instance, scale, **kwargs)
    def tag(self, instance, scale=None, height=None, width=None, alt=None,
            css_class=None, title=None, **kwargs):
        """Create a tag including scale
        """

        image = self.getScale(instance, scale=scale)
        if not self.use_fstorage(instance) and not isinstance(image, AWSFile):
            return ImageField.tag(self, instance, scale=scale, height=height,
                                  width=width, alt=alt, css_class=css_class,
                                  title=title, **kwargs)
        if image:
            img_width, img_height = self.getSize(instance, scale=scale)
            url = image.absolute_url()
        else:
            img_height = 0
            img_width = 0
            url = instance.absolute_url()

        if height is None:
            height = img_height
        if width is None:
            width = img_width

        values = {'src': url,
                  'alt': escape(alt and alt or instance.Title(), 1),
                  'title': escape(title and title or instance.Title(), 1),
                  'height': height,
                  'width': width}

        result = '<img src="%(src)s" alt="%(alt)s" title="%(title)s" '\
                 'height="%(height)s" width="%(width)s"' % values

        if css_class is not None:
            result = '%s class="%s"' % (result, css_class)

        for key, value in kwargs.items():
            if value:
                result = '%s %s="%s"' % (result, key, value)

        return '%s />' % result
Esempio n. 5
0
def rescaleOriginal(context, max_size=(1200,900), dry_run=True):

    """
    Steps:
        * If Image, use own data --or--
        * If News Item, use image field --or--
        * Use leadImage field
        * Determine if it's greater than max dimensions (size)
        * If yes, scale it down and reset
    """
    if not HAS_PIL:
        return False

    portal_type = context.portal_type

    if portal_type == 'Image':
        imageObj = context
        imageField = ImageField()

    else:

        if portal_type == 'News Item':
            imageField = context.getField('image')
        else:
            imageField = context.getField(IMAGE_FIELD_NAME)

        imageObj = imageField.get(context)
        
    if not imageObj:
        status = False
        width = height = 0
        new_dimensions = (0,0)
    elif imageObj.content_type not in ('image/jpeg', 'image/png', 'image/gif'):
        status = False
        width = height = 0
        new_dimensions = (0,0)
        LOG('Products.agCommon.rescaleOriginal', INFO, "Invalid image type %s for %s" % (imageObj.content_type, context.absolute_url()) )
    else:

        width = imageObj.width
        height = imageObj.height
        
        new_dimensions = calculateDimensions(imageObj, max_size=max_size)
        
        if (width, height) > new_dimensions:
            if not dry_run:
                if isinstance(imageObj.data, str):
                    imageData = imageObj.data
                else:
                    imageData = imageObj.data.data

                (resizedImage, format) = imageField.scale(imageData, *new_dimensions)

                if portal_type == 'Image':
                    context.setImage(resizedImage.read())
            
                else:
                    imageField.set(context, resizedImage.read())

            status = True

        else:
            status = False

    return (status, portal_type, context.absolute_url(), (width, height), new_dimensions)
Esempio n. 6
0
from Products.Archetypes.Field import ImageField
from Products.Archetypes.Field import ImageWidget
from Products.Archetypes.Field import ReferenceField
from Products.Archetypes.Field import ReferenceWidget
from Products.Archetypes.public import SelectionWidget
from Products.Archetypes.public import registerType
from Products.Archetypes.references import HoldingReference
from Products.CMFPlone.utils import safe_unicode
from zope.interface import implements

schema = Person.schema.copy() + atapi.Schema((
    ImageField(
        "Signature",
        widget=ImageWidget(
            label=_("Signature"),
            description=_("Upload a scanned signature to be used on printed "
                          "analysis results reports. Ideal size is 250 pixels "
                          "wide by 150 high"),
        ),
    ),
    ReferenceField(
        "Departments",
        required=0,
        vocabulary_display_path_bound=sys.maxint,
        allowed_types=("Department", ),
        relationship="LabContactDepartment",
        vocabulary="_departmentsVoc",
        referenceClass=HoldingReference,
        multiValued=1,
        widget=ReferenceWidget(
            checkbox_bound=0,