Prevent crazy huge images

Hello, Content managers may upload massive photos. Is there a built in way to set say a global max width and/or height for images. Ideally the uploaded image would be resized to the limit which is set. I know there’s max/min rules but I want to allow them to upload what they have and if its too massive have it resized down to something reasonable. Thing Imsanity for WP. This would prevent huge files from taken up all my expensive SSD host space. Thanks.

There is a plugin that does that:

And another gist file that does the same: https://gist.github.com/samnabi/f4ecb351f2ec2bd596e5

2 Likes

Perfect. Thanks for the quick reply.

Is there a limit in the files size which prevents the upload.hook from being executed?

I have insert @samnabi code in my config file. But if the file seems to be to big, the file will not be resized.

I’ve tested it with a image1 (3000x4498, 3.2MB) which works great. But if the Image is 5304x7952 and around 6.6MB the file doesn’t get resized.

Or is this related to my php settings?
They are:

  • memory_limit: 128M
  • upload_max_filesize: 32M

jonathanhess, I’ve experienced this too. I would like to know as well.

another option - this wont stop super huge images being uploaded by content editors, but it will resize the image on the fly when it gets served in the browser.

http://adaptive-images.com

@jonathanhess But the huge images do get uploaded?

Yes. The files are in the content folder. But at first the panel gets a bit buggy… the file is not shown in the file list and all the content disappears. And when I click the edit file button I see that there is an image, but it seems to be empty. And if i click the add file button the page gets reloaded and everything works again: The file is showing up in the file list and it can be edited and sorted or whatever.

Btw: I’m on Kirby 2.5.2

Are you using imagemagick or the standard gd library? Instead of in a hook, try to resize such an image in a template, maybe you can trigger an error message that way.

I’m using the standard gd library, at least I didn’t change anything with image processing.

I’ll try to resize it in my template… and let you know what happens.

Edit: It’s a memory limit problem. I got the error:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes)
If I set the memory limit to for example 512M it works fine.

But I think I also have to train my client not to upload such big images :wink:

Well, yes, that might be a good idea. But at least we now know why it didn’t work. Hooks fail silently, so testing your hook code in a template is always a good idea.

Good luck with that. I’ve been trying to train clients for the last 18 years…

1 Like