0

Ok, so I have a content type with an image field that gets cropped using the Image Widget Crop. My users will potentially be uploading images directly from their DSLR cameras which are anywhere from 8 to 12 MB each. I want to reduce them down to 1080px x 720px where they'll be around 300kb and drastically cut down on server space.

I've been using hook_ENTITY_TYPE_presave() to reduce the image size however I just discovered this is messing up my cropping. The Image Widget Crop is cropping the original full sized image but after the image size reduction in the presave hook the cropping is no longer valid.

So, is there a way I can hook into the image file as it's being uploaded to then reduce the image size before it hits the Image Widget Crop?

Alternatively, is there a way to convert the crop settings after the image size reduction?

  • You could implement hook_entity_presave and then update the file there. – Jdrupal Jul 31 at 7:25
  • 1
    I just add a scale in the image style in order right after the crop so the crop still conforms to whatever parameters you have and scale sizes it down. (Width only). This works great with various crop aspect ratios as well as responsive images. – Danny Englander Jul 31 at 12:28
  • Thanks for the comments but they're not applicable. I'm already using the presave hook to reduce the image but that's what is screwing up the cropping because the cropping is applied to the original. Also, adding a scaling effect after the crop effect in the image style doesn't help. It would if I wasn't resizing the original to reduce server space. – mrlexington Jul 31 at 15:12
1

I answered my own question and I feel a little silly. I realized resizing images programmatically is unnecessary. In the UI you can set maximum dimensions for an image that will be uploaded. I knew that but I'd never read the text underneath that mentions the image gets resized if it violates the limits. That's all I needed, no need to for me to do it in code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.