Mäx Müller
Mäx Müller

Reputation: 253

Perl GD does not draw circles, instead it draws a rectangle every time

I recently discovered the Perl module GD and I wanted to try it out a little bit. So far I haven't got any problems creating a new object, allocating a color and redirecting the whole bunch of data into a file. The problem came up as I opened the image file in a viewer: No matter what I changed, it just displayed a rectangle. And to make fun of me it only got the right color (blue).

What is wrong? Is the creation of a circle not correct?

I will keep trying, but any help is appreciated. Thanks in advance for your help!

This is my code so far:

my $image1 = new GD::Image(100,100);
my $blue = $image1->colorAllocate(0,0,255);
$image1->arc(100,100,50,50,0,360,$blue);
my $print = $image1->png;

open(IMG,">","/home/bernd/perl/pie.png");
binmode IMG;
print IMG $print;
close(IMG);

Upvotes: 2

Views: 705

Answers (2)

Sibster
Sibster

Reputation: 3189

try adding :

my $image1 = new GD::Image(100,100);
my $white = $image1->colorAllocate(255,255,255);
my $blue = $image1->colorAllocate(0,0,255);

you are drawing the arc in the same color as your background

Upvotes: 0

Mark Stosberg
Mark Stosberg

Reputation: 13381

I believe the problem was that the background and foreground were the same color.

Here's a working example which draws a blue circle on a transparent background:

use GD::Image;
use File::Slurp;

my $image1 = GD::Image->new(100,100);
my $white = $image1->colorAllocate(255,255,255);
my $blue = $image1->colorAllocate(0,0,255);
$image1->transparent($white);
$image1->interlaced('true');

$image1->arc(50,50,100,100,0,360,$blue);

write_file("/home/bernd/perl/pie.png", $image1->png);

I also updated the object creation to avoid using using the indirect object notation style of new GD::Image() which is not recommended. File::Slurp was used to clean up writing out the file.

If you are new to image manipulation with Perl, I recommend that you also check out Imager.pm, which has a more modern design.

Upvotes: 2

Related Questions