1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
use Test::More;
use lib 't/lib';
use DBI;
use Carp::Assert;
use CGI::Uploader::Test; # provides setup() and read_file()
use strict;
BEGIN {
use_ok('CGI::Uploader');
use_ok('File::Path');
};
my $found_module = 0;
eval { require Image::Magick; };
$found_module = !$@;
if ($found_module) {
plan (qw/no_plan/)
}
else {
eval { require Graphics::Magick; };
$found_module = !$@;
if ($found_module) {
plan (qw/no_plan/)
}
else {
plan skip_all => "No graphics module found for image resizing. Install Graphics::Magick or Image::Magick: $@ ";
}
}
use CGI::Uploader::Transform::ImageMagick;
# This should work, even if we don't preload either one
delete $INC{'Image/Magick.pm'};
delete $INC{'Graphics/Magick.pm'};
my ($tmp_filename, $img) = CGI::Uploader::Transform::ImageMagick->gen_thumb( 't/20x16.png', [ w => 5 ]);
my ($w,$h) = $img->Get('width','height');
is($w,5,'as class method - correct height only width is supplied');
is($h,4,'as class method - correct height only width is supplied');
####
my ($DBH,$drv) = setup();
my %imgs = (
'img_1' => {
gen_files => {
# old API
img_1_thumb => {
transform_method => \&gen_thumb,
params => [{ w => 10 }],
},
# new API
new_api_thumb => gen_thumb({ w => 10}),
},
},
);
use CGI;
my $u = CGI::Uploader->new(
updir_path=>'t/uploads',
updir_url=>'http://localhost/test',
dbh => $DBH,
spec => \%imgs,
query => CGI->new(),
);
ok($u, 'Uploader object creation');
{
my ($tmp_filename,$img) = CGI::Uploader::Transform::ImageMagick->gen_thumb({
filename => 't/20x16.png',
w => 10,
});
my ($w,$h) = $img->Get('width','height');
is($h,8,'correct height only width is supplied (also testing new API)');
}
{
my ($tmp_filename,$img) = CGI::Uploader::Transform::ImageMagick->gen_thumb({
filename => 't/20x16.png',
h => 8,
});
my ($w,$h) = $img->Get('width','height');
is($w,10,'correct width only width is supplied (also testing new API');
}
eval {
my %entity_upload_extra = $u->store_upload(
file_field => 'img_1',
src_file => 't/20x16.png',
uploaded_mt => 'image/png',
file_name => '20x16.png',
);
};
is($@,'', 'store_upload() survives');
my $db_height =$DBH->selectrow_array(
"SELECT height
FROM uploads
WHERE upload_id = 2");
is($db_height, 8, "correct height calculation when thumb height omitted from spec ");
{
my $db_height =$DBH->selectrow_array(
"SELECT height
FROM uploads
WHERE upload_id = 3");
is($db_height, 8, "correct height calculation when thumb height omitted from spec (using new API) ");
}
|