Skip to main content

Crops Images in Plone manually using cropper JS library

Project description

https://travis-ci.org/collective/plone.app.imagecropping.png?branch=master https://coveralls.io/repos/collective/plone.app.imagecropping/badge.png

Why do I need this?

Automatic cropping is already possible, plone.namedfile does already handle this via the direction parameter:

<img tal:define="scales context/@@images"
     tal:replace="structure python: scales.tag('image',
                  width=1200, height=800, direction='down')"
     />

However it only crops from the top/center of the image, so in some ocasions this is not what you want.

plone.app.imagecropping allows you to select the cropping area manually for each available image scale using the cropper Javascript library

How it works

There is a view @@croppingeditor available for every content type implementing IImageCroppingMarker via an object action. There are specific markers for Archetypes and Dexterity based types:

  • plone.app.imagecropping.dx.IImageCroppingDX

  • plone.app.imagecropping.at.IImageCroppingAT

The interfaces are implemented by default for Dexterity based plone.app.contenttypes (Plone 5 or Plone 4.x as addon) Image. Products.ATContentTypes (Plone 4.x) implements it on``ATImage`` and ATNewsItem.

There is also a Dexterity behavior called Enable Image Cropping which can be applied on custom content types containing at least one image.

The editor view hav a tabbed navigation on the top and two columns:

  • Image fields on tabbed navigation.

  • Image scales column on left

  • Cropping editor column on right.

The aspect ratio for the cropping area in the editor is automatically set to the image scale selected by the user.

https://raw.github.com/collective/plone.app.imagecropping/master/docs/editor.png

The image stored for this scale gets replaced with the cropped and scaled version. This way you can access them as you’re used to. For example:

<img tal:replace="structure context/@@images/image/mini" />

This also enables support for richtext editors such as TinyMCE to insert cropped scales into a textfield.

Automatic cropping behavior

You can use direction='down' for autocropped scales as usual. This package overrides the direction and delivers the cropped scale if it is available.

Load editor as overlay

The editor can also be loaded as an overlay anywhere. Just place a link to the @@croppingeditor url of an image (<image_base_url>/@@croppingeditor) and add some javascript. For example:

(function($) {
    $(function() {
        $("a[href$='@@croppingeditor']").prepOverlay({
            subtype:'ajax',
            formselector:'#coords',
            closeselector:"input[name='form.button.Cancel']"
        })

        $(document).bind("formOverlayLoadSuccess", function() {
            imagecropping = new ImageCropping();
            imagecropping.init_editor();
        })
    })
})(jQuery);

TODO: Show how to use with mockup/patterns.

Configuration

At Plone Site Setup under Content there is a new setting Image Cropping. There you can limit the scales to be shown in the cropping editor. By default all scales are shown.

You can also set those values in the profile of your (policy)product using plone.app.registry mechanism (file registry.xml).

Further Information

History

There has been a need for cropping for a long time and there are lots of addons around that have different ways to achieve this.

There is plip #10174 asking for adding image cropping to plone core which recently got rejected by the FWT. The cropping functionality should go into an addon first that also work for dexterity and can be pliped into core.

This package aims to be THE cropping solution for plone that ‘just works TM’.

Design decisions

  • make this package as minimally invasive as possible

    • therefore we store the cropped image immediately, so plone.namedfile traverser doesn’t need to care about cropping

    • users can access cropped images the same way as the access scales (so it works in richtext editors too)

  • support archetypes and dexterity content

  • a cropped image gets stored instead of the scaled image. if you want back the uncropped image scale you’ll need to remove the cropped version in the editor

Information about changes from version 1.x to 2.0

  • Plone 5.1 only.

  • New js library cropper.

  • Complete Javascript rewrite using patterns.

  • New editor layout.

  • Save/Remove using Ajax.

Information about changes from version 0.1 to 1.0

The marker interface for archetypes changed from plone.app.imagecropping.interfaces.IImageCropping to plone.app.imagecropping.at.IImageCroppingAT.

The marker interface for dexterity based types changed from plone.app.imagecropping.browser.scaling.interfaces.IImageCroppingScale to plone.app.imagecropping.dx.IImageCroppingDX.

The generic base interface is now plone.app.imagecropping.interfaces.IImageCroppingMarker. Do not use it directly on your, but use the marker to bind view or other adapters to image-cropping enabled types.

Possible extensions / changes for the future

  • allow to mark scales as auto-croppable in the imaging controlpanel. this enables cropped scales w/o manually defining the cropping area but would require some changes in Plone (extend traverser, change controlpanel)

  • see also the issue tracker

  • Peter Mathis (petschki)

  • Joscha Krutzki (joka)

  • Harald Frießnegger (fRiSi)

  • Martijn Zweistra (themask96)

  • Daniel Widerin (saily)

  • Wolfgang Thomas (pysailor)

  • Héctor Velarde (hvelarde)

  • Tom Gross (tom_gross)

  • Jens W. Klein (jensens)

  • Benjamin Stefaner (benniboy)

  • Peter Holzer (agitator)

Credits

The basic concept of plone.app.imagecropping has already been implemented in Yiorgis’s branch of plone.app.imaging

The library cropper by Fengyuan Chen was published under MIT License and is used since plone.app.imagecropping Version 2.0 for the JS part to select a croppig area.

Source Code

If you want to help with the development (improvement, update, bug-fixing, …) of plone.app.imagecropping this is a great idea!

The code is located in the github collective.

You can clone it and send a pull request or get access to the github-collective and work directly on a branch of the project.

Changelog

2.0 (2016-11-24)

  • Load plone_app_imagecropping bundle only for logged in users. [agitator]

  • Updated styles Removed unused settings [agitator]

  • Depend on Products.CMFPlone instead of Plone (no Archetypes by default). Fix conditional zcml. [jensens]

2.0b6 (2016-11-18)

  • Add Spanish translation [erral]

2.0b5 (2016-11-06)

  • Align editor template layout to Plone standards and place it within content-core macros. [thet]

  • Restrict image in navigation tab to not exceed the tabs size. [thet]

2.0b4 (2016-07-13)

  • Fix: Scales got lost after content with image was modified. This is solved by using the new scale factories from plone.scale/plone.namedfile. Needs plone.namedfile 4.0+ and plone.scale 1.5+. [jensens]

2.0b3 (2016-04-29)

  • Fix: Enable accidentially disabled JS bundle in resource registry. [jensens]

2.0b2 (2016-04-01)

  • Give IImageCroppingBehavior a shortname. [jensens]

  • Fix: Make croppingeditor play nice with pat-modal. [jensens]

2.0b1 (2016-03-29)

  • Run i18ndude and translated new/missing strings to German. [jensens]

  • Use more modern cropper Javascript library, kick out JCrop and rewrite all JS. It uses patterns now to initialize the cropper and image selections. [jensens]

  • Code refactoring and big overhaul to make it work with Plone 5 (only) and the new JS. [jensens]

  • Housekeeping: upgrades at one place, zca decorators, travis caching [jensens]

  • Added more Dutch translations. [maurits]

  • Added French translations. [laulaz]

1.3 (2015-12-10)

  • introduce Changed/Removed events after editing cropping information [petschki]

  • remove initial content scrolling (aka location.hash) [petschki]

  • Use “application/javascript” media type instead of the obsolete “text/javascript”. [hvelarde]

  • fix direction=’down’ handling. Deliver cropped scale if we have one. [petschki]

  • Purge proxy caches if needed on crop. [alecm]

  • Fix issue with crops disappearing for non-blob images (e.g. ATNewsItem images) [alecm]

  • Refactor upgrade step to reduce memory consumption and avoid restarts on instances running with supervisor’s memmon. [hvelarde]

1.2 (2014-10-15)

  • Add Finnish localization [datakurre]

  • Fix Chameleon compatibility [datakurre]

  • Remove hard dependency on plone.app.contenttypes to avoid compatibility issues with Plone 4.2 (fixes #57). [hvelarde]

  • Update package dependencies. [hvelarde]

  • fix error with copy & paste for dx image types #52 [pysailor]

  • do not include testing.zcml automatically and use the behavior for the testing-dx-type.

    ATTENTION: we no longer provide the cropping maker interface for plone.dexterity.content.DexterityContent automatically, please use plone.app.imagecropping.behaviors.IImageCroppingBehavior to enable cropping for your dexterity types. [fRiSi]

1.1 (2014-09-13)

  • scroll to selected scale in middle column after save. [jensens]

  • use field names (not ids) in editor view. Slightly better style now. [jensens]

  • add upgrade step for sane migration from 0.1 to 1.0 [fRiSi]

1.0 (2014-09-05)

  • fixed jcrop image to not scale wrong (!). [jensens]

  • better initial selection for cropping, also mark scales in left column cropped/ uncropped and show uncropped in its default appereance. [jensens]

  • Fix: Removed registration of Traverse for dexterity types since its base class does not support dexterity either. [jensens]

  • Added subscriber to recreate all scales on copied objects. [saily, jensens]

  • fixed some glitches in JCrop init process. [jensens]

  • added dexterity behavior for image cropping. [benniboy]

  • Major cleanup and split up code and interfaces into Dexterity and Archtypes. Also renamed/ moved Interfaces to be used as markers! Attention, in custom code minor changes are needed in order to point to the correct interfaces. see README for details. Also removed some superfluos/unused interface= passes to methods of IImageCroppingUtils. [jensens]

  • Update Jcrop to version 0.9.12. [hvelarde]

  • Use plone.app.robotframework instead of writing all keywords from scratch. This brings us autologin support for faster robot tests. Rewrite tests to test scenarios. [saily]

  • Drop Plone 4.1 support and tests. [saily]

  • Cleanup code, align to all coding conventions for Plone. Pep8, Flake8, pep3101, sort imports, remove grouped imports, … [saily]

  • Cleanup/refactor javascript code, don’t define global variables. [saily]

  • Add coveralls, code-analysis and update travis configuration. [saily]

  • Check for plone.namedfile not Dexterity. It can be used seperately. If plone.namedfile is used it needs to be at least version 2.0.1 [tomgross]

  • @@croppingeditor now displays a message to add croppable scales in the controlpanel if there are no croppable scales to show. (previously this caused a SiteError) [fRiSi]

  • Rename imagecropping_keywords.txt to keywords.robot to allow simple reusage in plone.app.robotframework. [saily]

  • pin zc.buildout=2.1.1 for travis-ci boostrap [petschki]

  • Added Russian translations [bogdangi]

  • Prevent fieldname loosing for for current field [bogdangi]

  • Fixed #21 (cropping was reset on modifying image) [tomgross, fRiSi]

  • Only test Plone 4.2 and 4.3 with Python 2.7 on Travis-CI [tomgross]

0.1rc2 (2013-05-03)

  • Include styles for authenticated users only. [saily]

  • Make tests work in Plone 4.1 [saily]

  • Rename acceptance to robot to align new plone.app.robotframework guidelines. [saily]

  • Pin plone.app.testing to make Plone 4.1 tests work. [saily]

  • Use correct dependency for plone.app.testing with extra [robot]. [saily]

  • Add cropping ui-tests using robotframework [saily]

  • Update bootstrap.py to work with zc.buildout 2.0 [saily]

  • Implemented #11 - Mark image scales as “croppable” [jensens]

  • Added tests for control panel and registry

  • Added Spanish and Brazilian Portuguese translations [hvelarde]

  • Small documentation update [saily]

0.1rc1 (2013-03-11)

  • add support for multiple image fields

  • refactored javascript includes so the editor can be loaded as overlay

  • fixed JS error when editor is invisible (ie editor is loaded in an overlay)

  • fixed edit/remove actions when editor is loaded as overlay

  • make editor view more convenient (disable columns)

  • update documentation [petschki]

0.1b1 (2013-03-03)

  • Made cropping work in dexterity-only sites [pysailor]

  • Add travis integration [saily]

0.1a2 (2012-11-10)

  • fix tests

  • add test setups for Plone 4.1-4.3

  • Products.CMFPlone dependency. Right now we only support Plone >= 4.1

  • make dexterity support optional [petschki]

0.1a1 (2012-11-05)

  • public alpha release [petschki]

  • Package created using templer [fRiSi]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plone.app.imagecropping-2.0.tar.gz (565.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page