File Coverage

blib/lib/App/CopyrightImage.pm
Criterion Covered Total %
statement 84 85 98.8
branch 30 38 78.9
condition 12 15 80.0
subroutine 11 11 100.0
pod 1 1 100.0
total 138 150 92.0


line stmt bran cond sub pod time code
1             package App::CopyrightImage;
2              
3 3     3   52164 use 5.006;
  3         19  
4 3     3   13 use warnings;
  3         5  
  3         56  
5 3     3   12 use strict;
  3         3  
  3         58  
6              
7 3     3   23 use Exporter qw(import);
  3         5  
  3         100  
8 3     3   13 use File::Basename;
  3         22  
  3         262  
9 3     3   1177 use File::Copy;
  3         10565  
  3         144  
10 3     3   1236 use File::Find::Rule;
  3         19392  
  3         18  
11 3     3   3958 use Image::ExifTool qw(:Public);
  3         125184  
  3         2179  
12              
13             our @EXPORT = qw(imgcopyright);
14              
15             our $VERSION = '1.01';
16              
17             sub imgcopyright {
18 10     10 1 251365 my (%data) = @_;
19              
20 10 100       56 die "need to supply the 'image' argument!\n" if ! $data{src};
21 9 100 100     51 if (! $data{name} && (! $data{check} && ! $data{remove})){
      100        
22 1         5 die "need to supply the 'name' argument\n";
23             }
24 8         323 $data{year} = (localtime(time))[5] + 1900;
25              
26 8 50 66     203 if ($data{dst} && -d $data{dst}){
    100          
27 0         0 $data{basename} = $data{dst};
28             }
29             elsif (-d $data{src})
30             {
31 3         18 $data{basename} = $data{src};
32             }
33             else {
34 5         212 $data{basename} = dirname $data{src};
35             }
36              
37 8 100       109 if (-d $data{src}){
38 3         3568 @{ $data{images} } = File::Find::Rule->file()
39             ->name('*.jpg', '*.jpeg')
40             ->maxdepth(1)
41 3         141 ->in($data{src});
42             }
43             else {
44 5         17 push @{ $data{images} }, $data{src};
  5         26  
45             }
46              
47 8 100       60 if ($data{check}){
48 3         14 return _check(\%data);
49             }
50             else {
51 5         41 _exif(\%data);
52             }
53             }
54             sub _exif {
55 5     5   12 my $data = shift;
56              
57             my $dst = $data->{dst}
58             ? $data->{dst}
59 5 100       21 : "$data->{basename}/ci";
60            
61 5 50       76 if (! -d $dst){
62 5 50       340 mkdir $dst
63             or die "can't create the destination image directory $dst!: $!";
64             }
65              
66 5         57 my $et = Image::ExifTool->new;
67 5         590 my %errors;
68              
69 5         9 for my $img (@{ $data->{images} }){
  5         17  
70              
71             # original
72            
73 7         94 $et->ExtractInfo($img);
74              
75 7 100       196640 if ($data->{remove}){
76 1         7 $et->SetNewValue('Copyright', '');
77 1         2446 $et->SetNewValue('Creator', '');
78 1         1153 $et->WriteInfo($img, "$img.tmp");
79 1         51210 move "$img.tmp", $img;
80 1         1251 next;
81             }
82              
83 6         22 my $cp = $et->GetValue('Copyright');
84 6         305 my $cr = $et->GetValue('Creator');
85              
86 6 100 66     236 if (! $data->{force} && ($cp || $cr)){
      66        
87 1 50       4 my $set = "Copyright is already set;" if $cp;
88 1 50       5 $set .= " Creator is already set;" if $cr;
89 1         3 $errors{$img} = $set;
90 1         3 next;
91             }
92            
93 5         47 $et->SetNewValue('Copyright', "Copyright (C) $data->{year} by $data->{name}");
94 5         180483 my $creator_string = $data->{name};
95 5 50       18 $creator_string .= " ($data->{email})" if $data->{email};
96              
97 5         18 $et->SetNewValue('Creator', $creator_string);
98              
99 5         28526 my $ci_img = (fileparse($img))[0];
100 5         19 $ci_img = "$dst/ci_$ci_img";
101            
102             # write out the new image
103              
104 5         27 $et->WriteInfo($img, $ci_img);
105              
106             # updated
107              
108 5         261367 $et->ExtractInfo($ci_img);
109              
110 5 50       122190 $errors{$img} = "failed to add Copyright; "
111             if ! $et->GetValue('Copyright');
112              
113 5 50       255 $errors{$img} .= "failed to add Creator"
114             if ! $et->GetValue('Creator');
115             }
116 5         194 return %errors;
117             }
118             sub _check {
119 3     3   6 my $data = shift;
120              
121 3         27 my $et = Image::ExifTool->new;
122              
123 3         33924 for (@{ $data->{images} }){
  3         11  
124 4         22 $et->ExtractInfo($_);
125 4         155093 my $cp = $et->GetValue('Copyright');
126 4         258 my $cr = $et->GetValue('Creator');
127              
128 4         179 my $err_str;
129 4 100       20 $err_str .= " missing Copyright; " if ! $cp;
130 4 100       15 $err_str .= " missing Creator; " if ! $cr;
131              
132 4 100       33 print "$_: $err_str\n" if $err_str;
133             }
134 3         43 return ();
135             }
136              
137             1;
138             __END__