[opencms-dev] Downscale large images automatically upon upload / create image resource

Henning Treu henning.treu at codecentric.de
Tue Oct 1 08:59:24 CEST 2013


Hi Paul,

> If not, can I avoid touching core classes in OpenCms if I was to create such functionality myself? (Or: Can I add-on a custom “create image resource” handler class? How do I make OpenCms use it?) 

there is a way to implement your own functionality like this. In manifest.xml there is an element <class> which lets you provide a full qualified class name for your custom implementation of I_CmsModuleAction. You can register event listeners in the initialize method and process  EVENT_RESOURCE_CREATED events. Make sure to retain the admin CmsObject from the initialise method.
Something like this:

@Override
public void initialize(CmsObject adminCms, CmsConfigurationManager configurationManager, CmsModule module) {
	CmsEventManager eventManager = OpenCms.getEventManager();

	LOG.info("Registering cms event listeners:");
	CCECmsEventListener listener = new MyCustomEventListener();
	listener.setCmsObject(adminCms); // set the admin cms object for future use
	eventManager.addCmsEventListener(listener, 
		new int[] { I_CmsEventListener.EVENT_RESOURCE_CREATED });
	LOG.info(listener.getClass().getName() + " registered.");
}

In your listener you might use the CmsImageScaler. Its quite easy to use. I do it like this:

private CmsImageScaler createImageScaler(int width, int height) {
	CmsImageScaler scaler = new CmsImageScaler();

	scaler.setWidth(width);
	scaler.setHeight(height);
	scaler.setType(3); // preserve aspect ratio
	scaler.setRenderMode(Simapi.RENDER_QUALITY); // QUALITY or SPEED
	scaler.setQuality(100); // JPEG compression quality

	return scaler;
}


Cheers
Henning


> Alternatively, is there a way to simply disallow upload of “large” (as defined by me) images?
>  
> Any advice is much appreciated! :)
>  
> Challenge: 
> Some editors don’t bother to downscale images before uploading. This is a problem, because although all our images are downscaled in-page, we still link the in-page image to the original. Also, if the resolution is really huge, we could run into out-of-memory situations on the server when creating the downscaled versions displayed in-page on the site. (Especially if OpenCms was to initiate multiple downscales at roughly the same time – after flushing the image cache, for example.)
>  
> Imagined solution:
> Eliminate the problem by setting a max resolution for images. Possibly implemented by automatically downscaling large images upon upload / create. Ideally I should also be able to override the restriction on a per-folder basis, for those special cases when a high-res image is actually called for.
>  
> Cheers,
> Paul
>  
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
> 
> 
> 




More information about the opencms-dev mailing list