免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2161 | 回复: 3
打印 上一主题 下一主题

GD图形如何在web里显示. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-21 11:13 |只看该作者 |倒序浏览
Hi,

大家有没有碰这样的问题,perl调用GD包,在网页里生成图形,我这里页面是乱码.怎么才能生成图片,谢谢.

生成图片的代码是用DG包自带的例子.

论坛徽章:
0
2 [报告]
发表于 2009-07-21 16:25 |只看该作者
原帖由 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.

  1. #!/usr/bin/perl -w
  2. # image.cgi
  3. # chmod a+x image,cgi
  4. # HTML code: <img src="URL_PATH_TO_THIS_SCRIPT">

  5. use strict;
  6. use GD;

  7. sub image {
  8.     # sample image
  9.     my $im = new GD::Image(100,100);
  10.     # allocate some colors
  11.     my $white = $im->colorAllocate(255,255,255);
  12.     my $black = $im->colorAllocate(0,0,0);
  13.     my $red = $im->colorAllocate(255,0,0);
  14.     my $blue = $im->colorAllocate(0,0,255);
  15.     # make the background transparent and interlaced
  16.     $im->transparent($white);
  17.     $im->interlaced('true');
  18.     # Put a black frame around the picture
  19.     $im->rectangle(0,0,99,99,$black);
  20.     # Draw a blue oval
  21.     $im->arc(50,50,95,75,0,360,$blue);
  22.     # And fill it with red
  23.     $im->fill(50,50,$red);
  24.     # make sure we are writing to a binary stream
  25.     binmode STDOUT;
  26.     # Convert the image to PNG and print it on standard output
  27.     print $im->png;
  28. }

  29. print "Content-type: image/png\n\n";

  30. # display in web browser
  31. image();

复制代码

论坛徽章:
0
3 [报告]
发表于 2009-07-21 16:52 |只看该作者
原帖由 ulmer 于 2009-7-21 16:25 发表


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 ge ...



Hi ulmer, 谢谢.

我想设置html的header为
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="image/gif; charset=iso-8859-1" />
</head>

但死活用new CGI生成不了,content回到text/html.

用另一个文件,先用GD生成图片,然后保存成文件,用如下的html

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<img src=123.gif name="test" />
</body>
</html>
要可以显示的,然用perl动态生成上面的html,在IE却显示叉.

用CGI调用您的代码,去显示下载.

论坛徽章:
0
4 [报告]
发表于 2009-07-21 16:59 |只看该作者
再跑一遍,成了,终于出图了,万分感谢,ulmer!!

奇怪的是,在perl里设置:

$cgih=CGI->new();
print $cgih->header(-type=>" image/png");

从打印出来的html看,设置的content-type是image/png,但网页显示却是乱码.

应是Content-Type的设置问题, 再次感谢,ulmer!!

[ 本帖最后由 sun.os 于 2009-7-21 17:07 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP