白森 发表于 2017-3-20 13:03:51

PHP使用图像

PHP使用图像
  首先了解一下PHP使用图像的流程:
  (1):创建画布  ImageCreate()
  (2):定义颜色 ImageColorAllocate()
  (3):绘制形状和线条
  ImageEllipse()        绘制一个椭圆
  ImageArc()             绘制一个部分椭圆
  ImagePolygon()      绘制一个多边形
  ImageRectangle()  绘制一个矩形
  ImageLine()           绘制一个线条
  (4):发送到浏览器
  (5):清除内存
  熟悉绘制流程:

<?php
//create the canvas
$myImage = imagecreate(150, 150);
//set up some colors
$black = imagecolorallocate($myImage, 0, 0, 0);
$white = imagecolorallocate($myImage, 255, 255, 255);
$red = imagecolorallocate($myImage, 255, 0, 0);
$green = imagecolorallocate($myImage, 0, 255, 0);
$blue = imagecolorallocate($myImage, 0, 0, 255);
//draw some rectangles
imagerectangle($myImage, 15, 15, 55, 85, $red);
imagerectangle($myImage, 55, 85, 125, 135, $white);
//output the image to the browser
//把图像数据的流输出到Web浏览器,首先使用所创建图像的MIME类型来发送相应的
//header()函数。然后,使用ImageGif()、ImageJpeg()或ImagePng()
//函数相应地输出数据流。
header("Content-type:image/png");
imagepng($myImage);
//clean up after yourself
//使用imagedestroy()函数清空imagecreate()函数在脚本开头所使用的内存
imagedestroy($myImage);
?>
  服务器下访问,效果如下:

 例子:PHP绘制3d饼图:

<?php
//create the canvas
$myImage = imagecreate(300, 300);
//set up some colors
$white = imagecolorallocate($myImage, 255, 255, 255);
$red = imagecolorallocate($myImage, 255, 0, 0);
$green = imagecolorallocate($myImage, 0, 255, 0);
$blue = imagecolorallocate($myImage, 0, 0, 255);
$lt_red = imagecolorallocate($myImage, 255, 150, 150);
$lt_green = imagecolorallocate($myImage,150, 255,150);
$lt_blue = imagecolorallocate($myImage, 150, 150, 255);
//draw the shaded area
for ($i=120;$i>100;$i--){
imagefilledarc($myImage, 100, $i, 200, 150, 0, 90, $lt_red, IMG_ARC_PIE);
imagefilledarc($myImage, 100, $i, 200, 150, 90, 180, $lt_green, IMG_ARC_PIE);
imagefilledarc($myImage, 100, $i, 200, 150, 180, 360, $lt_blue, IMG_ARC_PIE);
}
//draw a pie
imagefilledarc($myImage, 100, 100, 200, 150, 0,90,$red, IMG_ARC_PIE);
imagefilledarc($myImage, 100, 100, 200, 150, 90,180,$green, IMG_ARC_PIE);
imagefilledarc($myImage, 100, 100, 200, 150, 180,360,$blue, IMG_ARC_PIE);

//output the image to the browser
//把图像数据的流输出到Web浏览器,首先使用所创建图像的MIME类型来发送相应的
//header()函数。然后,使用ImageGif()、ImageJpeg()或ImagePng()
//函数相应地输出数据流。
header("Content-type:image/png");
imagepng($myImage);
//clean up after yourself
//使用imagedestroy()函数清空imagecreate()函数在脚本开头所使用的内存
imagedestroy($myImage);
?>
  服务器下访问,效果如下: 

 
页: [1]
查看完整版本: PHP使用图像