Quiq/Gd/Image.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« Quiq/Diff.pm ↑ Summary Quiq/Hash/Persistent.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Gd::Image - Schnittstelle zur GD Graphics Library
8  
9 =head1 BASE CLASSES
10  
11 =over 2
12  
13 =item *
14  
15 GD::Image
16  
17 =item *
18  
19 L<Quiq::Object>
20  
21 =back
22  
23 =head1 SYNOPSIS
24  
25 use Quiq::Gd::Image;
26
27 my $img = Quiq::Gd::Image->new(100,100);
28 $img->background('#ffffff');
29 print $img->jpg;
30  
31 =head1 DESCRIPTION
32  
33 Die Klasse ist eine Überdeckung der Klasse GD::Image. Sie
34 überschreibt existierende Methoden und ergänzt die Klasse um
35 weitere Methoden. Die Klasse kann überall verwendet werden, wo
36 GD::Image verwendet wird. Alle Methoden von GD::Image sind auch
37 auf Quiq::Gd::Image-Objekte anwendbar.
38  
39 =head2 Vorteile
40  
41 Die Klasse Quiq::Gd::Image bietet folgende Vorteile:
42  
43 =over 2
44  
45 =item *
46  
47 Beliebig viele Farben, da alle Bilder per Default TrueColor sind.
48 Bei GD sind Bilder per Default pallette-basiert mit maximal
49 256 Farben.
50  
51 =item *
52  
53 Die Klasse verfügt mit der Methode string() über eine einheitliche
54 Schnittstelle zum Zeichnen von GD- und TrueType-Fonts, horizontal
55 und vertikal. Bei GD werden GD- und TrueType-Fonts uneinheitlich
56 behandelt.
57  
58 =item *
59  
60 Die Methoden der Klasse lösen im Fehlerfall eine Exception aus.
61  
62 =back
63  
64 =head2 Unterschiede zwischen Palette-basierten und TrueColor-Bildern
65  
66 Bei TrueColor liefert die GD-Methode colorAllocate() - mehrfach
67 für denselben Farbwert aufgerufen - immer den gleichen
68 Farbindex. Bei einem Palette-Bild wird immer ein neuer Farbindex
69 geliefert, auch wenn der Farbwert gleich ist. Daher muss aus
70 portablitätsgründen die GD-Methode colorResolve() genutzt werden.
71 Diese Portabilität wird von der Methode L<color|"color() - Alloziere Farbe">() sichergestellt.
72  
73 =head2 Portierung einer existierenden Anwendung
74  
75 =over 4
76  
77 =item 1.
78  
79 Konstruktor-Aufruf ersetzen:
80  
81 $img = Quiq::Gd::Image->new($width,$height);
82  
83 statt
84  
85 $img = GD::Image->new($width,$height);
86  
87 =item 2.
88  
89 Nach dem Konstruktor-Aufruf die Hintergrundfarbe setzen:
90  
91 $white = $img->background(255,255,255);
92  
93 statt
94  
95 $white = $img->colorAllocate(255,255,255);
96  
97 =item 3.
98  
99 Anwendung testen. Sie sollte fehlerfrei laufen.
100  
101 =back
102  
103 =head2 Text in ein existierendes Bild schreiben
104  
105 use Quiq::Gd::Font;
106 use Quiq::Gd::Image;
107 use Quiq::Path;
108
109 my $fnt = Quiq::Gd::Font->new('gdMediumBoldFont');
110 my $img = Quiq::Gd::Image->new('bild.jpg');
111 my $color = $img->color(255,0,0);
112 $img->string($fnt,10,10,'TEST',$color);
113 Quiq::Path->write('bild.jpg',$img->jpeg);
114  
115 =head2 Hintergrund transparent machen
116  
117 my $img = Quiq::Gd::Image->new($width,$height);
118 my $white = $img->background(255,255,255);
119 $img->transparent($white);
120  
121 =cut
122  
123 # -----------------------------------------------------------------------------
124  
125 package Quiq::Gd::Image;
126 3 ▶use base qw/GD::Image Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 use v5.10;
129 use strict;
130 use warnings;
131 use utf8;
132  
133 our $VERSION = '1.240';
134  
135 use GD ();
136 use Quiq::Gd::Font;
137 use Quiq::Gd::Image;
138 use Scalar::Util ();
139 use Quiq::Color;
140 use Quiq::Option;
141 use Quiq::Math;
142  
143 # -----------------------------------------------------------------------------
144  
145 =head1 METHODS
146  
147 =head2 Konstruktor
148  
149 =head3 new() - Instantiiere Bildobjekt
150  
151 =head4 Synopsis
152  
153 $img = $class->new($file);
154 $img = $class->new($data);
155 $img = $class->new($width,$height);
156 $img = $class->new($width,$height,$maxColors);
157  
158 =head4 Returns
159  
160 Bildobjekt
161  
162 =head4 Description
163  
164 Instantiiere ein Bildobjekt der Breite $width und der Höhe $height mit
165 einer maximalen Anzahl von $maxColors Farben und liefere eine
166 Referenz auf dieses Objekt zurück. Schlägt der Aufruf fehl, löse
167 eine Exception aus.
168  
169 Ist $maxColors nicht angegeben oder $maxColors > 256, wird ein
170 TrueColor-Bild erzeugt, andernfalls ein palette-basiertes Bild
171 mit maximal 256 Farben.
172  
173 Die Methode blesst das Objekt auf die Klasse $class, da die Methoden
174 newPalette() und newTrueColor() der Klasse GD::Image dies nicht tun!
175  
176 Der Hintergrund eines TrueColor-Bildes ist schwarz. Eine andere
177 Hintergrundfarbe wird mit background() gesetzt. Anders als
178 bei einem palette-basierten Bild ist I<nicht> die erste allozierte
179 Farbe die Hintergrundfarbe!
180  
181 =head4 See Also
182  
183 Siehe "perldoc GD", Methoden newPalette(), newTrueColor().
184  
185 =cut
186  
187 # -----------------------------------------------------------------------------
188  
189 sub new {
190 my $class = shift;
191 # my @args = @_; # @args wird nur im Fehlerfall gebraucht
192 # @_: s.u.
193  
194 my $self;
195 if (@_ == 1) { # $file -or- $data
196 $self = $class->SUPER::new(shift);
197  
198 #my $file = shift;
199 #
200 #if ($file =~ /\.jpg$/i) {
201 # $self = $class->newFromJpeg($file,1);
202 #}
203 #elsif ($file =~ /\.png$/i) {
204 # $self = $class->newFromPng($file,1);
205 #}
206 #else {
207 # $class->throw(
208 # 'GD-00005: Unbekannter Dateityp',
209 # File => $file,
210 # );
211 #}
212 }
213 else { # $width,$height,$color
214 my $width = shift;
215 my $height = shift;
216 my $colors = shift;
217  
218 if ($colors && $colors <= 256) {
219 # Palettebasiertes Bild mit max. 256 Farben
220 $self = $class->SUPER::newPalette($width,$height);
221 }
222 else {
223 # TrueColor-Bild
224 $self = $class->SUPER::newTrueColor($width,$height);
225 }
226 }
227 unless ($self) {
228 $class->throw(
229 'GD-00001: Konstruktoraufruf fehlgeschlagen',
230 # ConstructorArguments => "[@args]",
231 );
232 }
233  
234 return bless $self,$class;
235 }
236  
237 # -----------------------------------------------------------------------------
238  
239 =head2 Klassenmethoden
240  
241 =head3 textImage() - Erzeuge Bild mit Text
242  
243 =head4 Synopsis
244  
245 $img = $class->textImage($text,@opt);
246  
247 =head4 Arguments
248  
249 =over 4
250  
251 =item $text
252  
253 Text, der in das Bild geschrieben wird.
254  
255 =back
256  
257 =head4 Options
258  
259 =over 4
260  
261 =item -background => $color (Default: 'white')
262  
263 Hintergrundfarbe.
264  
265 =item -color => $color (Default: 'white')
266  
267 Textfarbe.
268  
269 =item -font => $font (Default: 'gdGiantFont')
270  
271 Font, in dem der Text gesetzt wird.
272  
273 =back
274  
275 =head4 Returns
276  
277 Bildobjekt
278  
279 =head4 Description
280  
281 Erzeuge ein Bild, das den Text $text enthält.
282  
283 =cut
284  
285 # -----------------------------------------------------------------------------
286  
287 sub textImage {
288 my ($self,$text) = splice @_,0,2;
289  
290 # Optionen
291  
292 my $background = '#ffffff';
293 my $color = '#000000';
294 my $font = 'gdGiantFont';
295  
296 $self->parameters(\@_,
297 background => \$background,
298 color => \$color,
299 font => 'gdGiantFont',
300 );
301  
302 # Bild erzeugen
303  
304 my $fnt = Quiq::Gd::Font->new($font);
305 my $width = $fnt->stringWidth($text)+1;
306 my $height = $fnt->stringHeight($text);
307 my $img = Quiq::Gd::Image->new($width,$height);
308 $img->background($background);
309 $img->string($fnt,1,0,$text,$img->color($color));
310  
311 return $img;
312 }
313  
314 # -----------------------------------------------------------------------------
315  
316 =head2 Objektmethoden
317  
318 =head3 color() - Alloziere Farbe
319  
320 =head4 Synopsis
321  
322 $color = $img->color; # Default-Farbe
323 $color = $img->color(undef); # Default-Farbe
324 $color = $img->color($n); # Index, bereits allozierte GD-Farbe
325 $color = $img->color($r,$g,$b); # dezimal
326 $color = $img->color(\@rgb); # dezimal als Array-Referenz
327 $color = $img->color('RRGGBB'); # hexadezimal
328 $color = $img->color('#RRGGBB'); # hexadezimal
329  
330 =head4 Aliases
331  
332 =over 2
333  
334 =item *
335  
336 colorAllocate()
337  
338 =item *
339  
340 colorResolve()
341  
342 =item *
343  
344 colorFindAllocate()
345  
346 =back
347  
348 =head4 Returns
349  
350 Farbe
351  
352 =head4 Description
353  
354 Alloziere Farbe in der Farbtabelle des Bildes und liefere den Index des
355 Eintrags zurück. Existiert die Farbe bereits, liefere den existierenden
356 Index.
357  
358 =head4 See Also
359  
360 "perldoc GD" Methode colorResolve()
361  
362 =cut
363  
364 # -----------------------------------------------------------------------------
365  
366 my @DefaultColor = (0,0,0);
367  
368 sub color {
369 my $self = shift;
370 # @_: @color
371  
372 # Farbangabe @color in Tripel ($r,$g,$b) übersetzen
373  
374 if (@_ == 0) {
375 # Default-Farbe
376 @_ = @DefaultColor;
377 }
378 elsif (@_ == 1) {
379 if (!defined $_[0]) {
380 # Default-Farbe
381 @_ = @DefaultColor;
382 }
383 elsif ($_[0] =~ /^\d+$/ && length($_[0]) != 6) {
384 # bereits allozierte GD-Farbe
385 return $_[0];
386 }
387 else {
388 # alles andere mittels Farbklasse übersetzen
389 # \@rgb, #RRGGBB, RRGGBB
390 @_ = Quiq::Color->new($_[0])->rgb;
391 }
392 }
393 elsif (@_ == 3) {
394 # nichts tun
395 }
396 else {
397 $self->throw(
398 'GD-00003: Unerlaubte Farbangabe',
399 Color => join(',',@_),
400 );
401 }
402  
403 my $color = $self->SUPER::colorResolve(@_); # $r,$g,$b
404 if ($color < 0) {
405 $self->throw(
406 'GD-00002: Kann Farbtabelleneintrag nicht allozieren',
407 RGB => join(',',@_),
408 );
409 }
410  
411 return $color;
412 }
413  
414 {
415 no warnings 'once';
416 *colorAllocate = \&color;
417 *colorResolve = \&color;
418 *colorFindAllocate = \&color;
419 }
420  
421 # -----------------------------------------------------------------------------
422  
423 =head3 background() - Alloziere Farbe und setze Hintergrund
424  
425 =head4 Synopsis
426  
427 $color = $img->background(@color);
428  
429 =head4 Returns
430  
431 Farbe
432  
433 =head4 Description
434  
435 Alloziere Farbe @color, fülle das gesamte Bild mit der Farbe
436 und liefere den Farbindex zurück.
437  
438 Zu den möglichen Angaben für @color siehe Methode $img->L<color|"color() - Alloziere Farbe">().
439  
440 Der Hintergrund eines TrueColor-Bildes ist anfänglich schwarz.
441 Anders als bei einem Palette-basierten Bild wird I<nicht> die erste
442 allozierte Farbe automatisch die Hintergrundfarbe. Daher sollte die
443 erste Farbe mit dieser Methode alloziert werden, damit
444 gleichzeitig die Hintergrundfarbe gesetzt wird.
445  
446 =cut
447  
448 # -----------------------------------------------------------------------------
449  
450 sub background {
451 my $self = shift;
452 # @_: @color
453  
454 my $color = $self->color(@_);
455 $self->filledRectangle(0,0,$self->width-1,$self->height-1,$color);
456  
457 return $color;
458 }
459  
460 # -----------------------------------------------------------------------------
461  
462 =head3 border() - Zeichne inneren Rahmen um das Bild
463  
464 =head4 Synopsis
465  
466 $img->border($color);
467  
468 =head4 Returns
469  
470 Nichts
471  
472 =head4 Description
473  
474 Zeichne einen inneren Rahmen in Farbe $color um das Bild.
475 "Innerer Rahmen" bedeutet, dass der Rahmen Teil des Bildes ist
476 und nicht außen um das Bild herumgelegt ist.
477  
478 =cut
479  
480 # -----------------------------------------------------------------------------
481  
482 sub border {
483 my $self = shift;
484 my $color = shift;
485  
486 my $width = $self->width-1;
487 my $height = $self->height-1;
488 $color = $self->color($color);
489 $self->rectangle(0,0,$width,$height,$color);
490  
491 return;
492 }
493  
494 # -----------------------------------------------------------------------------
495  
496 =head3 drawCross() - Zeichne ein Kreuz
497  
498 =head4 Synopsis
499  
500 $img->drawCross($x,$y,$color);
501  
502 =cut
503  
504 # -----------------------------------------------------------------------------
505  
506 sub drawCross {
507 my ($self,$x,$y,$color) = @_;
508  
509 $self->line($x-2,$y,$x+2,$y,$color);
510 $self->line($x,$y-2,$x,$y+2,$color);
511  
512 return;
513 }
514  
515 # -----------------------------------------------------------------------------
516  
517 =head3 string() - Zeichne Zeichenkette horizontal oder vertikal
518  
519 =head4 Synopsis
520  
521 $img->string($font,$x,$y,$string,$color,@opt);
522  
523 =head4 Options
524  
525 =over 4
526  
527 =item -up => $bool (Default: 0)
528  
529 Schreibe den Text nicht horizontal sondern vertikal.
530  
531 =back
532  
533 =head4 Returns
534  
535 Nichts
536  
537 =head4 Description
538  
539 Zeichne Zeichenkette $string an Position ($x,$y). Die Zeichenkette
540 kann mehrzeilig sein.
541  
542 Der Font ist ein C<< Quiq::Gd::Font >>-Objekt. Diese Klasse
543 vereinheitlicht GD- und TrueType-Fonts. GD-Fonts können nicht
544 in beliebigem Winkel, sondern nur horizontal und vertikal geschrieben
545 werden, daher erlaubt die Methode nur diese beiden Ausrichtungen.
546  
547 Die Position ($x,$y) ist in beiden Ausrichtungen die linke Ecke
548 oberhalb des ersten Zeichens.
549  
550 horizontal vertikal
551
552 ($x,$y)
553 x---------+ +---+
554 | ....... | | . |
555 +---------+ | . |
556 | . |
557 | . |
558 | . |
559 ($x,$y) x---+
560  
561 =cut
562  
563 # -----------------------------------------------------------------------------
564  
565 sub string {
566 my $self = shift;
567 my $font = shift;
568 my $x = shift;
569 my $y = shift;
570 my $string = shift;
571 my $color = shift;
572 # @_: @opt
573  
574 # Optionen
575  
576 my $up = 0;
577  
578 if (@_) {
579 Quiq::Option->extract(\@_,
580 -up => \$up,
581 );
582 }
583  
584 if (!$font->isTrueType) {
585 # GD-Font
586  
587 my $gdFont = $font->{'font'};
588  
589 unless ($string =~ tr/\n//) {
590 # Einzeiliger Text
591  
592 if ($up) {
593 $self->SUPER::stringUp($gdFont,$x,$y,$string,$color);
594 }
595 else {
596 $self->SUPER::string($gdFont,$x,$y,$string,$color);
597 }
598 }
599 else {
600 # Mehrzeiliger Text. Da die GD-Methoden string() und stringUp()
601 # mit mehrzeiligem Text nicht richtig umgehen kann (statt Newline
602 # wird "VT" geschrieben und es wird keine Zeile weiter
603 # geschaltet), realisieren wird das Schreiben von mehrzeiligem
604 # Text hier selbst.
605  
606 my $charHeight = $font->charHeight;
607  
608 if ($up) {
609 for my $line (split /\n/,$string) {
610 $self->SUPER::stringUp($gdFont,$x,$y,$line,$color);
611 $x += $charHeight;
612 }
613 }
614 else {
615 for my $line (split /\n/,$string) {
616 $self->SUPER::string($gdFont,$x,$y,$line,$color);
617 $y += $charHeight;
618 }
619 }
620 }
621 }
622 else {
623 # TrueType Font
624  
625 my (undef,undef,$xOffset,$yOffset) = $font->stringGeometry($string,
626 -up => $up,
627 );
628 $x += $xOffset;
629 $y += $yOffset;
630  
631 my $fontName = $font->{'font'};
632 my $pt = $font->{'pt'};
633 my $angle = $up? Quiq::Math->degreeToRad(90): 0;
634 $self->stringFT($color,$fontName,$pt,$angle,$x,$y,$string);
635 }
636  
637 return;
638 }
639  
640 # -----------------------------------------------------------------------------
641  
642 =head3 stringUp() - Zeichne Zeichenkette vertikal
643  
644 =head4 Synopsis
645  
646 $img->stringUp($font,$x,$y,$string,$color);
647  
648 =head4 Returns
649  
650 Nichts
651  
652 =head4 Description
653  
654 Die Methode ist die font-portable Überdeckung für die gleichnmige
655 Methode in der GD-Bibliothek.
656  
657 =cut
658  
659 # -----------------------------------------------------------------------------
660  
661 sub stringUp {
662 return shift->string(@_,-up=>1);
663 }
664  
665 # -----------------------------------------------------------------------------
666  
667 =head3 stringCentered() - Zeichne Zeichenkette horizontal oder vertikal zentriert
668  
669 =head4 Synopsis
670  
671 $img->stringCentered($font,$orientation,$x,$y,$string,$color);
672  
673 =cut
674  
675 # -----------------------------------------------------------------------------
676  
677 sub stringCentered {
678 my ($self,$fnt,$orientation,$x,$y,$string,$color) = @_;
679  
680 if ($orientation eq 'v') {
681 # Bei vertikaler Zentrierung ist kein Korrekturwert nötig
682 # (sowohl bei GD- als auch TrueType-Fonts)
683  
684 $y -= $fnt->stringHeight($string)/2;
685 }
686 else {
687 # Bei horizontaler Zentrierung ist ein Korrekturwert nötig: -1
688 # (sowohl bei GD- als auch TrueType-Fonts)
689  
690 $x -= $fnt->stringWidth($string)/2+$fnt->hCenterOffset;
691 }
692 $self->string($fnt,$x,$y,$string,$color);
693  
694 return;
695 }
696  
697 # -----------------------------------------------------------------------------
698  
699 =head3 rainbowColors() - Alloziere Regenbogenfarben-Palette
700  
701 =head4 Synopsis
702  
703 @colors | $colorA = $img->rainbowColors($n);
704  
705 =head4 Returns
706  
707 Array von Farben
708  
709 =head4 Description
710  
711 Alloziere eine Palette von $n Regenbogenfarben (Blau nach Rot) und
712 liefere die Liste der Farbtabellen-Indizes zurück.
713  
714 Werte für $n: 4, 8, 16, 32, 64, 128, 256, 512, 1024.
715  
716 Die Regenbogenfarben können verwendet werden, um die Werte
717 eines Wertebereichs in einen Farbverlauf zu übersetzen.
718  
719 Farbe % Wertebereich R G B
720 --------- -------------- --- --- ---
721 Blau 0 0 0 255
722 | G-Anteil nimmt zu
723 Hellblau 25 0 255 255
724 | B-Anteil nimmt ab
725 Gruen 50 0 255 0
726 | R-Anteil nimmt zu
727 Gelb 75 255 255 0
728 | G-Anteil nimmt ab
729 Rot 100 255 0 0
730  
731 =cut
732  
733 # -----------------------------------------------------------------------------
734  
735 sub rainbowColors {
736 my $self = shift;
737 my $n = shift;
738  
739 if ($n % 4) {
740 $self->throw(
741 'GD-00007: Anzahl Farben muss durch 4 teilbar sein',
742 N => $n,
743 );
744 }
745 if (256 % ($n/4)) {
746 $self->throw(
747 'GD-00008: Unzulässige Anzahl Farben',
748 N => $n,
749 );
750 }
751  
752 my $step = 256/($n/4);
753  
754 my @colors;
755 for (my $i = 0; $i < 256; $i += $step) {
756 push @colors,$self->color(0,$i,255);
757 }
758  
759 for (my $i = 256-$step; $i >= 0; $i -= $step) {
760 push @colors,$self->color(0,255,$i);
761 }
762  
763 for (my $i = 0; $i < 256; $i += $step) {
764 push @colors,$self->color($i,255,0);
765 }
766  
767 for (my $i = 256-$step; $i >= 0; $i -= $step) {
768 push @colors,$self->color(255,$i,0);
769 }
770  
771 return wantarray? @colors: \@colors;
772 }
773  
774 # -----------------------------------------------------------------------------
775  
776 =head1 VERSION
777  
778 1.240
779  
780 =head1 AUTHOR
781  
782 Frank Seitz, L<http://fseitz.de/>
783  
784 =head1 COPYRIGHT
785  
786 Copyright (C) 2026 Frank Seitz
787  
788 =head1 LICENSE
789  
790 This code is free software; you can redistribute it and/or modify
791 it under the same terms as Perl itself.
792  
793 =cut
794  
795 # -----------------------------------------------------------------------------
796  
797 1;
798  
799 # eof
« Quiq/Diff.pm ↑ Summary Quiq/Hash/Persistent.pm »