php图片输出特殊符号,php 字符串生成图片,并输出

GD和图像处理(一)

摘要:如何使用字符串生成图片,并输出到浏览器或指定的路径文件。   1. 创建图像:

新建一个基于调色板的图像

$im = imagecreate($width, $height) or die(“不能初始化新的 GD 图像流”); //返回图像标识符  $_bg_color = imagecolorallocate($im, 255,255,255); //创建颜色,返回颜色标识符

创建图像后,紧跟的第一个颜色标识符为其背景颜色。新建一个真彩色图像

$im = imagecreatetruecolor($width, $height) or die(“不能初始化新的 GD 图像流”); //返回图像标识符,背景为黑色  $_bg_color = imagecolorallocate($im, 255,255,255); //创建颜色,返回颜色标识符  imagefill($im, 0, 0, $_bg_color); //初始化图像背景为$_bg_color

以上两种方式创建的图像相同。

2. 生成字符串图片:

$str = ‘12345678’;  $_text_color = imagecolorallocate($im, 0,0,0);

(bool) imagestring ( resource $image , int $font , int $x , int $y , string $s , int $color )

imagestring($im, 3, 2, 3, $str , $_text_color);

(array) imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo ] )

$font = ‘./KozGoPro-Light-2.otf’; //必须,可以设置期望的字体  imagefttext($im, 10, 0, 1, 15, $_text_color, $font, $str);

若字体相同,以上两种方式生成的图片相同。

3. 输出图片:

输出到浏览器

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

imagedestroy($im); //销毁图像,释放资源

输出的指定文件

$path = ‘C:\Users\Administrator\Desktop\image.png’; //文件保存路径及名称  imagepng($im, $path);  imagedestroy($im); //销毁图像,释放资源

- 欢迎各大神点评 -

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值