- 论坛徽章:
- 0
|
原帖由 sun.os 于 2009-7-21 11:13 发表 ![]()
Hi,
大家有没有碰这样的问题,perl调用GD包,在网页里生成图形,我这里页面是乱码.怎么才能生成图片,谢谢.
生成图片的代码是用DG包自带的例子.
you should put mime type correctly in the HTTP Header:
like :
print "Content-type: image/png\n\n";
instead of print "Content-type: text/html\n\n";
Web browser will translate the code generated by GD into an image.
- #!/usr/bin/perl -w
- # image.cgi
- # chmod a+x image,cgi
- # HTML code: <img src="URL_PATH_TO_THIS_SCRIPT">
- use strict;
- use GD;
- sub image {
- # sample image
- my $im = new GD::Image(100,100);
- # allocate some colors
- my $white = $im->colorAllocate(255,255,255);
- my $black = $im->colorAllocate(0,0,0);
- my $red = $im->colorAllocate(255,0,0);
- my $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;
- }
- print "Content-type: image/png\n\n";
- # display in web browser
- image();
复制代码 |
|