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);
?>

服务器下访问,效果如下: 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值