Automatic Image Resizing

Hey there,

I was wondering, how can I automatically resizing (all) images, inserted into a webpage through the upload feature. What’s the best way to do it? I searched for it for a long time, but couldn’t find anything.
My problem is the following: Users should be able to upload original, full size photos from their cameras. But these photos should not be distributed from the website. These photos should all be resized automatically, without applying a filter to each photo manually with Twig or Markdown.

Thanks!

There is currently no ‘hook’ to do work on uploading an image via the admin. However you can set media defaults so that all images receive some processing.

To do that you can look at the system/config/media.yaml file and override this in your user/config/media.yaml file. Specifically the image filters section:

defaults:
  image:
    filters:
      default:
        - enableProgressive
        - [cropResize, 1024, 1024]

I’ve created the new file, with exactly this code. When I append a filter directly to the image, the resolution changes. Otherwise nothing happens.

ah yes… true, you can use ?cache to ensure it’s processed with no other filter.

So it’s seems like it’s not possible without adding a filter to each photo?

Currently no. This is because there is an optimization check so images are not processed at all if there are no filters manually added.

Any processing is going to take time and potentially degrade photo quality (multiple processing of jpeg images is a lossy process). So if you don’t tell Grav to do any processing, it wont.

I’m thinking about adding an option to make this configurable though. Is this something you would find useful?

Actually I had already added this!!!

system.yaml there’s an option:

images.cache_all which is set to false by default, just change this to true.

Thanks, this is working excellent!

So now, when i try to open a page on my webhoster with some images, this error occurs:
Whoops \ Exception \ ErrorException (E_ERROR)
Out of memory (allocated 62914560) (tried to allocate 2650801 bytes)

in the file htdocs/next/vendor/gregwar/image/Gregwar/Image/Adapter/GD.php

How do i solve that?

Well, really that’s a problem with some of your image being too large and PHP’s GD library is using too much memory to resize them. So two real options for you:

  1. Resize those really large images before uploading them to PHP
  2. Increase PHP memory via php.ini