Quiq/File/Image.pm

filter: Statementstmt 81.0% 81 / 100 Branchbran 50.0% 6 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.2% 26 / 32 Podpod 100% 14 / 14 totaltotal 78.1% 129 / 165 CC 9 SCAR 6.9
CC
9
Cov
76%
analyzeFile
5.9
new
2.1
property
2.1
CRAP 2.0
« Quiq.pm ↑ Summary Quiq/Dbms.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::File::Image - Informationen über Bild-Datei
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::File::Image;
18 7 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 7 our $VERSION = '1.240';
25  
26 7 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use Quiq::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use Quiq::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use Image::ExifTool ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶use Image::Size ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # -----------------------------------------------------------------------------
35  
36 =head1 METHODS
37  
38 =head2 Konstruktor
39  
40 =head3 new() - Instantiiere Bild-Datei-Objekt
41  
42 =head4 Synopsis
43  
44 $img = $class->new($path);
45  
46 =head4 Description
47  
48 Instantiiere ein Bild-Datei-Objekt und liefere eine Referenz auf
49 dieses Objekt zurück.
50  
51 =cut
52  
53 # -----------------------------------------------------------------------------
54  
55 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
56 4 my ($class,$path) = @_;
57  
58 4 ▶ if (!-e Encode::encode('utf-8',$path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e &Encode::encode("utf-8", $path)) 0 4 4
59 0 $class->throw(
60 'IMAGE-00001: File not found',
61 Path => $path,
62 );
63 }
64  
65 # Objekt instantiieren
66  
67 4 return $class->SUPER::new(
68 # Eigenschaften
69 path => $path,
70 filename => undef,
71 basename => undef,
72 extension => undef,
73 width => undef,
74 height => undef,
75 created => undef,
76 type => undef,
77 # Properties
78 propertyH => Quiq::Hash->new->unlockKeys,
79 );
80 }
81  
82 # -----------------------------------------------------------------------------
83  
84 =head2 Eigenschaften
85  
86 =head3 path() - Datei-Pfad
87  
88 =head4 Synopsis
89  
90 $path = $img->path;
91  
92 =head4 Description
93  
94 Liefere den Dateinamen einschl. Pfad der Bild-Datei.
95  
96 =head3 filename() - Datei-Name ohne Verzeichnisanteil
97  
98 =head4 Synopsis
99  
100 $filename = $img->filename;
101  
102 =head4 Description
103  
104 Liefere den Dateinamen ohne Verzeichnisanteil, aber mit Extension,
105 z.B. '000456.jpg'.
106  
107 =cut
108  
109 # -----------------------------------------------------------------------------
110  
111 1 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
112 1 my $self = shift;
113  
114 1 ▶ return $self->memoize('filename',sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 my ($self,$key) = @_;
116 1 $self->path =~ m|/([^/]+)$|;
117 1 return $1;
118 1 });
119 }
120  
121 # -----------------------------------------------------------------------------
122  
123 =head3 basename() - Datei-Name ohne Verzeichnis und Extension
124  
125 =head4 Synopsis
126  
127 $basename = $img->basename;
128  
129 =head4 Description
130  
131 Liefere den Dateinamen ohne Verzeichnis und Extension, z.B. '000456'.
132  
133 =cut
134  
135 # -----------------------------------------------------------------------------
136  
137 0 ▶sub basename {
Subroutine basename: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self = shift;
139  
140 0 ▶ return $self->memoize('basename',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 my ($self,$key) = @_;
142 0 $self->filename =~ m|([^.]+)|;
143 0 return $1;
144 0 });
145 }
146  
147 # -----------------------------------------------------------------------------
148  
149 =head3 extension() - Datei-Extension
150  
151 =head4 Synopsis
152  
153 $ext = $img->extension;
154  
155 =head4 Description
156  
157 Liefere die Extentsion der Datei, z.B. 'png'.
158  
159 =cut
160  
161 # -----------------------------------------------------------------------------
162  
163 1 ▶sub extension {
Subroutine extension: called CC=1 SCAR=0.0
Pod documented
164 1 my $self = shift;
165  
166 1 ▶ return $self->memoize('extension',sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 1 my ($self,$key) = @_;
168 1 return Quiq::Path->extension($self->path);
169 1 });
170 }
171  
172 # -----------------------------------------------------------------------------
173  
174 =head3 mtime() - Letzte Änderung
175  
176 =head4 Synopsis
177  
178 $mtime = $img->mtime;
179  
180 =head4 Description
181  
182 Liefere den Zeitpunkt (Unix Epoch), an dem die Bilddatei
183 das letzte Mal geändert wurde.
184  
185 =cut
186  
187 # -----------------------------------------------------------------------------
188  
189 0 ▶sub mtime {
Subroutine mtime: not called CC=1 SCAR=6.9
Pod documented
190 0 return (stat(shift->path))[9];
191 }
192  
193 # -----------------------------------------------------------------------------
194  
195 =head3 type() - Bild-Typ
196  
197 =head4 Synopsis
198  
199 $type = $img->type;
200  
201 =head4 Description
202  
203 Liefere den Datei-Typ des Bildes, z.B. 'jpg'.
204  
205 =cut
206  
207 # -----------------------------------------------------------------------------
208  
209 1 ▶sub type {
Subroutine type: called CC=1 SCAR=0.1
Pod documented
210 1 my $self = shift;
211  
212 1 ▶ return $self->memoize('type',sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
213 1 my ($self,$key) = @_;
214  
215 1 my $type = Quiq::Image->type($self->path);
216 1 ▶ if ($type eq 'jpeg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "jpeg") 0 1 1
217 0 $type = 'jpg';
218 }
219  
220 1 return $type;
221 1 });
222 }
223  
224 # -----------------------------------------------------------------------------
225  
226 =head3 width() - Breite
227  
228 =head4 Synopsis
229  
230 $width = $img->width;
231  
232 =head4 Description
233  
234 Liefere die Breite des Bildes in Pixeln, z.B. 1920.
235  
236 =cut
237  
238 # -----------------------------------------------------------------------------
239  
240 4 ▶sub width {
Subroutine width: called CC=1 SCAR=0.0
Pod documented
241 4 my $self = shift;
242  
243 2 ▶ return $self->memoize('width',sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 2 my ($self,$key) = @_;
245 2 $self->analyzeFile;
246 2 return $self->get($key);
247 4 });
248 }
249  
250 # -----------------------------------------------------------------------------
251  
252 =head3 height() - Höhe
253  
254 =head4 Synopsis
255  
256 $height = $img->height;
257  
258 =head4 Description
259  
260 Liefere die Höhe des Bildes in Pixeln, z.B. 1080.
261  
262 =cut
263  
264 # -----------------------------------------------------------------------------
265  
266 4 ▶sub height {
Subroutine height: called CC=1 SCAR=2.0
Pod documented
267 4 my $self = shift;
268  
269 0 ▶ return $self->memoize('height',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
270 0 my ($self,$key) = @_;
271 0 $self->analyzeFile;
272 0 return $self->get($key);
273 4 });
274 }
275  
276 # -----------------------------------------------------------------------------
277  
278 =head3 created() - Aufnahmezeitpunkt
279  
280 =head4 Synopsis
281  
282 $created = $img->created;
283  
284 =head4 Description
285  
286 Liefere den Aufnahmezeitpunkt des Bildes, z.B. '2024-11-11 22:22:06'.
287  
288 =cut
289  
290 # -----------------------------------------------------------------------------
291  
292 0 ▶sub created {
Subroutine created: not called CC=1 SCAR=6.9
Pod documented
293 0 my $self = shift;
294  
295 0 ▶ return $self->memoize('created',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
296 0 my ($self,$key) = @_;
297 0 $self->analyzeFile;
298 0 return $self->get($key);
299 0 });
300 }
301  
302 # -----------------------------------------------------------------------------
303  
304 =head3 size() - Breite und Höhe
305  
306 =head4 Synopsis
307  
308 $size = $img->size;
309 ($width,$height) = $img->size;
310  
311 =head4 Description
312  
313 Liefere die Breite und Höhe des Bildes in Pixeln. Im Skalarkontext
314 werden die Breite und Höhe in einem String zusammengefasst geliefert:
315  
316 'WIDTHxHEIGHT'
317  
318 =head4 Examples
319  
320 List-Kontext:
321  
322 $img->size;
323 =>
324 (1920,1080)
325  
326 Skalar-Kontext:
327  
328 $img->size;
329 =>
330 '1920x1080'
331  
332 =cut
333  
334 # -----------------------------------------------------------------------------
335  
336 1 ▶sub size {
Subroutine size: called CC=2 SCAR=7.0
Pod documented
337 1 my $self = shift;
338 1 my $width = $self->width;
339 1 my $height = $self->height;
340 1 ▶ return wantarray? ($width,$height): $width.'x'.$height;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
341 }
342  
343 # -----------------------------------------------------------------------------
344  
345 =head3 scaleFactor() - Skalierungsfaktor für andere Breite, Höhe
346  
347 =head4 Synopsis
348  
349 $scale = $img->scaleFactor($width,$height);
350  
351 =head4 Description
352  
353 Liefere den Skalierungsfaktor, wenn das Bild auf die Breite $width
354 und die Höhe $height skaliert werden soll. Werden nicht-proportionale
355 Werte für $width und $height angegeben, dass also für die Breite
356 und die Höhe unterschiedliche Skalierungsfakoren berechnet werden,
357 liefere von beiden den kleineren Wert.
358  
359 =head4 Example
360  
361 Das Bild hat die Größe 249 x 249 und soll skaliert werden auf
362 die Größe 83 x 83:
363  
364 $scale = $img->scaleFactor(83,83);
365 # 0.333333333333333
366  
367 =cut
368  
369 # -----------------------------------------------------------------------------
370  
371 1 ▶sub scaleFactor {
Subroutine scaleFactor: called CC=1 SCAR=0.0
Pod documented
372 1 my ($self,$width,$height) = @_;
373 1 return Quiq::Array->min([$width/$self->width,$height/$self->height]);
374 }
375  
376 # -----------------------------------------------------------------------------
377  
378 =head3 aspectRatio() - Seitenverhältnis
379  
380 =head4 Synopsis
381  
382 $aspectRatio = $img->aspectRatio;
383  
384 =head4 Description
385  
386 Liefere das Seitenverhältnis des Bildes, z.B. '16:9'.
387  
388 =cut
389  
390 # -----------------------------------------------------------------------------
391  
392 1 ▶sub aspectRatio {
Subroutine aspectRatio: called CC=1 SCAR=0.0
Pod documented
393 1 my $self = shift;
394 1 return Quiq::Image->aspectRatio($self->width,$self->height);
395 }
396  
397 # -----------------------------------------------------------------------------
398  
399 =head2 Properties
400  
401 =head3 property() - Liefere Property-Hash
402  
403 =head4 Synopsis
404  
405 $h = $img->property;
406 $h = $img->property(\%hash);
407  
408 =head4 Description
409  
410 Liefere eine Referenz auf den Property-Hash des Bildes. Der
411 Property-Hash speichert zusätzliche Eigenschaften des
412 Bild-Datei-Objektes, die z.B. im Zuge einer Bild-Bearbeitung
413 verwendet werden.
414  
415 Der Property-Hash ist ein Quiq::Hash-Objekt, dessen Schlüssel
416 nicht gelockt sind. Nach der Objekt-Instantiierung ist der
417 Property-Hash leer.
418  
419 =head4 Examples
420  
421 Setze Eigenschaft:
422  
423 $img->property->set(sizeFill=>[1440,1080]);
424  
425 Eigenschaft abfragen:
426  
427 ($width,$height) = $img->property->getArray('sizeFill');
428  
429 =cut
430  
431 # -----------------------------------------------------------------------------
432  
433 3 ▶sub property {
Subroutine property: called CC=2 SCAR=7.6
Pod documented
434 3 my $self = shift;
435 # @_: \%hash
436  
437 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
438 0 return $self->{'propertyH'} = shift;
439 }
440  
441 3 return $self->{'propertyH'};
442 }
443  
444 # -----------------------------------------------------------------------------
445  
446 =head2 Interne Methoden
447  
448 =head3 analyzeFile() - Analysiere Bild-Datei
449  
450 =head4 Synopsis
451  
452 $img->analyzeFile;
453  
454 =head4 Description
455  
456 Analysiere die Bild-Datei und weise die ermittelten
457 Eigenschaften an die Attribute des Objektes zu.
458  
459 =cut
460  
461 # -----------------------------------------------------------------------------
462  
463 7 our $xft = Image::ExifTool->new; # nur einmal instantiieren
464  
465 2 ▶sub analyzeFile {
Subroutine analyzeFile: called CC=5 SCAR=17.8
Pod documented
466 2 my $self = shift;
467  
468 2 ▶ if (!defined $self->get('width')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get("width")) 2 0 2
469 2 my ($width,$height) = Image::Size::imgsize($self->path);
470 2 $self->set(width=>$width);
471 2 $self->set(height=>$height);
472 2 my $h = $xft->ImageInfo($self->path);
473 2 ▶ my $created = $h->{'CreateDate'} || $h->{'DateTimeOriginal'} || '';
Condition40%2/5
$h->{'CreateDate'} || $h->{'DateTimeOriginal'} || ''
l!l
02
$h->{"CreateDate"} or $h->{"DateTimeOriginal"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$h->{'CreateDate'} || $h->{'DateTimeOriginal'} || ''
$h->{"CreateDate"} $h->{"DateTimeOriginal"}
Truth table33%1/3
A or B
A= $h->{"CreateDate"}B= $h->{"DateTimeOriginal"}
ABresult
1X1
011
000
474 2 ▶ if ($created) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created) 0 2 2
475 0 my @arr = split /[^\d]+/,$created;
476 0 $created = sprintf '%04d-%02d-%02d %02d:%02d:%02d',@arr;
477 }
478 2 $self->set(created=>$created);
479 }
480  
481 2 return;
482 }
483  
484 # -----------------------------------------------------------------------------
485  
486 =head1 VERSION
487  
488 1.240
489  
490 =head1 AUTHOR
491  
492 Frank Seitz, L<http://fseitz.de/>
493  
494 =head1 COPYRIGHT
495  
496 Copyright (C) 2026 Frank Seitz
497  
498 =head1 LICENSE
499  
500 This code is free software; you can redistribute it and/or modify
501 it under the same terms as Perl itself.
502  
503 =cut
504  
505 # -----------------------------------------------------------------------------
506  
507 7 1;
508  
509 # eof
« Quiq.pm ↑ Summary Quiq/Dbms.pm »