Quiq/Image.pm

filter: Statementstmt 38.7% 38 / 98 Branchbran 7.8% 3 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.9% 10 / 13 Podpod 100% 5 / 5 totaltotal 33.1% 56 / 169 CC 25 SCAR 42.6
CC
25
Cov
28%
findImages
182.0
standardName
30.0
type
10.5
CRAP 70.6
« Quiq/Cascm.pm ↑ Summary Quiq/DestinationTree.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Image - Operationen im Zusammenhang mit Bildern/Bilddateien
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Image;
18 44 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 44 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 44 our $VERSION = '1.240';
25  
26 44 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 44 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 44 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 44 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # -----------------------------------------------------------------------------
32  
33 =head1 METHODS
34  
35 =head2 Klassenmethoden
36  
37 =head3 aspectRatio() - Seitenverhältnis eines Bildes
38  
39 =head4 Synopsis
40  
41 $aspectRatio = $this->aspectRatio($width,$height);
42  
43 =head4 Description
44  
45 Liefere das Seitenverhältnis eines Bildes, gegeben dessen
46 Breite und Höhe. Mögliche Rückgabewerte: '16:9', '4:3' oder (bei
47 anderen Seitenverhltnissen) der Quotient $width/$height.
48  
49 =cut
50  
51 # -----------------------------------------------------------------------------
52  
53 1 ▶sub aspectRatio {
Subroutine aspectRatio: called CC=3 SCAR=15.1
Pod documented
54 1 my ($this,$width,$height) = @_;
55  
56 1 ▶ if ($width/16*9 == $height) {
Branch: 2 branches
BranchTrueFalseTotal
if ($width / 16 * 9 == $height) { } 1 0 1
elsif ($width / 4 * 3 == $height) { } 0 0 0
57 1 return '16:9';
58 }
59 elsif ($width/4*3 == $height) {
60 0 return '4:3';
61 }
62  
63 0 return $width/$height;
64 }
65  
66 # -----------------------------------------------------------------------------
67  
68 =head3 edit() - Editiere Bild mit Bildbearbeitungsprogramm
69  
70 =head4 Synopsis
71  
72 $class->edit($file,@opt);
73  
74 =head4 Arguments
75  
76 =over 4
77  
78 =item $file
79  
80 Pfad der Bilddatei.
81  
82 =back
83  
84 =head4 Options
85  
86 =over 4
87  
88 =item -backupDir => (Default: undef)
89  
90 Sichere die unbearbeitete Bilddatei nach $dir.
91  
92 =item -program => $program (Default: 'gimp')
93  
94 Nutze Programm $program zum Editieren des Bildes.
95  
96 =back
97  
98 =head4 Description
99  
100 Editiere Bilddatei $file mit Programm $program. Ist ein Backupverzeichnis
101 $backupDir gegeben, wird die unbearbeitete Datei unter einer laufenden
102 Nummer plus Extension dorthin gesichert, aber nur, wenn die Bilddatei
103 tatsächlich geändert wurde.
104  
105 =cut
106  
107 # -----------------------------------------------------------------------------
108  
109 0 ▶sub edit {
Subroutine edit: not called CC=2 SCAR=17.9
Pod documented
110 0 my ($class,$file) = splice @_,0,2;
111 # @_: @opt
112
113 # Optionen
114  
115 0 my $backupDir = undef;
116 0 my $program = 'gimp';
117  
118 0 Quiq::Option->extract(-mode=>'sloppy',\@_,
119 -backupDir => \$backupDir,
120 -program => \$program,
121 );
122  
123 # Operation ausführen
124  
125 0 my $p = Quiq::Path->new;
126  
127 0 my $backupFile;
128 0 ▶ if ($backupDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backupDir) 0 0 0
129 0 my $ext = $p->extension($file);
130 0 my $num = $p->nextNumber($backupDir,5);
131 0 $backupFile = sprintf '%s/%s.%s',$backupDir,$num,$ext;
132 0 $p->copy($file,$backupFile);
133 }
134 0 Quiq::Shell->exec("$program $file");
135  
136 # MEMO: Geht nicht, wenn GIMP bereits offen ist
137 # if ($backupFile && !$p->compare($file,$backupFile)) {
138 # $p->delete($backupFile);
139 # }
140  
141 0 return;
142 }
143  
144 # -----------------------------------------------------------------------------
145  
146 =head3 findImages() - Suche Bild-Dateien
147  
148 =head4 Synopsis
149  
150 @files|$fileA = $class->findImages(@filesAndDirs);
151 @images|$imageA = $class->findImages(@filesAndDirs,-objects=>1);
152  
153 =head4 Arguments
154  
155 =over 4
156  
157 =item @filesAndDirs
158  
159 Liste von Bilddateien und Verzeichnissen mit Bilddateien.
160  
161 =back
162  
163 =head4 Options
164  
165 =over 4
166  
167 =item -object => $class (Default: undef)
168  
169 Liefere Objekte vom Typ $class statt Dateinamen.
170  
171 =item -sort => 'mtime'|'name' (Default: undef)
172  
173 Sortiere die Bilder primär nach Zeit (und sekundär nach Name)
174 oder nach Name. Per Default werden die Bilder unsortiert geliefert.
175  
176 =back
177  
178 =head4 Description
179  
180 Liefere die Liste aller Bild-Dateien, die in @filesAndDirs
181 vorkommen. Verzeichnisse werden rekursiv nach Bild-Dateien durchsucht.
182 Als Bild-Dateien werden alle Dateien angesehen, die eine
183 Bild-Extension (.jpg, .png, .gif) besitzen. Bei Dateien ohne
184 Extension wird mittels Quiq::Image->type() geprüft, ob es sich
185 um eine Bild-Datei handelt.
186  
187 =cut
188  
189 # -----------------------------------------------------------------------------
190  
191 0 ▶sub findImages {
Subroutine findImages: not called CC=13 SCAR=52.0
Pod documented
192 0 my $class = shift;
193 # @_: @filesAndDirs
194
195 # Optionen
196  
197 0 my $encoding = 'utf-8';
198 0 my $object = 0;
199 0 my $sort = '';
200  
201 0 Quiq::Option->extract(-mode=>'sloppy',\@_,
202 -encoding => \$encoding,
203 -object => \$object,
204 -sort => \$sort,
205 );
206  
207 0 my @arr;
208 0 for my $path (@_) {
209 0 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
210 0 my @tmp;
211 0 for my $file (Quiq::Path->find($path,
212 -type => 'f',
213 -decode => $encoding,
214 )) {
215 0 my $ext = Quiq::Path->extension($file);
216 0 ▶ if ($ext) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext) { } 0 0 0
elsif ($class->type($file, '-sloppy', 1)) { } 0 0 0
217 0 ▶ if ($ext =~ /^(jpe?g|gif|png)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext =~ /^(jpe?g|gif|png)$/i) 0 0 0
218 # Bild an Endung erkannt
219 0 push @tmp,$file;
220 }
221 }
222 elsif ($class->type($file,-sloppy=>1)) {
223 # Bild an Magic-Bytes erkannt
224 0 push @tmp,$file;
225 }
226 }
227  
228 # Verzeichnis-Dateien sortieren
229
230 0 ▶ if ($sort eq 'mtime') {
Branch: 2 branches
BranchTrueFalseTotal
if ($sort eq 'mtime') { } 0 0 0
elsif ($sort eq 'name') { } 0 0 0
231 0 ▶ @tmp = sort {(stat $a)[9]<=>(stat $b)[9] || $a cmp $b} @tmp;
Condition0%0/3
(stat $a)[9] <=> (stat $b)[9] || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
(stat $a)[9] <=> (stat $b)[9] || $a cmp $b
(stat $a)[9] <=> (stat $b)[9] $a cmp $b
Truth table0%0/3
A || B
A= (stat $a)[9] <=> (stat $b)[9]B= $a cmp $b
ABresult
1X1
011
000
232 }
233 elsif ($sort eq 'name') {
234 0 @tmp = sort @tmp;
235 }
236  
237 # Verzeichnis-Dateien an Gesamt-Liste anhängen
238 0 push @arr,@tmp;
239 }
240 else {
241 0 push @arr,$path;
242 }
243 }
244  
245 0 ▶ if ($object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 0 0 0
246 0 ▶ for (my $i = 0; $i < @arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arr) 0 0 0
247 0 $arr[$i] = $object->new($arr[$i]);
248 }
249 }
250
251 0 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
252 }
253  
254 # -----------------------------------------------------------------------------
255  
256 =head3 standardName() - Standard-Name eines Bildes
257  
258 =head4 Synopsis
259  
260 $standardName = $class->standardName($n,$width,$height,$extension,@opt);
261  
262 =head4 Options
263  
264 =over 4
265  
266 =item -name => $name
267  
268 Ergänzender Text zum Bild.
269  
270 =back
271  
272 =head4 Description
273  
274 Erzeuge einen Standard-Bild-Namen und liefere diesen zurück.
275 Ein Standard-Bild-Name hat den Aufbau:
276  
277 NNNNNN-WIDTHxHEIGHT[-NAME].EXT
278  
279 Hierbei ist:
280  
281 =over 4
282  
283 =item NNNNNN
284  
285 Die Bildnummer $i. Diese wird mit führenden Nullen auf sechs
286 Stellen gebracht.
287  
288 =item WIDTH
289  
290 Die Breite des Bildes.
291  
292 =item HEIGHT
293  
294 Die Höhe des Bildes.
295  
296 =item NAME
297  
298 Ein ergänzender Text zum Bild. Dieser ist optional. Leerzeichen
299 werden durch Bindestriche (-) ersetzt.
300  
301 =item EXT
302  
303 Die Datei-Endung, die sich aus dem Typ des Bildes ableitet,
304 z.B. 'jpg', 'png', 'gif' usw.
305  
306 =back
307  
308 =cut
309  
310 # -----------------------------------------------------------------------------
311  
312 0 ▶sub standardName {
Subroutine standardName: not called CC=5 SCAR=34.0
Pod documented
313 0 my $class = shift;
314 0 my $n = shift;
315 0 my $width = shift;
316 0 my $height = shift;
317 0 my $extension = shift;
318
319 # Optionen
320  
321 0 my $name = undef;
322  
323 0 Quiq::Option->extract(\@_,
324 -name=>\$name,
325 );
326
327 0 ▶ if ($n < 1 || $n > 999999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1 or $n > 999999) 0 0 0
Condition0%0/3
$n < 1 or $n > 999999
l!l&&r!l&&!r
000
MC/DC0%0/2
$n < 1 or $n > 999999
$n < 1 $n > 999999
Truth table0%0/3
A or B
A= $n < 1B= $n > 999999
ABresult
1X1
011
000
328 0 $class->throw;
329 }
330  
331 0 my $str = sprintf '%06d-%dx%d',$n,$width,$height;
332 0 ▶ if (defined($name) && $name ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $name ne "") 0 0 0
Condition0%0/3
defined $name and $name ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table0%0/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
333 0 $name =~ s/\W+/-/g;
334 0 $name =~ s/^-+|-+$//g;
335 0 $str .= "-$name";
336 }
337 0 $str .= ".$extension";
338
339 0 return $str;
340 }
341  
342 # -----------------------------------------------------------------------------
343  
344 =head3 type() - Typ einer Bilddatei
345  
346 =head4 Synopsis
347  
348 $type = $class->type($file,@opt);
349  
350 =head4 Options
351  
352 =over 4
353  
354 =item -enum => $i (Default: 0)
355  
356 Die Typbezeichnung, die geliefert wird:
357  
358 =over 4
359  
360 =item Wert: 0 oder nicht angegeben
361  
362 'jpg', 'png', 'gif'
363  
364 =item Wert: 1
365  
366 'jpeg', 'png', 'gif'
367  
368 =back
369  
370 =item -sloppy => $bool (Default: 0)
371  
372 Wirf keine Exception, wenn der Bild-Typ nicht erkannt wird, sondern
373 liefere einen Leerstring ('').
374  
375 =back
376  
377 =head4 Description
378  
379 Ermittele den Typ der Bilddatei $file anhand seiner Magic-Bytes
380 und liefere diesen zurück. Drei Bildtypen werden erkannt:
381  
382 =over 2
383  
384 =item *
385  
386 JPEG
387  
388 =item *
389  
390 PNG
391  
392 =item *
393  
394 GIF
395  
396 =back
397  
398 Wird der Bildtyp nicht erkannt, wirft die Methode eine Exception,
399 sofern nicht die Option -sloppy gesetzt ist.
400  
401 Anstelle eines Dateinamens kann auch eine Skalarreferenz
402 (in-memory Bild) übergeben werden.
403  
404 =cut
405  
406 # -----------------------------------------------------------------------------
407  
408 1 ▶sub type {
Subroutine type: called CC=6 SCAR=23.5
Pod documented
409 1 my $class = shift;
410 1 my $file = shift;
411 # @_: @opt
412  
413 # Optionen
414  
415 1 my $enum = 0;
416 1 my $sloppy = 0;
417  
418 1 Quiq::Option->extract(\@_,
419 -enum => \$enum,
420 -sloppy => \$sloppy,
421 );
422  
423 # Operation ausführen
424
425 1 my $fh = Quiq::FileHandle->new('<',$file);
426 1 my $data = $fh->read(8);
427 1 $fh->close;
428  
429 1 ▶ if ($data =~ /^\xff\xd8\xff/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($data =~ /^\xff\xd8\xff/) { } 1 0 1
elsif ($data =~ /^\x89PNG\r\n\x1a\n/) { } 0 0 0
elsif ($data =~ /^(GIF89a|GIF87a)/) { } 0 0 0
elsif (not $sloppy) { } 0 0 0
430 1 ▶ return $enum? 'jpeg': 'jpg';
Branch: 1 branch
BranchTrueFalseTotal
$enum ? : 0 1 1
431 }
432 elsif ($data =~ /^\x89PNG\r\n\x1a\n/) {
433 0 return 'png';
434 }
435 elsif ($data =~ /^(GIF89a|GIF87a)/) {
436 0 return 'gif';
437 }
438 elsif (!$sloppy) {
439 0 $class->throw(
440 'IMG-00001: Unknown image type',
441 File => $file,
442 Data => $data,
443 );
444 }
445  
446 0 return '';
447 }
448  
449 # -----------------------------------------------------------------------------
450  
451 =head1 VERSION
452  
453 1.240
454  
455 =head1 AUTHOR
456  
457 Frank Seitz, L<http://fseitz.de/>
458  
459 =head1 COPYRIGHT
460  
461 Copyright (C) 2026 Frank Seitz
462  
463 =head1 LICENSE
464  
465 This code is free software; you can redistribute it and/or modify
466 it under the same terms as Perl itself.
467  
468 =cut
469  
470 # -----------------------------------------------------------------------------
471  
472 44 1;
473  
474 # eof
« Quiq/Cascm.pm ↑ Summary Quiq/DestinationTree.pm »