将图形以PNG格式输出到浏览器或文件
將圖形以PNG格式輸出到瀏覽器或文件
PNG格式的英文全稱是:Portable Network Graphic Format,即流式網(wǎng)絡(luò)圖形格式,這種格式主要用于替換GIF和TIFF格式的文件,其主要擴展名是:.png。使用imagepng()函數(shù)可以把圖形輸出到瀏覽器中,也可以把圖形輸出為PNG格式的文件。下面介紹imagepng()函數(shù)的使用方法,代碼如清單所示。
<?php
//創(chuàng)建一個新圖形
$image = imagecreatetruecolor(400,100);
//分配顏色
$bgColor = imagecolorallocate($image,250,250,250);
$black = imagecolorallocate($image,0,0,0);
//填充背景
imagefill($image,0,0,$bgColor);
//指定imagettftext()函數(shù)使用的字體
$font = “simhei.ttf”;
//字義要輸出的中文字符串
$string = “這是PNG圖形”;
//對中文字符進行編碼
$codeString = iconv(“GB2312″,”UTF-8″,$string);
//使用imagettftext()函數(shù)輸出文字
imagettftext($image,20,0,30,30,$black,$font,$codeString);
//把PNG圖形保存為文件
imagepng($image,”pngfile.png”);
//把PNG圖形輸出到瀏覽器
header(“Content-type: image/png”);
imagepng($image);
//釋放資源
imagedestroy($image);
?>
總結(jié)
以上是生活随笔為你收集整理的将图形以PNG格式输出到浏览器或文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 输出中文字符
- 下一篇: 将图形以JPEG格式输出到浏览器或文件