Heim > php教程 > php手册 > Hauptteil

PHP Cookbook读书笔记 – 第17章图形

WBOY
Freigeben: 2016-06-06 19:40:52
Original
1702 Leute haben es durchsucht

概述 PHP中绘制 图形 多使用GD库来实现,GD的功能多寡会因你使用的GD版本以及在配置期间启用了哪些选项而有很大的不同。 常用的GD函数 ImageCreate() : 创建一个基于调色板的图像 ImageColorAllocate() : 为一副图像分配颜色,很古怪的一点是: 第一次调用时

概述

PHP Cookbook读书笔记 – 第17章图形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() : 画一个矩形并填充颜色

ImageRectangle() : 画一个矩形
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 Cookbook读书笔记 – 第17章图形

针对 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);
}
Nach dem Login kopieren


针对 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);
}
Nach dem Login kopieren


针对 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);
}
Nach dem Login kopieren

绘制图形

这个例子用到了常用的GD函数

PHP Cookbook读书笔记 – 第17章图形

<?php $image = ImageCreate(500,500);
$background_color= imagecolorallocate($image, 255,255,255);
$grayline = imagecolorallocate($image,230,230,230);
$color = imagecolorallocate($image, 200,0,0);
//imagefilledrectangle($image, 50, 10, 150, 40, $gray);
for($i=5;$i<500;$i=$i+5)
{
	//横线
	imageline($image, 5,$i,495,$i,$grayline);
	//纵线
	imageline($image,$i,5,$i,495,$grayline);
}
//正三角
$points = array(20,20,20,200,200,200);
imagefilledpolygon($image, $points,3, $color);
//倒三角
$points2 = array(22,20,202,20,202,200);
imagefilledpolygon($image, $points2,3, $color);

//椭圆
imagesetstyle($image,array($color));
imagearc($image, 300,100, 20,100, 0,360, $color);
imagearc($image, 300,100, 40,100, 0,360, $color);
//正圆
imagearc($image, 300,100, 100,100, 0,360, $color);

//输出文字
imagestring($image,8,20,300,"I love PHP language.",imagecolorallocate($image,0,0,50));
imagestring($image,4,300,480,"image size:500px *500px",imagecolorallocate($image,0,0,255));

//画五角星
$point = array();
for($i=0;$i<6;$i++)
{
    //(int)(cX*(0.50+0.5*Math.Cos(dAngle))), (int)(cY*(0.50+0.5*Math.Sin(dAngle))) ,dAngle=(i*0.8-0.5)*Math.PI;
    $dAngle = ($i*0.8-0.5)*pi();
    $x = intval(200*(0.5+0.5*cos($dAngle)))+300;
    $y = intval(200*(0.5+0.5*sin($dAngle)))+300;
    $point[$i] = array('X'=>$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);
?>
Nach dem Login kopieren

读取EXIF数据

通过PHP的内置函数即可获取exif的数据

<?php $exif = exif_read_data('/beth-and-seth.jpeg');
print_r($exif);
?>
Nach dem Login kopieren


获取到的数据是:

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] =>
)
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!