[SOLVED] 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

@Martelinho Did you ever finish an implementation of such a mechanism?

No, unfortunately not. Way too far, way too dark, this would take me at least 1-2 weeks, can’t invest that for an uncertain success.

Did some digging on the Neos Media package, and this should be the POC

In Package.php there is a signal/slot that can create thumbnails, from Settings.yaml configuration. So far so good, we have the code for creating a thumbnail.

But then we need, the newly created version of the original resource, to replace the asset. There is a method for that

So, after “assetCreated”, create a thumbnail, get the PersistentResource, and call the “replace” method, with the new PersitentResource as second argument

@martoro @Martelinho @lorenzulrich I do not joke, but I now have a working package that automatically resizes the image on upload


@martoro @Martelinho @lorenzulrich

I created meteko/image-autoresizer - go give it a try

That’s great, you’re amazing!!!
At this moment I only can answer to that. My NEOS test project where I asked for that is over. Now I’m deep into a completely different topic. But if there will be some time I of course will try it out.
Again, amazing!

Damn… :stuck_out_tongue: You could have hired me at that time, and you would have had the feature before the day was over :wink:

Have fun with your new topic :slight_smile:

LOL Hiring you seems to be a good idea in general. :wink: But with this past project I tried to figure out if/how NEOS can fit the needs for future projects. So walking the way completely alone was part of the mission. :wink:

1 Like

It’s important to remember, that you are far from alone in this community :slight_smile:

1 Like