Home >php教程 >php手册 >PHP绘制3D图形

PHP绘制3D图形

WBOY
WBOYOriginal
2016-06-06 19:46:421105browse

PEAR提供了Image_3D Package来创建3D图像。图像或光线在3D空间中按照X、Y 、Z 坐标定位。生成的图像将呈现在2D空间中,可以存储为 PNG、SVG 格式,或输出到Shell。通过Image_3D可以很方便生成一些简单的3D对象,例如立方体、锥体、球体、文本和饼图。查了一

       PEAR提供了Image_3D Package来创建3D图像。图像或光线在3D空间中按照X、Y 、Z 坐标定位。生成的图像将呈现在2D空间中,可以存储为 PNG、SVG 格式,或输出到Shell。通过Image_3D可以很方便生成一些简单的3D对象,例如立方体、锥体、球体、文本和饼图。查了一下PEAR文档关于Image_3D的实例一个也没有~囧~,只好自己研究一下,将经验分享给大家,请多多拍砖。言归正传,Image_3D需要PEAR和GD(配置php.ini)的支持。

1. 安装Image_3D

下载Image_3D Package ,目前最新版本为:0.4.1 alpha

 

官方下载:http://pear.php.net/package/Image_3D/download

将压缩包放到有pear.bat的目录中,例如:c:\php,若是WAMP5的话则为c:\wamp\php。

执行 pear install Image_3D-0.4.1.gz  安装Package:

PHP绘制3D图形

安装完毕,Image_3D会在 C:\wamp\php\PEAR\Image 目录中。

 

2. 3D坐标系

Image_3D的坐标系及旋转请参考下图,用于缩放、旋转或移动物体:

PHP绘制3D图形

3. 绘制圆锥体

代码如下:

<span><?php </span><span>//调用3D图形类
</span><span>require_once</span><span>(</span><span>'Image/3D.php'</span><span>)</span>;

<span>//创建一个三维空间
</span><span>$image </span><span>= </span><span>new </span>Image_3D<span>()</span>;
<span>$image</span><span>-></span>setColor<span>(</span><span>new </span>Image_3D_Color<span>(</span><span>255</span><span>, </span><span>255</span><span>, </span><span>255</span><span>))</span>;

<span>//在坐标系左下方创建一个红色光点
</span><span>$light1 </span><span>= </span><span>$image</span><span>-></span>createLight<span>(</span><span>"Light"</span><span>,</span><span>array</span><span>(-</span><span>100</span><span>, </span><span>100</span><span>, -</span><span>100</span><span>))</span>;
<span>$light1</span><span>-></span>setColor<span>(</span><span>new </span>Image_3D_Color<span>(</span><span>255</span><span>, </span><span>0</span><span>, </span><span>0</span><span>))</span>;

<span>//在坐标系右上方创建一个绿色光点
</span><span>$light2 </span><span>= </span><span>$image</span><span>-></span>createLight<span>(</span><span>"Light"</span><span>,</span><span>array</span><span>(</span><span>100</span><span>, -</span><span>200</span><span>, -</span><span>50</span><span>))</span>;
<span>$light2</span><span>-></span>setColor<span>(</span><span>new </span>Image_3D_Color<span>(</span><span>0</span><span>, </span><span>200</span><span>, </span><span>0</span><span>))</span>;

<span>//创建3D圆锥体
</span><span>$cone </span><span>= </span><span>$image</span><span>-></span>createObject<span>(</span><span>'cone'</span><span>,</span><span>array</span><span>(</span><span>'detail'</span>=><span>360</span><span>))</span>;
<span>$cone</span><span>-></span>setColor<span>(</span><span>new </span>Image_3D_Color<span>(</span><span>255</span><span>, </span><span>255</span><span>, </span><span>255</span><span>))</span>;
<span>//按X,Y,Z轴缩放
</span><span>$cone</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'scale'</span><span>, </span><span>array</span><span>(</span><span>80</span><span>, </span><span>150</span><span>, </span><span>80</span><span>)))</span>;
<span>//按X,Y,Z轴旋转
</span><span>$cone</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'rotation'</span><span>, </span><span>array</span><span>(</span><span>15</span><span>, </span><span>0</span><span>, </span><span>20</span><span>)))</span>;
<span>//按X,Y,Z轴移动
</span><span>$cone</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'move'</span><span>, </span><span>array</span><span>(</span><span>0</span><span>, -</span><span>50</span><span>, </span><span>0</span><span>)))</span>;

<span>//创建2D输出图像
//渲染效果,Image_3D共提供了种效果:Isometric,Perspectively,Raytrace
//参考...\PEAR\Image\3D\Renderer目录
</span><span>$image</span><span>-></span>createRenderer<span>(</span><span>'perspectively'</span><span>)</span>;
<span>//图像驱动,也可以SVG,ASCII等
//参考...\PEAR\Image\3D\Driver目录
</span><span>$image</span><span>-></span>createDriver<span>(</span><span>'gd'</span><span>)</span>;
<span>//创建输出图片名称及大小
</span><span>$image</span><span>-></span>render<span>(</span><span>400</span><span>, </span><span>300</span><span>, </span><span>'object.png'</span><span>)</span>;
<span>//显示效果图
</span><span>echo </span><span>'<img  src="object.png" alt="PHP绘制3D图形" >'</span>;
<span>?></span></span>

效果图:

PHP绘制3D图形

4. 3D文字效果

将上例创建3D圆锥体部分代码替换为:

<span>//创建3D字体
</span><span>$text </span><span>= </span><span>$image</span><span>-></span>createObject<span>(</span><span>'text'</span><span>,</span><span>'cnblogs'</span><span>)</span>;
<span>$text</span><span>-></span>setColor<span>(</span><span>new </span>Image_3D_Color<span>(</span><span>255</span><span>, </span><span>255</span><span>, </span><span>255</span><span>))</span>;
<span>$text</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'scale'</span><span>, </span><span>array</span><span>(</span><span>10</span><span>, </span><span>10</span><span>, </span><span>20</span><span>)))</span>;
<span>$text</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'rotation'</span><span>, </span><span>array</span><span>(</span><span>20</span><span>, </span><span>0</span><span>, </span><span>20</span><span>)))</span>;
<span>$text</span><span>-></span>transform<span>(</span><span>$image</span><span>-></span>createMatrix<span>(</span><span>'move'</span><span>, </span><span>array</span><span>(-</span><span>150</span><span>, -</span><span>100</span><span>, </span><span>0</span><span>)))</span>;

效果图:

PHP绘制3D图形

源代码下载:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn