概述 PHP中绘制 图形 多使用GD库来实现,GD的功能多寡会因你使用的GD版本以及在配置期间启用了哪些选项而有很大的不同。 常用的GD函数 ImageCreate() : 创建一个基于调色板的图像 ImageColorAllocate() : 为一副图像分配颜色,很古怪的一点是: 第一次调用时
概述
php中绘制图形多使用gd库来实现,gd的功能多寡会因你使用的gd版本以及在配置期间启用了哪些选项而有很大的不同。
常用的GD函数
ImageCreate() : 创建一个基于调色板的图像
ImageColorAllocate() : 为一副图像分配颜色,很古怪的一点是:第一次调用时分配图像的背景色,后面的调用才是用来分配线条、图形等
ImagePNG() : 将GD图形流以PNG格式输出到标准输出(通常是浏览器),通过可选参数可以输出到文件
ImageCreateFromPNG() : 从本地的PNG文件或URL创建一个PNG图像
ImageCreateFromJPEG() : 从本地的JPG文件或URL创建一个JPG图像(URL创建需要php.ini的allow_url_fopen设置为true)
ImageLine() :画线
ImageFilledRectangle() : 画一个矩形并填充颜色
ImagePolygon() : 画一个多边形
ImageFilledPolygon() : 画一个多边形
ImageArc() : 画一个椭圆弧
ImageFillToBorder() : 区域填充到边界 (如无边界色,则整幅图像将被填充)
ImageSetStyle() : 设定划线的样式,具体介绍请参考另一篇博文(GD库之有意思的imagesetstyle)
ImageString() : 水平的画字符串(可设字符大小,无法设字体。坐标基点是左上)
ImageTTFText() : 用 TrueType 字体向图像写入文本(.ttf字体文件是安装在服务器上的,坐标基点左下)
ImagePSText() : 用 PostScript Type1 字体把文本字符串画在图像上(字符串的坐标是右上)
ImageStringUp() : 垂直的画一行字符串(逆时针90度)
imagecreatetruecolor() : 创建一个指定大小的黑色背景真彩色图像
ImageColorTransparent() : 将莫颜色定义为透明色
ImageColorsForIndex() : 取得图像中某个点的red,green,blue 和 alpha 的键名的关联数组
ImageSX() : 获取图像X轴长度
ImageSY() : 获取图像的Y轴长度
ImagePSBBox() : 获取PostScript Type1字符串的左下和右上坐标
ImageTTFBBox() : 获取TureType字符串左下、右下、右上、左上4点坐标
不同字体库对基点的定义
内建字符函数基点:左上
TureType字体基点:左下
PostScript Type1基点:右下
立即学习“PHP免费学习笔记(深入)”;
创建居中的文字

针对 PHP 内建字体
function pc_ImageStringCenter($image, $text, $font) {
//字号对应的宽和高
$width = array(1 => 5, 6, 7, 8, 9);
$height = array(1 => 6, 8, 13, 15, 15);
// 获取图片的宽和高
$xi = ImageSX($image);
$yi = ImageSY($image);
// 获取文字的长度和高度
$xr = $width[$font] * strlen($text);
$yr = $height[$font];
// 计算中心点
$x = intval(($xi - $xr) / 2);
$y = intval(($yi - $yr) / 2);
return array($x, $y);
}
针对 PostScript 字体
function pc_ImagePSCenter($image, $text, $font, $size, $space = 0,
$tightness = 0, $angle = 0) {
// 获取图片的长宽
$xi = ImageSX($image);
$yi = ImageSY($image);
// 获取字符串左下和右上的坐标
list($xl, $yl, $xr, $yr) = ImagePSBBox($text, $font, $size,
$space, $tightness, $angle);
// 计算中心点
$x = intval(($xi - $xr) / 2);
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
针对 TrueType 字体
function pc_ImageTTFCenter($image, $text, $font, $size) {
// 计算图片长宽
$xi = ImageSX($image);
$yi = ImageSY($image);
// 计算字符串左右上下4个点的坐标
$box = ImageTTFBBox($size, $angle, $font, $text);
$xr = abs(max($box[2], $box[4]));//右上、右下角的X位置
$yr = abs(max($box[5], $box[7]));//右上、右下角的Y位置
// 计算中心点
$x = intval(($xi - $xr) / 2);
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
绘制图形
这个例子用到了常用的GD函数

$x,'Y'=>$y);
}
imageline($image, $point[0]['X'],$point[0]['Y'], $point[1]['X'],$point[1]['Y'],$color);
imageline($image, $point[1]['X'],$point[1]['Y'], $point[2]['X'],$point[2]['Y'],$color);
imageline($image, $point[2]['X'],$point[2]['Y'], $point[3]['X'],$point[3]['Y'],$color);
imageline($image, $point[3]['X'],$point[3]['Y'], $point[4]['X'],$point[4]['Y'],$color);
imageline($image, $point[4]['X'],$point[4]['Y'], $point[0]['X'],$point[0]['Y'],$color);
//五角星外边
imageline($image, $point[0]['X'],$point[0]['Y'], $point[2]['X'],$point[2]['Y'],$color);
imageline($image, $point[1]['X'],$point[1]['Y'], $point[3]['X'],$point[3]['Y'],$color);
imageline($image, $point[2]['X'],$point[2]['Y'], $point[4]['X'],$point[4]['Y'],$color);
imageline($image, $point[3]['X'],$point[3]['Y'], $point[0]['X'],$point[0]['Y'],$color);
imageline($image, $point[4]['X'],$point[4]['Y'], $point[1]['X'],$point[1]['Y'],$color);
header('Content-type:image/png');
imagepng($image);
?>
读取EXIF数据
通过PHP的内置函数即可获取exif的数据
获取到的数据是:
Array
(
[FileName] => beth-and-seth.jpg
[FileDateTime] => 1096055414
[FileSize] => 182080
[FileType] => 2
[MimeType] => image/jpeg
[divsFound] => APP12
[COMPUTED] => Array
(
[html] => width="642" height="855"
[Height] => 855
[Width] => 642
[IsColor] => 1
)
[Company] => Ducky
[Info] =>
)










