Esempio n. 1
0
    def scales(self):
        """Returns information to initialize JCrop for all available scales
           on the current content with the given fieldname and interface."""

        scales = []
        croputils = IImageCroppingUtils(self.context)
        cropview = self.context.restrictedTraverse('@@crop-image')
        image_size = croputils.get_image_size(self.fieldname, self.interface)
        all_sizes = self.exclude_scales(getAllowedSizes())
        scale_names = all_sizes.keys()
        scale_names.sort()
        current_selected = self.request.get('scalename', all_sizes.keys()[0])
        # TODO: implement other imagefields
        large_image_url = self.image_url(self.fieldname)

        for size in scale_names:
            scale = dict()
            # scale jcrop config
            min_width, min_height = self._min_size(image_size, all_sizes[size])
            max_width, max_height = self.default_cropping_max_size[0],\
                self.default_cropping_max_size[1]
            ratio_width, ratio_height = all_sizes[size][0], all_sizes[size][1]

            # lookup saved crop info
            select_box = cropview._read(self.fieldname, size)
            is_cropped = True

            if select_box is None:
                select_box = (0, 0, min_width, min_height)
                is_cropped = False

            config = dict([
                ("allowResize", True),
                ("allowMove", True),
                ("trueSize", [image_size[0], image_size[1]]),
                ("boxWidth", self.default_editor_size[0]),
                ("boxHeight", self.default_editor_size[1]),
                ("setSelect", select_box),
                ("aspectRatio", "%.2f" % (
                    float(ratio_width) / float(ratio_height))),
                ("minSize", [min_width, min_height]),
                ("maxSize", [max_width, max_height]),
                ("imageURL", large_image_url),
            ])
            scale["config"] = json.dumps(config)
            # scale value/id
            scale["id"] = size
            scale["title"] = "%s %s" % (size, all_sizes[size])
            scale["selected"] = size == current_selected and 'selected' or ''
            # flag if saved cropped scale was found
            # this helps to prevent generating unused
            # default scales in preview column
            scale["is_cropped"] = is_cropped
            # TODO: this is for thumbnail live-preview
            scale["thumb_width"] = ratio_width
            scale["thumb_height"] = ratio_height

            scales.append(scale)
        return scales
Esempio n. 2
0
def apply_crops_after_copy(context, event):
    crops = IAnnotations(event.original).get(PAI_STORAGE_KEY)
    if not crops:
        return
    croputils = IImageCroppingUtils(context)
    request = getRequest()
    cropper = getMultiAdapter((context, request), name='crop-image')
    for fieldname in croputils.image_field_names():
        for crop_key in crops:
            if crop_key.startswith(fieldname):
                scalename = crop_key[len(fieldname) + 1:]
                cropper._crop(fieldname, scalename, crops[crop_key])
    def _crop(self, fieldname, scale, box):
        """switch between dexterity and Archetypes
        """
        croputils = IImageCroppingUtils(self.context)
        data = croputils.get_image_data(fieldname)

        original_file = StringIO(data)
        image = PIL.Image.open(original_file)
        image_format = image.format or self.DEFAULT_FORMAT

        cropped_image = image.crop(box)
        cropped_image_file = StringIO()
        cropped_image.save(cropped_image_file, image_format, quality=100)
        cropped_image_file.seek(0)

        croputils.save_cropped(fieldname, scale, cropped_image_file)

        # store crop information in annotations
        self._store(fieldname, scale, box)
Esempio n. 4
0
    def _crop(self, fieldname, scale, box):
        """Delegate to store.

        """
        storage = Storage(self.context)
        storage.store(fieldname, scale, box)

        if IImageCroppingDX.providedBy(self.context):
            return
        # AT BBB scaling.
        croputils = IImageCroppingUtils(self.context)
        data = croputils.get_image_data(fieldname)

        original_file = BytesIO(data)
        image = PIL.Image.open(original_file)
        image_format = image.format or self.DEFAULT_FORMAT

        cropped_image = image.crop(box)
        cropped_image_file = BytesIO()
        cropped_image.save(cropped_image_file, image_format, quality=100)
        cropped_image_file.seek(0)

        croputils.save_cropped(fieldname, scale, cropped_image_file)
Esempio n. 5
0
    def _crop(self, fieldname, scale, box, interface=None):
        """switch between dexterity and Archetypes
        """
        croputils = IImageCroppingUtils(self.context)
        field = croputils.get_image_field(fieldname)
        data = croputils.get_image_data(fieldname)

        original_file = StringIO(data)
        image = PIL.Image.open(original_file)
        image_format = image.format or self.DEFAULT_FORMAT

        cropped_image = image.crop(box)
        cropped_image_file = StringIO()
        cropped_image.save(cropped_image_file, image_format, quality=100)
        cropped_image_file.seek(0)

        croputils.save_cropped(fieldname, field, scale, cropped_image_file,
                               interface)

        # store crop information in annotations
        self._store(fieldname, scale, box)
Esempio n. 6
0
 def _croputils(self):
     return IImageCroppingUtils(self.context)
Esempio n. 7
0
    def scales(self, fieldname=None):
        """Returns information to initialize JCrop for all available scales
           on the current content with the given fieldname and interface."""

        scales = []
        croputils = IImageCroppingUtils(self.context)
        cropview = self.context.restrictedTraverse('@@crop-image')
        if fieldname is None:
            fieldname = self.fieldname
        image_size = croputils.get_image_size(fieldname, self.interface)
        all_sizes = getAllowedSizes()
        current_selected = self.request.get('scalename', all_sizes.keys()[0])
        large_image_url = self.image_url(fieldname)
        constrain_cropping = self._editor_settings.constrain_cropping
        cropping_for = self._editor_settings.cropping_for

        for size in all_sizes:
            if constrain_cropping and size not in cropping_for:
                continue
            scale = dict()
            # scale jcrop config
            min_width, min_height = self._min_size(image_size, all_sizes[size])
            max_width, max_height = self.default_cropping_max_size[0], \
                self.default_cropping_max_size[1]
            ratio_width, ratio_height = all_sizes[size][0], all_sizes[size][1]

            # lookup saved crop info
            select_box = cropview._read(fieldname, size)
            is_cropped = True

            if select_box is None:
                select_box = (0, 0, min_width, min_height)
                is_cropped = False

            config = dict([
                ("allowResize", True),
                ("allowMove", True),
                ("trueSize", [image_size[0], image_size[1]]),
                ("boxWidth", self.default_editor_size[0]),
                ("boxHeight", self.default_editor_size[1]),
                ("setSelect", select_box),
                ("aspectRatio",
                 "%.2f" % (float(ratio_width) / float(ratio_height))),
                ("minSize", [min_width, min_height]),
                ("maxSize", [max_width, max_height]),
                ("imageURL", large_image_url),
            ])
            scale["config"] = json.dumps(config)
            # scale value/id
            scale["id"] = size
            scale["title"] = "%s %s" % (size, all_sizes[size])
            scale["selected"] = size == current_selected and 'selected' or ''
            # flag if saved cropped scale was found
            # this helps to prevent generating unused
            # default scales in preview column
            scale["is_cropped"] = is_cropped
            # TODO: this is for thumbnail live-preview
            scale["thumb_width"] = ratio_width
            scale["thumb_height"] = ratio_height
            # safe original image url
            scale["image_url"] = large_image_url

            scales.append(scale)
        return scales
Esempio n. 8
0
 def image_field_names(self):
     return IImageCroppingUtils(self.context).image_field_names()