现在的位置: 首页 > 综合 > 正文

将图形以PNG格式输出到浏览器或文件

2018年04月14日 ⁄ 综合 ⁄ 共 711字 ⁄ 字号 评论关闭

将图形以PNG格式输出到浏览器或文件

PNG格式的英文全称是:Portable Network Graphic Format,即流式网络图形格式,这种格式主要用于替换GIF和TIFF格式的文件,其主要扩展名是:.png。使用imagepng()函数可以把图形输出到浏览器中,也可以把图形输出为PNG格式的文件。下面介绍imagepng()函数的使用方法,代码如清单所示。

<?php

//创建一个新图形

$image = imagecreatetruecolor(400,100);

//分配颜色

$bgColor = imagecolorallocate($image,250,250,250);

$black = imagecolorallocate($image,0,0,0);

//填充背景

imagefill($image,0,0,$bgColor);

//指定imagettftext()函数使用的字体

$font = “simhei.ttf”;

//字义要输出的中文字符串

$string = “这是PNG图形”;

//对中文字符进行编码

$codeString = iconv(“GB2312″,”UTF-8″,$string);

//使用imagettftext()函数输出文字

imagettftext($image,20,0,30,30,$black,$font,$codeString);

//把PNG图形保存为文件

imagepng($image,”pngfile.png”);

//把PNG图形输出到浏览器

header(“Content-type: image/png”);

imagepng($image);

//释放资源

imagedestroy($image);

?>

抱歉!评论已关闭.