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); //销毁图像,释放资源
- 欢迎各大神点评 -