Handle image size on upload

Hi there, I searched for an option to automatically resize images on upload to a maximum extension (width/height). I couldn’t find a solution just some hints from many years ago where it seemed not to be possible.

Is there, with a current CMS version, the possibility to configure automatic resize of images on upload? Or if not, is there the possibility to at least set a size limit (width/height)? And if both do not exist, how could an automatic resize be made possible?

I never tried this but if i had to i would use a PSR 15 http-component in Neos 7 where the uploaded files are accessible as UploadedFile object. That way the scaling could be done before the image is converted to a persistentResource.

This could even be implemented as a Framework agnostic psr15 component that only has imagine as dependency. It may even be possible to find an existing component that does exactly that. Nevertheless i had no success during a short search for that.

Regards Martin

1 Like

How can garbage collection be handled if the persistentResource object is invalid in terms of validation rules?

Offcourse you would have to be careful with that approach. If you scale the image down and later validate for minimum width you will end up in a mess. However as i understood this was mererely requested as a way to prevent ultralarge images beeing uploaded directly from digital cameras.

It would be a bit harder to limit upload image sizes this only for a specific usecase like forms. I would try to use a custom type converter in that case. But how to integrate this into the concrete case is another topic and i never actually tried that.

Absolutely correct. This and to prevent users from wasting diskspace on websites with many pictures and no need for very high resolutions. I know the described functionality as a standard feature from other CMS so I thought I should not be the first to think about it.

Thanks for your answers. So there is no such functionality in NEOS. And the requirements to implement this seem to be far beyond my knowledge. Never mind. Thank you.

The Neos.Media package has a singnal called assetCreated - you might find tha tuseful. Be aware, that if you change the original resource, you might have to re-create thumbnails etc, if it’s already created at that point

Using the signal is also a good approach. Maybe i am a bit too enthusiastic about PSR15 Components at the moment.

I could find out that Neos\Media\Domain\Service\AssetService seems to send such a signal. Apart from the sad fact that I don’t know anything about the wohle media package there is one major point that seems not to suite the requirements.
As I understand it the original (too large) picture is created anyway and also exists later on in NEOS. I would have to do my scaling work after the creation of the unwanted image. But the requirements on an image upload would be:

  1. Create the image if it is not larger (width or height) than the configured maximum extension (e.g. 1920px).
  2. If it is larger, first scale it down to the maximum extension and then create it.

You would create a new scaled down resource, assign it to the asset and then delete the original (too large) resource.

Not sure wether finding and deleting all possibly already created thumbnails is really needed as we are only scaling down.

If you get it to work it would be cool to share your solution here.

Thanks for your info.
I would of course like to implement something but as already mentioned this is far beyond my knowledge. I hoped that if no configuration is possible there is at least a more simple way. For example I hoped for an event which is triggered while uploading and before saving and a function could be registered on that event whithin which the source image is available and can be manipulated before processing will be finished by NEOS. That would have been one place to do just one work, scaling. Reading an trying out that would have been many hours/days for a working solution. But there’s more to do and much more to know. This is out of my reach.

That is the signal assetCreated that was mentioned