Repository navigation
Expand file tree
/
Copy pathGD.pm
More file actions
2153 lines (1504 loc) · 70.4 KB
/
Copy pathGD.pm
File metadata and controls
2153 lines (1504 loc) · 70.4 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package GD;
# Copyright 1995 Lincoln D. Stein. See accompanying README file for
# usage information
use strict;
require 5.004;
require FileHandle;
require Exporter;
require DynaLoader;
require AutoLoader;
use Carp 'croak','carp';
use GD::Image;
use GD::Polygon;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
$VERSION = '2.86';
our $XS_VERSION = $VERSION;
$VERSION = eval $VERSION;
@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
gdBrushed
gdDashSize
gdMaxColors
gdStyled
gdStyledBrushed
gdTiled
gdTransparent
gdAntiAliased
gdArc
gdChord
gdPie
gdNoFill
gdEdged
gdTinyFont
gdSmallFont
gdMediumBoldFont
gdLargeFont
gdGiantFont
gdAlphaMax
gdAlphaOpaque
gdAlphaTransparent
);
@EXPORT_OK = qw (
GD_CMP_IMAGE
GD_CMP_NUM_COLORS
GD_CMP_COLOR
GD_CMP_SIZE_X
GD_CMP_SIZE_Y
GD_CMP_TRANSPARENT
GD_CMP_BACKGROUND
GD_CMP_INTERLACE
GD_CMP_TRUECOLOR
);
%EXPORT_TAGS = ('cmp' => [ @EXPORT_OK ] );
# documentation error
*GD::Polygon::delete = \&GD::Polygon::deletePt;
sub AUTOLOAD {
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function. If a constant is not found then control is passed
# to the AUTOLOAD in AutoLoader.
my($constname);
($constname = $AUTOLOAD) =~ s/.*:://;
undef $!;
my $val = constant($constname);
if ($! != 0) {
if ($! =~ /Invalid/) {
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
}
else {
my($pack,$file,$line) = caller;
die "Your vendor has not defined GD macro $pack\:\:$constname, used at $file line $line $!.\n";
}
}
eval "sub $AUTOLOAD { $val }";
goto &$AUTOLOAD;
}
bootstrap GD;
# Preloaded methods go here.
sub GD::gdSmallFont {
return GD::Font->Small;
}
sub GD::gdLargeFont {
return GD::Font->Large;
}
sub GD::gdMediumBoldFont {
return GD::Font->MediumBold;
}
sub GD::gdTinyFont {
return GD::Font->Tiny;
}
sub GD::gdGiantFont {
return GD::Font->Giant;
}
sub GD::Image::startGroup { } # does nothing - used by GD::SVG
sub GD::Image::endGroup { } # does nothing - used by GD::SVG
sub GD::Image::newGroup {
my $self = shift;
GD::Group->new($self,$self->startGroup);
}
=head1 NAME
GD.pm - Interface to Gd Graphics Library
=head1 SYNOPSIS
use GD;
# create a new image
$im = GD::Image->new(100,100);
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
# Put a black frame around the picture
$im->rectangle(0,0,99,99,$black);
# Draw a blue oval
$im->arc(50,50,95,75,0,360,$blue);
# And fill it with red
$im->fill(50,50,$red);
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to PNG and print it on standard output
print $im->png;
=head1 DESCRIPTION
B<GD.pm> is a Perl interface to Thomas Boutell's gd graphics library
(version 2.01 or higher; see below). GD allows you to create color
drawings using a large number of graphics primitives, and emit the
drawings as PNG files.
GD defines the following four classes:
=over 5
=item C<GD::Image>
An image class, which holds the image data and accepts graphic
primitive method calls.
=item C<GD::Font>
A font class, which holds static font information and used for text
rendering.
=item C<GD::Polygon>
A simple polygon object, used for storing lists of vertices prior to
rendering a polygon into an image.
=item C<GD::Simple>
A "simple" class that simplifies the GD::Image API and then adds a set
of object-oriented drawing methods using turtle graphics, simplified
font handling, ability to work in polar coordinates, HSV color spaces,
and human-readable color names like "lightblue". Please see
L<GD::Simple> for a description of these methods.
=back
A Simple Example:
#!/usr/bin/perl
use GD;
# create a new image
$im = GD::Image->new(100,100);
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
# Put a black frame around the picture
$im->rectangle(0,0,99,99,$black);
# Draw a blue oval
$im->arc(50,50,95,75,0,360,$blue);
# And fill it with red
$im->fill(50,50,$red);
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to PNG and print it on standard output
print $im->png;
Notes:
=over 5
=item 1.
To create a new, empty image, send a new() message to GD::Image, passing
it the width and height of the image you want to create. An image
object will be returned. Other class methods allow you to initialize
an image from a preexisting JPG, PNG, GD, GD2, XBM or other supported
image files.
=item 2.
Next you will ordinarily add colors to the image's color table.
colors are added using a colorAllocate() method call. The three
parameters in each call are the red, green and blue (rgb) triples for
the desired color. The method returns the index of that color in the
image's color table. You should store these indexes for later use.
=item 3.
Now you can do some drawing! The various graphics primitives are
described below. In this example, we do some text drawing, create an
oval, and create and draw a polygon.
=item 4.
Polygons are created with a new() message to GD::Polygon. You can add
points to the returned polygon one at a time using the addPt() method.
The polygon can then be passed to an image for rendering.
=item 5.
When you're done drawing, you can convert the image into PNG format by
sending it a png() message (or any other supported image format). It
will return a (potentially large) scalar value containing the binary
data for the image. Ordinarily you will print it out at this point or
write it to a file. To ensure portability to platforms that
differentiate between text and binary files, be sure to call
C<binmode()> on the file you are writing the image to.
=back
=head1 Object Constructors: Creating Images
See L<GD::Image> for the current list of supported Image formats.
The following class methods allow you to create new GD::Image objects.
=over 4
=item B<$image = GD::Image-E<gt>new([$width,$height],[$truecolor])>
=item B<$image = GD::Image-E<gt>new(*FILEHANDLE)>
=item B<$image = GD::Image-E<gt>new($filename)>
=item B<$image = GD::Image-E<gt>new($data)>
The new() method is the main constructor for the GD::Image class.
Called with two integer arguments, it creates a new blank image of the
specified width and height. For example:
$myImage = GD::Image->new(100,100) || die;
This will create an image that is 100 x 100 pixels wide. If you don't
specify the dimensions, a default of 64 x 64 will be chosen.
The optional third argument, $truecolor, tells new() to create a
truecolor GD::Image object. Truecolor images have 24 bits of color
data (eight bits each in the red, green and blue channels
respectively), allowing for precise photograph-quality color usage.
If not specified, the image will use an 8-bit palette for
compatibility with older versions of libgd.
Alternatively, you may create a GD::Image object based on an existing
image by providing an open filehandle, a filename, or the image data
itself. The image formats automatically recognized and accepted are:
GIF, PNG, JPEG, XBM, XPM, BMP, GD2, TIFF, WEBP, HEIF or AVIF. Other formats,
including WBMP, and GD version 1, cannot be recognized automatically
at this time, only by filename.
If something goes wrong (e.g. insufficient memory), this call will
return undef.
=item B<$image = GD::Image-E<gt>trueColor([0,1])>
For backwards compatibility with scripts previous versions of GD,
new images created from scratch (width, height) are palette based
by default. To change this default to create true color images use:
GD::Image->trueColor(1);
before creating new images. To switch back to palette
based by default, use:
GD::Image->trueColor(0);
=item B<$image = GD::Image-E<gt>newPalette([$width,$height])>
=item B<$image = GD::Image-E<gt>newTrueColor([$width,$height])>
The newPalette() and newTrueColor() methods can be used to explicitly
create an palette based or true color image regardless of the
current setting of trueColor().
=item B<$image = GD::Image-E<gt>newFromPng($file, [$truecolor])>
=item B<$image = GD::Image-E<gt>newFromPngData($data, [$truecolor])>
The newFromPng() method will create an image from a PNG file read in
through the provided filehandle or file path. The filehandle must
previously have been opened on a valid PNG file or pipe. If
successful, this call will return an initialized image which you can
then manipulate as you please. If it fails, which usually happens if
the thing at the other end of the filehandle is not a valid PNG file,
the call returns undef. Notice that the call doesn't automatically
close the filehandle for you. But it does call C<binmode(FILEHANDLE)>
for you, on platforms where this matters.
You may use any of the following as the argument:
1) a simple filehandle, such as STDIN
2) a filehandle glob, such as *PNG
3) a reference to a glob, such as \*PNG
4) an IO::Handle object
5) the pathname of a file
In the latter case, newFromPng() will attempt to open the file for you
and read the PNG information from it.
Example1:
open (PNG,"barnswallow.png") || die;
$myImage = GD::Image->newFromPng(\*PNG) || die;
close PNG;
Example2:
$myImage = GD::Image->newFromPng('barnswallow.png');
To get information about the size and color usage of the information,
you can call the image query methods described below. Images created
by reading PNG images will be truecolor if the image file itself is
truecolor. To force the image to be palette-based, pass a value of 0
in the optional $truecolor argument.
The newFromPngData() method will create a new GD::Image initialized
with the PNG format B<data> contained in C<$data>.
=item B<$image = GD::Image-E<gt>newFromJpeg($file, [$truecolor])>
=item B<$image = GD::Image-E<gt>newFromJpegData($data, [$truecolor])>
These methods will create an image from a JPEG file. They work just
like newFromPng() and newFromPngData(), and will accept the same
filehandle and pathname arguments.
Images created by reading JPEG images will always be truecolor. To
force the image to be palette-based, pass a value of 0 in the optional
$truecolor argument.
=item B<$image = GD::Image-E<gt>newFromGif($file, [$truecolor])>
=item B<$image = GD::Image-E<gt>newFromGifData($data)>
These methods will create an image from a GIF file. They work just
like newFromPng() and newFromPngData(), and will accept the same
filehandle and pathname arguments.
Images created from GIFs are always 8-bit palette images. To convert
to truecolor, you must create a truecolor image and then perform a
copy.
=item B<$image = GD::Image-E<gt>newFromXbm($file, [$truecolor])>
This works in exactly the same way as C<newFromPng>, but reads the
contents of an X Bitmap (black & white) file:
open (XBM,"coredump.xbm") || die;
$myImage = GD::Image->newFromXbm(\*XBM) || die;
close XBM;
There is no newFromXbmData() function, because there is no
corresponding function in the gd library.
=item B<$image = GD::Image-E<gt>newFromWBMP($file)>
This works in exactly the same way as C<newFromPng>, but reads the
contents of a Wireless Application Protocol Bitmap (WBMP) file:
open (WBMP,"coredump.wbmp") || die;
$myImage = GD::Image->newFromWBMP(\*WBMP) || die;
close WBMP;
There is no newFromWBMPData() function, because there is no
corresponding function in the gd library.
=item B<$image = GD::Image-E<gt>newFromBmp($file)>
This works in exactly the same way as C<newFromPng>, but reads the
contents of a Windows Bitmap (BMP) file:
open (BMP,"coredump.bmp") || die;
$myImage = GD::Image->newFromBmp(\*BMP) || die;
close BMP;
There is no newFromBmpData() function, because there is no
corresponding function in the gd library.
=item B<$image = GD::Image-E<gt>newFromGd($file)>
=item B<$image = GD::Image-E<gt>newFromGdData($data)>
NOTE: GD and GD2 support was dropped witn libgd 2.3.2.
These methods initialize a GD::Image from a Gd file, filehandle, or
data. Gd is Tom Boutell's disk-based storage format, intended for the
rare case when you need to read and write the image to disk quickly.
It's not intended for regular use, because, unlike PNG or JPEG, no
image compression is performed and these files can become B<BIG>.
$myImage = GD::Image->newFromGd("godzilla.gd") || die;
close GDF;
=item B<$image = GD::Image-E<gt>newFromGd2($file)>
=item B<$image = GD::Image-E<gt>newFromGd2Data($data)>
NOTE: GD and GD2 support was dropped witn libgd 2.3.2.
This works in exactly the same way as C<newFromGd()> and
newFromGdData, but use the new compressed GD2 image format.
=item B<$image = GD::Image-E<gt>newFromGd2Part($file,srcX,srcY,width,height)>
This class method allows you to read in just a portion of a GD2 image
file. In addition to a filehandle, it accepts the top-left corner and
dimensions (width,height) of the region of the image to read. For
example:
open (GDF,"godzilla.gd2") || die;
$myImage = GD::Image->newFromGd2Part(\*GDF,10,20,100,100) || die;
close GDF;
This reads a 100x100 square portion of the image starting from
position (10,20).
=item B<$image = GD::Image-E<gt>newFromXpm($filename)>
This creates a new GD::Image object starting from a B<filename>. This
is unlike the other newFrom() functions because it does not take a
filehandle. This difference comes from an inconsistency in the
underlying gd library.
$myImage = GD::Image->newFromXpm('earth.xpm') || die;
This function is only available if libgd was compiled with XPM
support.
NOTE: The libgd library is unable to read certain XPM files, returning
an all-black image instead.
=item B<$bool = GD::supportsFileType($filename, $is_writing)>
This returns a TRUE or FALSE value, if libgd supports reading or when
the 2nd argument is 1, if libgd supports writing the given filetype,
depending on the filename extension. Only with libgd versions E<gt>= gd-2.1.1.
Assuming LibGD is compiled with support for these image types, the
following extensions are supported:
.gif
.gd, .gd2
.wbmp
.bmp
.xbm
.tga
.png
.jpg, .jpeg
.tiff, .tif
.webp
.heic, .heix
.avif
.xpm
Filenames are parsed case-insensitively.
.avifs is not yet suppurted upstream in libavif.
=back
=head1 GD::Image Methods
Once a GD::Image object is created, you can draw with it, copy it, and
merge two images. When you are finished manipulating the object, you
can convert it into a standard image file format to output or save to
a file.
=head2 Image Data Output Methods
The following methods convert the internal drawing format into
standard output file formats.
=over
=item B<$pngdata = $image-E<gt>png([$compression_level])>
This returns the image data in PNG format. You can then print it,
pipe it to a display program, or write it to a file. Example:
$png_data = $myImage->png;
open (DISPLAY,"| display -") || die;
binmode DISPLAY;
print DISPLAY $png_data;
close DISPLAY;
Note the use of C<binmode()>. This is crucial for portability to
DOSish platforms.
The optional $compression_level argument controls the amount of
compression to apply to the output PNG image. Values range from 0-9,
where 0 means no compression (largest files, highest quality) and 9
means maximum compression (smallest files, worst quality). A
compression level of -1 uses the default compression level selected
when zlib was compiled on your system, and is the same as calling
png() with no argument. Be careful not to confuse this argument with
the jpeg() quality argument, which ranges from 0-100 and has the
opposite meaning from compression (higher numbers give higher
quality).
=item B<$gifdata = $image-E<gt>gifanimbegin([$GlobalCM [, $Loops]])>
For libgd version 2.0.33 and higher, this call begins an animated GIF
by returning the data that comprises animated gif image file header.
After you call this method, call gifanimadd() one or more times to add
the frames of the image. Then call gifanimend(). Each frame must be
the same width and height.
A typical sequence will look like this:
my $gifdata = $image->gifanimbegin;
$gifdata .= $image->gifanimadd; # first frame
for (1..100) {
# make a frame of right size
my $frame = GD::Image->new($image->getBounds);
add_frame_data($frame); # add the data for this frame
$gifdata .= $frame->gifanimadd; # add frame
}
$gifdata .= $image->gifanimend; # finish the animated GIF
print $gifdata; # write animated gif to STDOUT
If you do not wish to store the data in memory, you can print it to
stdout or a file.
The image that you call gifanimbegin on is used to set the image size,
color resolution and color map. If argument $GlobalCM is 1, the image
color map becomes the GIF89a global color map. If $Loops is given and
>= 0, the NETSCAPE2.0 application extension is created, with looping
count. Looping count 0 means forever.
=item B<$gifdata = $image-E<gt>gifanimadd([$LocalCM [, $LeftOfs [, $TopOfs [, $Delay [, $Disposal [, $previm]]]]]])>
Returns the data that comprises one animated gif image frame. You can
then print it, pipe it to a display program, or write it to a file.
With $LeftOfs and $TopOfs you can place this frame in different offset
than (0,0) inside the image screen. Delay between the previous frame
and this frame is in 1/100s units. Disposal is usually and by default
1. Compression is activated by giving the previous image as a
parameter. This function then compares the images and only writes the
changed pixels to the new frame in animation. The Disposal parameter
for optimized animations must be set to 1, also for the first frame.
$LeftOfs and $TopOfs parameters are ignored for optimized frames.
=item B<$gifdata = $image-E<gt>gifanimend()>
Returns the data for end segment of animated gif file. It always
returns string ';'. This string must be printed to an animated gif
file after all image frames to properly terminate it according to GIF
file syntax. Image object is not used at all in this method.
=item B<$jpegdata = $image-E<gt>jpeg([$quality])>
This returns the image data in JPEG format. You can then print it,
pipe it to a display program, or write it to a file. You may pass an
optional quality score to jpeg() in order to control the JPEG quality.
This should be an integer between 0 and 100. Higher quality scores
give larger files and better image quality. If you don't specify the
quality, jpeg() will choose a good default.
=item B<$gifdata = $image-E<gt>gif()>.
This returns the image data in GIF format. You can then print it,
pipe it to a display program, or write it to a file.
=item B<$gddata = $image-E<gt>gd>
This returns the image data in GD format. You can then print it,
pipe it to a display program, or write it to a file. Example:
binmode MYOUTFILE;
print MYOUTFILE $myImage->gd;
=item B<$gd2data = $image-E<gt>gd2>
Same as gd(), except that it returns the data in compressed GD2
format.
=item B<$bmpdata = $image-E<gt>bmp([$compression])>
This returns the image data in BMP format, which is a Windows Bitmap.
If compression is set to 1, it will use RLE compression on the pixel
data; otherwise, setting it to 0 (the default) will leave the BMP
pixel data uncompressed.
=item B<$wbmpdata = $image-E<gt>wbmp([$foreground])>
This returns the image data in WBMP format, which is a black-and-white
image format. Provide the index of the color to become the foreground
color. All other pixels will be considered background.
=item B<$tiffdata = $image-E<gt>tiff()>
This returns the image data in TIFF format.
=item B<$webpdata = $image-E<gt>webp([$quality])>
This returns the image data in WEBP format, with the optional quality argument.
The default is 80, also chosen by the value -1.
A quality value of >= 101 is considered Lossless.
=item B<$webpdata = $image-E<gt>heif([$quality])>
This returns the truecolor image data in HEIF format, with the
optional quality and speed arguments.
If truecolor is not set, this fails.
The default quality is 80, also chosen by the value -1.
A quality value of 200 is considered Lossless.
=item B<$webpdata = $image-E<gt>avif([$quality,$speed])>
This returns the truecolor image data in AVIF format, with the
AVif encoder and 444 chroma, and the optional quality argument.
If truecolor is not set, this fails.
The default compression quality 1-100 is -1, the default speed 0-10 is 6.
=item B<$success = $image-E<gt>_file($filename)>
Writes an image to a file in the format indicated by the filename, with
libgd versions E<gt>= gd-2.1.1.
File type is determined by the extension of the file name. See
C<supportsFiletype> for an overview of the parsing.
For file types that require extra arguments, C<_file> attempts to
use sane defaults:
C<gdImageGd2> chunk size = 0, compression is enabled.
C<gdImageJpeg> quality = -1 (i.e. the reasonable default)
C<gdImageWBMP> foreground is the darkest available color
C<gdImageWEBP> quality default
C<gdImageHEIF> quality default, codes = HEVC, chroma = 444
C<gdImageAVIF> quality default, speed = 6
Everything else is called with the two-argument function and so will
use the default values.
C<_file> and the underlying libgd C<gdImageFile> has some rudimentary
error detection and will return FALSE (0) if a detectable error
occurred. However, the image loaders do not normally return their
error status so a result of TRUE (1) does **not** mean the file was
saved successfully.
=back
=head2 Color Control
These methods allow you to control and manipulate the GD::Image color
table for palette, non-truecolor images.
=over 4
=item B<$index = $image-E<gt>colorAllocate(red,green,blue)>
This allocates a color with the specified red, green and blue
components and returns its index in the color table, if specified.
The first color allocated in this way becomes the image's background
color. (255,255,255) is white (all pixels on). (0,0,0) is black (all
pixels off). (255,0,0) is fully saturated red. (127,127,127) is 50%
gray. You can find plenty of examples in /usr/X11/lib/X11/rgb.txt.
If no colors are allocated, then this function returns -1.
Example:
$black = $myImage->colorAllocate(0,0,0); #background color
$white = $myImage->colorAllocate(255,255,255);
$peachpuff = $myImage->colorAllocate(255,218,185);
=item B<$index = $image-E<gt>colorAllocateAlpha(reg,green,blue,alpha)>
This allocates a color with the specified red, green, and blue components,
plus the specified alpha channel. The alpha value may range from 0 (opaque)
to 127 (transparent). The C<alphaBlending> function changes the way this
alpha channel affects the resulting image.
=item B<$image-E<gt>colorDeallocate(colorIndex)>
This marks the color at the specified index as being ripe for
reallocation. The next time colorAllocate is used, this entry will be
replaced. You can call this method several times to deallocate
multiple colors. There's no function result from this call.
Example:
$myImage->colorDeallocate($peachpuff);
$peachy = $myImage->colorAllocate(255,210,185);
=item B<$index = $image-E<gt>colorClosest(red,green,blue)>
This returns the index of the color closest in the color table to the
red green and blue components specified. If no colors have yet been
allocated, then this call returns -1.
Example:
$apricot = $myImage->colorClosest(255,200,180);
=item B<$index = $image-E<gt>colorClosestAlpha(red,green,blue,alpha)>
This returns the index of the color closest in the color table to the
red green blue and alpha components specified. If no colors have yet been
allocated, then this call returns -1.
Example:
$apricot = $myImage->colorClosestAlpha(255,200,180,0);
=item B<$index = $image-E<gt>colorClosestHWB(red,green,blue)>
This also attempts to return the color closest in the color table to the
red green and blue components specified. It uses a Hue/White/Black
color representation to make the selected color more likely to match
human perceptions of similar colors.
If no colors have yet been allocated, then this call returns -1.
Example:
$mostred = $myImage->colorClosestHWB(255,0,0);
=item B<$in
10BC8
dex = $image-E<gt>colorExact(red,green,blue)>
This returns the index of a color that exactly matches the specified
red green and blue components. If such a color is not in the color
table, this call returns -1.
$rosey = $myImage->colorExact(255,100,80);
warn "Everything's coming up roses.\n" if $rosey >= 0;
=item B<$index = $image-E<gt>colorExactAlpha(red,green,blue,alpha)>
This returns the index of a color that exactly matches the specified
red green blue and alpha components. If such a color is not in the color
table, this call returns -1.
$rosey = $myImage->colorExactAlpha(255,100,80,0);
warn "Everything's coming up roses.\n" if $rosey >= 0;
=item B<$index = $image-E<gt>colorResolve(red,green,blue)>
This returns the index of a color that exactly matches the specified
red green and blue components. If such a color is not in the color
table and there is room, then this method allocates the color in the
color table and returns its index.
$rosey = $myImage->colorResolve(255,100,80);
warn "Everything's coming up roses.\n" if $rosey >= 0;
=item B<$index = $image-E<gt>colorResolveAlpha(red,green,blue,alpha)>
This returns the index of a color that exactly matches the specified
red green blue and alpha components. If such a color is not in the color
table and there is room, then this method allocates the color in the
color table and returns its index.
$rosey = $myImage->colorResolveAlpha(255,100,80,0);
warn "Everything's coming up roses.\n" if $rosey >= 0;
=item B<$colorsTotal = $image-E<gt>colorsTotal> I<object method>
This returns the total number of colors allocated in the object.
$maxColors = $myImage->colorsTotal;
In the case of a TrueColor image, this call will return undef.
=item B<$index = $image-E<gt>getPixel(x,y)> I<object method>
This returns the color table index underneath the specified
point. It can be combined with rgb()
to obtain the rgb color underneath the pixel.
Example:
$index = $myImage->getPixel(20,100);
($r,$g,$b) = $myImage->rgb($index);
=item B<($red,$green,$blue) = $image-E<gt>rgb($index)>
This returns a list containing the red, green and blue components of
the specified color index.
Example:
@RGB = $myImage->rgb($peachy);
=item B<($alpha) = $image-E<gt>alpha($index)>
This returns an item containing the alpha component of the specified
color index.
Example:
@RGB = $myImage->rgb($peachy);
=item B<$image-E<gt>transparent($colorIndex)>
This marks the color at the specified index as being transparent.
Portions of the image drawn in this color will be invisible. This is
useful for creating paintbrushes of odd shapes, as well as for
making PNG backgrounds transparent for displaying on the Web. Only
one color can be transparent at any time. To disable transparency,
specify -1 for the index.
If you call this method without any parameters, it will return the
current index of the transparent color, or -1 if none.
Example:
open(PNG,"test.png");
$im = GD::Image->newFromPng(PNG);
$white = $im->colorClosest(255,255,255); # find white
$im->transparent($white);
binmode STDOUT;
print $im->png;
=back
=head2 Special Colors
GD implements a number of special colors that can be used to achieve
special effects. They are constants defined in the GD::
namespace, but automatically exported into your namespace when the GD
module is loaded.
=over 4
=item B<$image-E<gt>setBrush($image)>
You can draw lines and shapes using a brush pattern. Brushes are just
palette, not TrueColor, images that you can create and manipulate in
the usual way. When you draw with them, their contents are used for
the color and shape of the lines.
To make a brushed line, you must create or load the brush first, then
assign it to the image using setBrush(). You can then draw in that
with that brush using the B<gdBrushed> special color. It's often
useful to set the background of the brush to transparent so that the
non-colored parts don't overwrite other parts of your image.
Example:
# Create a brush at an angle
$diagonal_brush = GD::Image->new(5,5);
$white = $diagonal_brush->colorAllocate(255,255,255);
$black = $diagonal_brush->colorAllocate(0,0,0);
$diagonal_brush->transparent($white);
$diagonal_brush->line(0,4,4,0,$black); # NE diagonal
# Set the brush
$myImage->setBrush($diagonal_brush);
# Draw a circle using the brush
$myImage->arc(50,50,25,25,0,360,gdBrushed);
=item B<$image-E<gt>setThickness($thickness)>
Lines drawn with line(), rectangle(), arc(), and so forth are 1 pixel
thick by default. Call setThickness() to change the line drawing
width.
=item B<$image-E<gt>setStyle(@colors)>
Styled lines consist of an arbitrary series of repeated colors and are
useful for generating dotted and dashed lines. To create a styled
line, use setStyle() to specify a repeating series of colors. It
accepts an array consisting of one or more color indexes. Then draw
using the B<gdStyled> special color. Another special color,
B<gdTransparent> can be used to introduce holes in the line, as the
example shows.
Example:
# Set a style consisting of 4 pixels of yellow,
# 4 pixels of blue, and a 2 pixel gap
$myImage->setStyle($yellow,$yellow,$yellow,$yellow,
$blue,$blue,$blue,$blue,
gdTransparent,gdTransparent);
$myImage->arc(50,50,25,25,0,360,gdStyled);
To combine the C<gdStyled> and C<gdBrushed> behaviors, you can specify
C<gdStyledBrushed>. In this case, a pixel from the current brush
pattern is rendered wherever the color specified in setStyle() is
neither gdTransparent nor 0.
=item B<gdTiled>
Draw filled shapes and flood fills using a pattern. The pattern is
just another image. The image will be tiled multiple times in order
to fill the required space, creating wallpaper effects. You must call
C<setTile> in order to define the particular tile pattern you'll use
for drawing when you specify the gdTiled color.
details.
=item B<gdStyled>
The gdStyled color is used for creating dashed and dotted lines. A
styled line can contain any series of colors and is created using the
setStyled() command.
=item B<gdAntiAliased>
The C<gdAntiAliased> color is used for drawing lines with antialiasing
turned on. Antialiasing will blend the jagged edges of lines with the
background, creating a smoother look. The actual color drawn is set
with setAntiAliased().
=item B<$image-E<gt>setAntiAliased($color)>
"Antialiasing" is a process by which jagged edges associated with line
drawing can be reduced by blending the foreground color with an
appropriate percentage of the background, depending on how much of the
pixel in question is actually within the boundaries of the line being
drawn. All line-drawing methods, such as line() and polygon, will draw
antialiased lines if the special "color" B<gdAntiAliased> is used when
calling them.
setAntiAliased() is used to specify the actual foreground color to be
used when drawing antialiased lines. You may set any color to be the
foreground, however as of libgd version 2.0.12 an alpha channel
component is not supported.
Antialiased lines can be drawn on both truecolor and palette-based
images. However, attempts to draw antialiased lines on highly complex
palette-based backgrounds may not give satisfactory results, due to
the limited number of colors available in the palette. Antialiased
line-drawing on simple backgrounds should work well with palette-based
images; otherwise create or fetch a truecolor image instead. When
using palette-based images, be sure to allocate a broad spectrum of
colors in order to have sufficient colors for the antialiasing to use.
=item B<$image-E<gt>setAntiAliasedDontBlend($color,[$flag])>
Normally, when drawing lines with the special B<gdAntiAliased>
"color," blending with the background to reduce jagged edges is the
desired behavior. However, when it is desired that lines not be
blended with one particular color when it is encountered in the
background, the setAntiAliasedDontBlend() method can be used to
indicate the special color that the foreground should stand out more
clearly against.
Once turned on, you can turn this feature off by calling
setAntiAliasedDontBlend() with a second argument of 0:
$image->setAntiAliasedDontBlend($color,0);