Bug 33857: Avoid increasing file size while uploading image

This patch prevents image size from increasing while uploading by
keeping the original image format

Test plan:

1. Download the attached image large-image.jpg
  --> It should be ~170kB and 1000×1500 pixels
2. Enable LocalCoverImages in System preferences
3. Find a record in the staff interface
4. Go to "Images" tab
5. Upload large-image.jpg image
   5.1. Click on "Upload"
   5.2. Click on "Drop files here or click to select a file"
   5.3. Choose picture (large-image.jpg)
   5.4. Click on "Process images"
6. Click on image for large image
7. Right-click on the image and save it
  --> It is ~930kB and 600x900 pixels

8. Apply the patch and flush browser cache (Crtl+F5) to update js files
9. Detele the previous uploaded image
10. Repeat step 3, 4, 5, 6, 7
   --> The image is ~92kB and 600x900 pixels
11. prove t/db_dependent/Koha/CoverImages.t
   --> All tests should be successful

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
This commit is contained in:
Hammat Wele 2025-11-13 13:23:55 +00:00 committed by Martin Renvoize
parent 699ba06eb7
commit 63a1924b27
No known key found for this signature in database
GPG Key ID: 2D86E0EAEC83711B
5 changed files with 62 additions and 5 deletions

View File

@ -63,9 +63,10 @@ sub new {
my $fullsize =
$class->_scale_image( $src_image, 600, 800 ); # MAX pixel dims are 600 X 800 for full-size image...
$params->{mimetype} = 'image/png';
$params->{imagefile} = $fullsize->png();
$params->{thumbnail} = $thumbnail->png();
$params->{mimetype} = $params->{mimetype} || 'image/png';
my ( $app, $format ) = split( '/', $params->{mimetype} );
$params->{imagefile} = $fullsize->$format();
$params->{thumbnail} = $thumbnail->$format();
}
return $class->SUPER::new($params);

View File

@ -227,9 +227,11 @@
[% IF itemnumber %]
<input type="hidden" id="itemnumber" name="itemnumber" value="[% itemnumber | html %]" />
<input type="hidden" name="filetype" value="image" />
<input type="hidden" name="mimetype" id="mimetype" value="" />
[% ELSIF biblionumber %]
<input type="hidden" id="biblionumber" name="biblionumber" value="[% biblionumber | html %]" />
<input type="hidden" name="filetype" value="image" />
<input type="hidden" name="mimetype" id="mimetype" value="" />
[% END %]
<fieldset id="upload_options" class="rows">

View File

@ -93,7 +93,9 @@ function prepUpLoad(event) {
$("#fileuploadstatus, #upload_options").show();
var fd = new FormData();
fd.append("file", file);
$("#mimetype").val("");
if (ParseFile(file)) {
$("#mimetype").val(file.type);
StartUpload(fd);
}
}

View File

@ -19,7 +19,7 @@
use Modern::Perl;
use Test::More tests => 14;
use Test::More tests => 23;
use Test::Exception;
use FindBin '$Bin';
@ -96,4 +96,54 @@ Koha::CoverImage->new(
is( $biblio->cover_images->count, 3, );
is( $item->cover_images->count, 2, );
$logo_filepath = "$Bin/../../../koha-tmpl/intranet-tmpl/prog/img/koha-logo.gif";
my $mimetype = 'image/gif';
$image = Koha::CoverImage->new(
{
biblionumber => $biblio->biblionumber,
src_image => GD::Image->new($logo_filepath),
mimetype => $mimetype
}
)->store;
$cover_image = $biblio->cover_images->last;
ok( $cover_image->imagefile, 'image is stored in imagefile' );
ok( $cover_image->thumbnail, 'thumbnail has been generated' );
is(
$cover_image->mimetype, $mimetype,
'The original mimetype has been kept'
);
$logo_filepath = "$Bin/../../../koha-tmpl/intranet-tmpl/prog/img/koha-logo.png";
$mimetype = 'image/png';
$image = Koha::CoverImage->new(
{
biblionumber => $biblio->biblionumber,
src_image => GD::Image->new($logo_filepath),
mimetype => $mimetype
}
)->store;
$cover_image = $biblio->cover_images->last;
ok( $cover_image->imagefile, 'image is stored in imagefile' );
ok( $cover_image->thumbnail, 'thumbnail has been generated' );
is(
$cover_image->mimetype, $mimetype,
'The original mimetype has been kept'
);
$logo_filepath = "$Bin/../../../koha-tmpl/opac-tmpl/lib/greybox/GreyBox_v5_5/static_files/night_valley.jpg";
$mimetype = 'image/jpeg';
$image = Koha::CoverImage->new(
{
biblionumber => $biblio->biblionumber,
src_image => GD::Image->new($logo_filepath),
mimetype => $mimetype
}
)->store;
$cover_image = $biblio->cover_images->last;
ok( $cover_image->imagefile, 'image is stored in imagefile' );
ok( $cover_image->thumbnail, 'thumbnail has been generated' );
is(
$cover_image->mimetype, $mimetype,
'The original mimetype has been kept'
);
$schema->storage->txn_rollback;

View File

@ -67,6 +67,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
my $filetype = $input->param('filetype');
my $biblionumber = $input->param('biblionumber');
my $itemnumber = $input->param('itemnumber');
my $mimetype = $input->param('mimetype');
my $replace = !C4::Context->preference("AllowMultipleCovers")
|| $input->param('replace');
my $op = $input->param('op') // q{};
@ -118,7 +119,8 @@ if ( $op eq 'cud-process' && $fileID ) {
{
biblionumber => $biblionumber,
itemnumber => $itemnumber,
src_image => $srcimage
src_image => $srcimage,
mimetype => $mimetype
}
)->store;
};