GD::Raw - Low level language bindings to GD Graphics Library
use GD::Raw;
my $fh = fopen("my-image.png", "rb");
my $img = gdImageCreateFromPng($fh);
LEAVE gdImageDestroy($_) with $img;
say "Image resolution is ", gdImageSX($img), "x", gdImageSX($img);GD::Raw is a low level language bindings to LibGD. It does not attempt to provide you with an rakuish interface, but tries to stay as close to its C origin as possible.
LibGD is large and this module far from covers it all. Feel free to add anything your missing and submit a pull request!
Not all libgd functions are ported to Raku module GD::Raw. Here is the list of these functions, in the same order as the libgd 2.3.3 documentation
[gdImageRed](https://libgd.github.io/manuals/2.3.3/files/gd-h.html#gdImageRed
L<C<gdImageGreen)|https://libgd.github.io/manuals/2.3.3/files/gd-h.html#gdImageGreen>
fopen file management for graphic files
fclose file management for graphic files
gdImageCreatePalette is an alias of gdImageCreate.
When creating an in-memory image, some memory is allocated in GD. This memory is not automatically deallocated when the variable which refers to the image goes out of scope. To counter this possible memory leak, the simplest way is to use the LEAVE phaser and call function gdImageDestroy like this
my $img = gdImageCreateFromPng($fh);
LEAVE gdImageDestroy($_) with $img;If a program creates several images, there will be a problem if the program reuses the $img variable. In this case, you cannot use the LEAVE phaser, you must call gdImageDestroy before creating the second image (and the third, and...)
my $img = gdImageCreateFromPng($fh1);
[...]
gdImageDestroy($img);
$img = gdImageCreateFromPng($fh2);
[...]
gdImageDestroy($img);
$img = gdImageCreateFromPng($fh3);
[...]
gdImageDestroy($img);Or a simpler solution is to use different variables $img1, $img2, $img3 and so on, and calling gdImageDestroy each time with the LEAVE phaser.
my $img1 = gdImageCreateFromPng($fh1);
LEAVE gdImageDestroy($_) with $img1;
[...]
my $img2 = gdImageCreateFromPng($fh2);
LEAVE gdImageDestroy($_) with $img2;
[...]
my $img3 = gdImageCreateFromPng($fh3);
LEAVE gdImageDestroy($_) with $img3;
[...]When using a function gdImageXXXPtr to fill a blob with graphic data, the memory management function is gdFree. Usually, the pointer has a very short lifespan. Once the blob has been generated by blob-from-pointer, the pointer is useless and can be fred immediately. A typical chunk of code would be:
use GD::Raw;
use NativeHelpers::Blob;
[...]
my int32 $size;
my $ptr = gdImagePngPtr($im, $size);
my $blob = blob-from-pointer($ptr, elems => $size, type => Blob[int8]);
gdFree($ptr);Raku Module GD: https://github.com/raku-community-modules/GD
C library: https://libgd.github.io/
-
Dagur Valberg Johannsson
-
Raku Community
Copyright 2013 - 2018 Dagur Valberg Johannsson
Copyright 2024, 2026 Raku Community
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.