Compress already imported images

Hello, I use medienbaecker-autoresize plugin to compress newly added images. But is there a way to compress already imported images?

Thanks in advance.

The plugin has a function autoresize() that you could call on existing files.

Is there any documentation how to use autoresize() on all existing files? Becau my approach crashes the site:

Kirby::plugin('medienbaecker/autoresize', [
    'options' => [
        'maxWidth' => 1800,
        'maxHeight' => 1920,
        'excludeTemplates' => [],
        'excludePages' => []
    ],
    'hooks' => [
        'my.custom.hook' => function ($file) {
            autoresize($file);
        },
    ]
]);


$files = kirby()->site()->index()->files();

foreach ($files as $file) {
    autoresize($file);
}

You should do this once in a template or so, don’t modify the plugin code.

Okay, so i leave plugin code untouched:

<?php

function autoresize($file) {
    $maxWidth = option('medienbaecker.autoresize.maxWidth');
    $maxHeight = option('medienbaecker.autoresize.maxHeight');
    $quality = option('medienbaecker.autoresize.quality');
    $excludeTemplates = option('medienbaecker.autoresize.excludeTemplates');
    $excludePages = option('medienbaecker.autoresize.excludePages');
    $excludedByTemplate = false;
    $excludedByPage = false;
    if($file->page()) {
        if(!empty($excludeTemplates)) $excludedByTemplate = in_array($file->page()->intendedTemplate(), $excludeTemplates);
        if(!empty($excludePages)) $excludedByPage = in_array($file->page()->uid(), $excludePages);
    }
    if($file->isResizable() && !$excludedByTemplate && !$excludedByPage) {
        if($file->width() > $maxWidth || $file->height() > $maxHeight){
            try {
                kirby()->thumb($file->root(), $file->root(), [
                    'width'   => $maxWidth,
                    'height'  => $maxHeight,
                    'quality' => $quality
                ]);
            }
            catch (Exception $e) {
                throw new Exception($e->getMessage());
            }
        }
    }
}

Kirby::plugin('medienbaecker/autoresize', [
    'options' => [
        'maxWidth' => 1920,
        'maxHeight' => 2000,
        'quality' => 100,
        'excludeTemplates' => [],
        'excludePages' => []
    ],
    'hooks' => [
        'file.create:after' => function ($file) {
            autoresize($file);
        },
        'file.replace:after' => function ($newFile, $oldFile) {
            autoresize($newFile);
        }
    ]
]);

Then edit template file with this code:

<?php

$images = page()->images();

foreach ($images as $image) {

autoresize($image);

}

?>

While compressing specific template images, I noticed that my website on the local MAMP server reloads repeatedly for approximately 8 minutes. This reloading occurs for all 40 template images until the compression process is completed.

Are there any factors I need to consider before implementing this on a live server? Could the website potentially crash or result in any damage, because there are thousands of images?

Maybe this approach is better to go with?
$images = page('my-specific-page')->images();

Well, you only need to run this once for all site images that haven’t been resized yet. So after this is done, you remove that code from the template again. If you have a lot of images, it might make sense to do this in batches (looks like your doing this per page, anyway, but then you have to include the code in each template, which doesn’t make sense).

Ideally, you would run such a script from the command line, where you wouldn’t run into script timeout issues.

1 Like

Are there any available resources that offer guidance on how to access non-resized images specifically? This is relevant because the plugin was integrated at a later stage in the development process.

I think your only option would be to go by file dimensions

1 Like