Project

General

Profile

Actions

Support #1261

closed

Script to shrink file size of cover_images in Koha by lowering quality

Added by Lari Taskula 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
Start date:
07.06.2024
Due date:
% Done:

0%

Estimated time:
Sponsored-by:
Koha Bugzilla Bug#:

Description

This script managed to shrink size of cover_images to about third of what it originally was.

Requirements

"convert" command:

apt install imagemagick
#!/usr/bin/perl

use Modern::Perl;
use GD;
use Koha::CoverImages;

# ADJUST THESE TO YOUR LIKINGS
my $tmp_path = '/tmp';
my $rows=1000;
my $page=1;

while (my $cover_images = Koha::CoverImages->search({ mimetype => 'image/png' }, { rows => $rows, page => $page }) ) {
  foreach my $cover_image ($cover_images->as_list) {
    warn $cover_image->biblionumber;
    my $gd = GD::Image->new($cover_image->imagefile);
    my $filename = $cover_image->imagenumber . '.jpg';
    open my $fh, '>', "$tmp_path/$filename";
    print $fh $gd->jpeg();
    close($fh);
    system("convert $tmp_path/$filename -quality 50% $tmp_path/$filename");
    $gd = GD::Image->new("$tmp_path/$filename");
    $cover_image->set({ imagefile => $gd->png() })->store();
    system("rm $tmp_path/$filename");

    my $gd_thumb = GD::Image->new($cover_image->thumbnail);
    my $filename_thumb = $cover_image->imagenumber . '_thumb.jpg';
    open my $fh_thumb, '>', "$tmp_path/$filename_thumb";
    print $fh_thumb $gd_thumb->jpeg();
    close($fh_thumb);
    system("convert $tmp_path/$filename_thumb -quality 50% $tmp_path/$filename_thumb");
    $gd_thumb = GD::Image->new("$tmp_path/$filename_thumb");
    $cover_image->set({ thumbnail => $gd_thumb->png() })->store();
    system("rm $tmp_path/$filename_thumb");
  }
  $page++;
}

Related issues 1 (0 open1 closed)

Related to Koha Fund - Support #1262: Shrinking cover_images with a scriptClosedLari Taskula07.06.2024

Actions
Actions

Also available in: Atom PDF