|
|
|||
Dealing with GD module from CPAN.
My task is to draw a graph or a diagram, using Perl. I use GD module. I'm working on Windows with ActivePerl 5.1. GD module is installed, but I'm afraid I haven't got libgd yet. If it's possible, please explain how to install this library. But the problem is, that I've copied as an example code from O'Reilly book "Mastering Algorithms with Perl" to draw a circle. Programm works (the file is created) but file cannot be displayed. For the case if the problem appeared not because of the library, i'm giving here the code:
#!/usr/bin/perl use GD; #Create the image my $gif = new GD::Image(100, 100); #Allocate colors my $white = $gif->colorAllocate(255, 255, 255); my $red = $gif->colorAllocate(255, 0, 0); #Background color $gif -> transparent($white); #The circle $gif -> arc(50, 50, #Center x y 30, 30, #Width, Height 0, 360, #Start Angle, End Angle $red); #Color #Output the image if (! open GIF, ">circle.jpg") { die "open failed: $!/n";} binmode GIF; print GIF $gif; close GIF; 2 Replies
Comment by
KBenson
: Nov 29 2009 10:06 PM
Works fine for me using ActiveState perl 5.10 and the perl GD module 2.44 with libgd 2.0.33, with the sample code included in the documentation for the perl GD module, included below (redirect to a file, or change code to open file):
use GD; # create a new image $im = new GD::Image(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; Is it possible you are not setting binmode on whatever file handle you are writing to? On windows this is a must. P.S. While the code may not have syntax mistakes, that says nothing about possible logic errors. Reducing the code to the minimum amount required to illustrate the problem and attaching that will probably yield more useful responses.
Since you are using ActivePerl's pre-compiled GD module, you should not need to install libgd separately; it is included in the ActiveState binary PPM of GD.
When printing a GD::Image object, you must use a method call to the desired image format, or you will output the object's string representation (its address) instead of the image. Just change the next-to-last line from this: print GIF $gif; to this: print GIF $gif->jpeg(); |
|||
|