Skip to content

Commit

Permalink
fixup! [Waste] Limit image size on Echo backed Bulky Collection reports.
Browse files Browse the repository at this point in the history
Apply increasing shrink percentage to original rather than repeated shrinking.
  • Loading branch information
neprune committed May 7, 2024
1 parent c1c1770 commit 49b1501
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions perllib/FixMyStreet/App/Model/PhotoSet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,36 @@ sub redact_image {
return $new_set;
}

# Repeatedly shrink any images over the given size to the given percentage
# of their original size until they are small enough.
# Shrinks any images over the given size until they are small enough.
# First tries shrinking to the given percentage of the original size.
# If this isn't small enough, next tries shrinking the original to the given percentage squared,
# and so on.
# E.g. for 90% it would next try 81%, then 72% etc.
# Returns the new photoset and a bool indicating if any images were shrunk.
sub shrink_all_to_size {
my ($self, $size_bytes, $resize_percent) = @_;

Check warning on line 426 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L426

Added line #L426 was not covered by tests

my $shrunk = 0;
my @images = $self->all_ids;
foreach my $i (0.. $#images) {
my $blob = $self->get_raw_image($i)->{data};
while (length $blob > $size_bytes) {
$blob = FixMyStreet::ImageMagick->new(blob => $blob)
->shrink_to_percentage($resize_percent)
->as_blob;
$images[$i] = $blob;
$shrunk = 1;
my $original_blob = $self->get_raw_image($i)->{data};

Check warning on line 431 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L428-L431

Added lines #L428 - L431 were not covered by tests

if (length $original_blob <= $size_bytes) {
next;

Check warning on line 434 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L434

Added line #L434 was not covered by tests
}

$shrunk = 1;

Check warning on line 437 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L437

Added line #L437 was not covered by tests

my $percent = $resize_percent;
my $shrunk_blob;
do {
$shrunk_blob = FixMyStreet::ImageMagick->new(blob => $original_blob)

Check warning on line 442 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L439-L442

Added lines #L439 - L442 were not covered by tests
->shrink_to_percentage($percent)
->as_blob;
$percent = $percent * $resize_percent;

Check warning on line 445 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L445

Added line #L445 was not covered by tests
} while (length $shrunk_blob > $size_bytes);

$images[$i] = $shrunk_blob;

Check warning on line 448 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L448

Added line #L448 was not covered by tests
}

my $new_set = (ref $self)->new({

Check warning on line 451 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L451

Added line #L451 was not covered by tests
Expand Down

0 comments on commit 49b1501

Please sign in to comment.