首页 > Web开发 > 详细

PHP动态图像处理

时间:2016-01-26 15:02:57      阅读:165      评论:0      收藏:0      [点我收藏+]

PHP中GD库的使用

        在PHP中,通过GD库处理图像的操作,都是现在内存中处理,操作完成以后再以文件流的方式,输出到浏览器或保存在服务器的磁盘中。创建一个图象应该包括4个基本步骤:
  • 创建画布:画布实际上就是在内存中开辟的一块临时区域,用于存储图像的信息。
  • 绘制图像:设置图像的颜色,填充点、线、几何图形、文本等。
  • 输出图像:完成绘制后,需要将图像以某种格式保存到服务器指定的文件中,或将图像直接输出到浏览器上显示给用户。但在图像输出之前,一定要使用header()函数发送Content-type通知浏览器。
  • 释放资源:图像被输出以后,话不中的内容也不再有用。处于节约系统资源的考虑,需要及时清除画布占用的所有内存资源。
画布管理
        可以使使用imagecreate()和imageCreateTrueColor()两个函数插件指定的画布。
        imagecreate():新建一个基于调色板的图像。
        imagecreatetruecolor():新建一个真彩色图像。
        画布的句柄如果不再使用,一定要将这个资源销毁,释放内存与该图像的存储单元。调用imagedestroy($image)函数就可以实现。
设置颜色

        imagecolorallocate(resouce $image,int $red,int $green,int $blue);

 

1
2
3
4
5
6
7
8
9
10
<?php
    $im = imagecreate(100, 100);                            //为设置颜色函数提供一个画布资源
    //背景设为红色
    $background = imagecolorallocate($im, 255, 0, 0);       //第一次调用即为画布设置背景颜色
    //设定一些颜色
    $white = imagecolorallocate($im, 255, 255, 255);        //返回由十进制整数设置为白色的标识符
    $black = imagecolorallocate($im, 0, 0, 0);              //返回由十进制整数设置为黑色的标识符
    //十六进制方式
    $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);     //返回由十六进制整数设置为白色的标识符
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);     //返回由十六进制整数设置为黑色的标识符
生成图像

        bool imagegif()

        bool imagejpeg()

        bool imagepng()

        boole imagewbmp()

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
    if (function_exists("imagegif")){
        header("Content-type:image/gif");
        imagegif($image);
    }elseif (function_exists("imagejpeg")){
        header("Content-type:image/jpeg");
        imagejpeg($image, "", 0.5);
    }elseif (function_exists("imagepng")){
        header("Content-type:imagepng");
        imagepng($image);
    }elseif (function_exists("imagewbmp")){
        header("Content-type:imagewbmp");
        imagewbmp($image);
    }else{
        die("在PHP服务中,不支持图像");
    }
绘制图像

        imageFill()实现区域填充。

 

1
imagefill(resource $image,int  $x,int $y,int $color);

 

1
2
3
4
5
6
7
8
<?php
    $image = imagecreatetruecolor(100, 100);
    $red = imagecolorallocate($image, 255, 0, 0);
     
    imagefill($image, 0, 0, $red);
    header("Content-type:image/png");
    imagepng($image);
    imagedestroy($image);

 

  •  绘制点:在php中,使用imageSetPixel()函数在画布中绘制一个单一像素的点,并且可以设置点的颜色。

 

 

1
bool imagesetpixel(resource $image, int $x, int $y, int $color);

 

  • 绘制线:imageline()绘制一条线段。

 

 

1
bool imageline(resource $image, int $x1, int $y1,int $x2, int $y2, int $color);

 

  •  绘制矩形: imageRectangle(),也可以使用imageFilledRectangle()函数绘制一个矩形并填充。

 

1
2
bool imageRectangle(resource $image, int $x1, int $y1,int $x2, int $y2, int $color);
bool imageFilledRectangle(resource $image, int $x1, int $y1,int $x2, int $y2, int $color);

 

  • 绘制多边形:  imagepolygon(),imagefilledploygon()。

 

 

1
2
bool imageploygon(resource $image, array $points, int $num_points, int $color);
bool imageFilledploygon(resource $image, array $points, int $num_points, int $color);

 

  • 绘制椭圆:imageEllipse()、imageFilledEllipse()函数绘制一个椭圆并填充。

 

 

1
2
bool imageellipse(resource $image, int $cx, int $cy,int $w, int $h, int $color);
bool imagefilledellipse(resource $image, int $cx, int $cy,int $w, int $h, int $color);

 

  • 绘制弧线:imagearc

 

 

1
bool imagearc(resource $image, int $cx, int $cy,int $width, int $height, int s, int $e, int $color);

在图像中绘制文字

        


        






PHP动态图像处理

原文:http://www.cnblogs.com/staven/p/d3ef425dd49f550b9757bfa758f3a842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!