| 1 |
use strict; |
| 2 |
|
| 3 |
use File::Path (); |
| 4 |
use File::Spec::Functions; |
| 5 |
use FindBin (); |
| 6 |
use Test::More; |
| 7 |
require Test::NoWarnings; |
| 8 |
|
| 9 |
use Image::Scale; |
| 10 |
|
| 11 |
my $gif_version = Image::Scale->gif_version(); |
| 12 |
my $png_version = Image::Scale->png_version(); |
| 13 |
|
| 14 |
if ($gif_version) { |
| •15 |
plan tests => 3; |
| 16 |
} |
| 17 |
else { |
| 18 |
plan skip_all => 'Image::Scale not built with giflib support'; |
| 19 |
} |
| 20 |
|
| 21 |
my $tmpdir = catdir( $FindBin::Bin, 'tmp' ); |
| 22 |
if ( -d $tmpdir ) { |
| 23 |
File::Path::rmtree($tmpdir); |
| 24 |
} |
| 25 |
mkdir $tmpdir; |
| 26 |
|
| 27 |
# corrupt file |
| 28 |
{ |
| 29 |
no strict 'subs'; |
| 30 |
no warnings; |
| 31 |
|
| 32 |
Test::NoWarnings::clear_warnings(); |
| 33 |
|
| 34 |
my $im = Image::Scale->new( _f("corrupt.gif") ); |
| 35 |
|
| 36 |
# Hide stderr |
| 37 |
open OLD_STDERR, '>&', STDERR; |
| 38 |
close STDERR; |
| 39 |
|
| 40 |
my $ok = $im->resize_gd_fixed_point( { width => 50 } ); |
| 41 |
|
| 42 |
# Restore stderr |
| 43 |
open STDERR, '>&', OLD_STDERR; |
| 44 |
|
| 45 |
is( $ok, 0, 'GIF corrupt failed resize ok' ); |
| 46 |
|
| 47 |
# Test that the correct warning was output |
| 48 |
like( (Test::NoWarnings::warnings())[0]->getMessage, qr/Image::Scale unable to read GIF file/i, 'GIF corrupt error output ok' ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
# Bug 17573, very thin gif could cause divide by 0 errors |
| 53 |
SKIP: |
| 54 |
{ |
| 55 |
skip "PNG support not built, skipping file comparison tests", 1 if !$png_version; |
| 56 |
|
| 57 |
my $outfile = _tmp("bug17573-thin_gd_fixed_point_w40.png"); |
| 58 |
my $im = Image::Scale->new( _f('bug17573-thin.gif') ); |
| 59 |
|
| 60 |
$im->resize_gd_fixed_point( { width => 40 } ); |
| 61 |
$im->save_png($outfile); |
| 62 |
|
| 63 |
is( _compare( _load($outfile), "bug17573-thin_gd_fixed_point_w40.png" ), 1, "GIF resize_gd_fixed_point from thin image ok" ); |
| 64 |
} |
| 65 |
|
| 66 |
diag("giflib version: $gif_version"); |
| 67 |
|
| 68 |
END { |
| 69 |
File::Path::rmtree($tmpdir); |
| 70 |
} |
| 71 |
|
| 72 |
sub _f { |
| 73 |
return catfile( $FindBin::Bin, 'images', 'gif', shift ); |
| 74 |
} |
| 75 |
|
| 76 |
sub _tmp { |
| 77 |
return catfile( $tmpdir, shift ); |
| 78 |
} |
| 79 |
|
| 80 |
sub _load { |
| 81 |
my $path = shift; |
| 82 |
|
| 83 |
open my $fh, '<', $path or die "Cannot open $path"; |
| 84 |
binmode $fh; |
| 85 |
my $data = do { local $/; <$fh> }; |
| 86 |
close $fh; |
| 87 |
|
| 88 |
return \$data; |
| 89 |
} |
| 90 |
|
| 91 |
sub _compare { |
| 92 |
my ( $test, $path ) = @_; |
| 93 |
|
| 94 |
my $ref = _load( catfile( $FindBin::Bin, 'ref', 'gif', $path ) ); |
| 95 |
|
| 96 |
return $$ref eq $$test; |
| 97 |
} |