[2015-09-11]
学习内容
《PHP和MySQL Web开发》第22章图像处理
学习体会
代码见于
https://github.com/easyfly007/practice_php/tree/master/chap22
1. 安装
http://www.php.net/manual/zh/install.windows.extensions.php
在windows下gd2.dll 是自带安装的,只要在 php.ini文件中打开就可以了
1.1 确保 extension_dir = C:phpextensions 指向你的在php 在扩展包的目录
1.2 确保 ;extension=php_bz2.dll 前面的注释符号;去掉
2. 图像格式
JPEG 有损
PNG 无损
GIF 无损
3. 建立图像的基本流程
3.1 创建图像的背景
3.2 绘制图像轮廓,或者输入文字
3.3 输出最终的图像
3.4 清楚所有的资源
4. 创建图像
4.1 可以新建一个图像
调用函数
$im = imagecreatetruecolor($width, $height);
返回一个新图像的标识符,类似于文件句柄
4.2 从已有图像文件打开
$im = imagecreatefrompng('imagename.png');
$im = imagecreatefromjpeg('imagename.jpeg');
$im = imagecreatefromgif('imagename.gif');
5. 绘制图像
5.1 选择绘制的颜色
$black = imagecolorallocate($im, 0,0,0);
$white = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
$red = imagecolorallocate($im, 255, 0, 0);
5.2 绘图函数
imagefill($im, 0, 0, $blue);
绘图填充区域的起始坐标 0,0,用$blue 填充
画一条线
imagestring($im, 0, 0, $width, $height, $white);
起始点(0,0),终点($width, $height), 颜色为$white
添加字符串标签
imgetstring($im, 4, 50, 150, 'sales', $white);
字体为4, 位置为(50, 150), 内容为 ‘sales', 颜色为$white
添加字符标签
bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int $color )
6. 输出图形
可以将这个图像直接输出到浏览器或者文件
6.1 Header('Content-type: image/png');
设置文件输出格式,可以用的包括:
超文本标记语言文本 .html,.html text/html
普通文本 .txt text/plain
RTF文本 .rtf application/rtf
GIF图形 .gif image/gif
JPEG图形 .ipeg,.jpg image/jpeg
au声音文件 .au audio/basic
MIDI音乐文件 mid,.midi audio/midi,audio/x-midi
RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
TAR文件 .tar application/x-tar
Header()也可以用来进行重定向,用于加载一个不同的页面
Header('Location: http://www.domain.com/new_homepage.html');
6.2 输出图像
imagepng($im);
不同的函数包括
imagejpeg($im);
imgetgif($im);
7. 清理
imagedestroy();
imagedestroy($im);