Jump to content

Image upload maxSize changed -> recreate images?


bernhard
 Share

Recommended Posts

I've setup a gallery field on one site. I forgot to set a maxSize for those images, so the gallery images have >5MB per Image, which is of course not ideal. I changed the settings now to a max of 1024x1024 px - but the images are still in max resolution on the server.

On the frontend gallery I changed the links to $img->maxSize(1024,1024) so the users will get the resized images, but I would like to save some space on the server and recreate (resize) the already uploaded images to fulfill this maxsize restriction.

Any ideas how this can be done easily?

Thanks :)

Link to comment
Share on other sites

Hi,

It might suffice just to apply some extra compression to those original image files on the server. On my Mac I always use the desktop version of ImageOptim. Jpg files can be very small with using its algorithms. Sure, this will introduce more jpeg artifacts but if you do not crush them too much, then generating your final output can still be good quality, you just need to experiment a little bit what compression ratio works best.

I normally batch-recompress image files on my local copy of the site and overwrite the ones on the server in one go. I often cheat like that. Pssst, don't tell @horst! :) 

  • Like 1
  • Sad 1
Link to comment
Share on other sites

8 hours ago, bernhard said:

I would like to save some space on the server and recreate (resize) the already uploaded images

I have used the script below to do that, but must emphasise that this is destructive. The script does copy the original image to a "for_deletion" folder in the site root, but in case of disaster it would be a big PITA to go through and put the original images back to their relevant pages. So use at your own risk!

// You could run this script manually, or put it on LazyCron if you have a lot of images

// Maximum image dimensions
$max_width = 1600;
$max_height = 1600;

// Images to process per operation
$limit = 5;

// Create array of large images
$large_images = [];
$i = 1;
foreach($fields->find("type=FieldtypeImage") as $field) {
    if(!count($field->getFieldgroups())) continue;
    foreach($pages->find("{$field->name}.count>0") as $p) {
        foreach($p->getUnformatted($field->name) as $image) {
            if($i > $limit) break;
            if($image->width > $max_width || $image->height > $max_height) {
                $large_images[] = [
                    'basename' => $image->basename,
                    'filename' => $image->filename,
                    'page' => $p->url,
                ];
                $i++;
            }
        }
    }
}

// Iterate over images
foreach($large_images as $large_image) {
    // Make backup copy of image (create the "for_deletion" folder first)
    $destination = $config->paths->root . 'for_deletion/' . $large_image['basename'];
    if(!file_exists($destination)) {
        $success = copy($large_image['filename'], $destination);
        // Don't carry on to the resizing stage if the copy wasn't successful
        if(!$success) continue;
        // Log file copy
        $log->save('resized_images', "Copied original image {$large_image['basename']} from {$large_image['page']} to /for_deletion/");
    }

    /* @var ImageSizer $sizer */
    $sizer = new ImageSizer($large_image['filename'], ['cropping' => false]);
    $sizer->resize($max_width, $max_height);
    // Log file resize
    $log->save('resized_images', "Resized image {$large_image['basename']} on page {$large_image['page']} to max width $max_width, max height $max_height");
}

If you are using image field types besides the core FieldtypeImage you could modify the identification of image fields to something like this.

Edited by Robin S
Log original image page also.
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...