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

Paul-Inge Flakstad flakstad at npolar.no
Tue Oct 1 12:12:22 CEST 2013


Hi Henning,

Thank you, this makes perfect sense. :)

(Being familiar with both the scaler and the event listeners, I should have thought of using a listener - it's quite obvious.)

But, anyway, I seem to recall reading about some native functionality in OpenCms which lets you set max image size. Not sure if my memory can be trusted though. 

Can you (or anyone else) tell me anything about this?

Cheers,
Paul

-----Original Message-----
From: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Henning Treu
Sent: 1. oktober 2013 08:59
To: The OpenCms mailing list
Subject: Re: [opencms-dev] Downscale large images automatically upon upload / create image resource

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
> 
> 
> 

_______________________________________________
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