图像基类

<?php
/*
* 图形处理基类
*
* @Author:zx
* @Time:2006年3月17日
* @File:drawstat.php
* @version:1.0
*/

include_once( 'error.php' );

class graphics
{
    //图片信息数组
    private $image;
   
    //颜色信息数组
    private $color;
   
    //字体信息数组
    private $font;
   
    //当前页面编码
    private $encode;
   
    //image数据当前的指针
    private $point;
   
    //实例化错误对象指针
    private $error;
   
    public function __construct( $encoding = "gb2312" )
    {
        /*
        * 初始化函数
        *
        * @access public
        * @return Bool
        */
       
        $this->encode = $encoding;
        $this->error = &new error;
        $this->error->regOnceEx( 1 );
    }
   
    public function create( $id , $width , $height , $isTrue , $over=false )
    {
        /*
        * 创建图象
        *
        * @var void
        * @param string $id
        * @param int $width
        * @param int $height
        * @param bool $isTrue
        * @param bool $over
        * @access public
        * @return Bool
        */
       
        if( !$this->image[ $id ][ 'resource' ] || $over )
        {
            $this->image[ $id ][ 'resource' ] = $isTrue ? @imagecreatetruecolor( $width , $height ) : @imagecreate( $width , $height );
            $this->image[ $id ][ 'width' ] = $width;
            $this->image[ $id ][ 'height' ] = $height;
        }
       
        if( !$this->image[ $id ][ 'resource' ] )
        {
            unset( $this->image[ $id ] );
            $this->error->regError( 1 , '没有创建画布!' , __FILE__ ,'画布创建失败,服务器不支持GD!' , 'graphics',  __LINE__ );
            return( false );
        }
       
        $this->point = $id;
        return( true );
    }
   
    public function createBy( $id , $path , $over=false )
    {
        /*
        * 从图片文件中创建图象
        *
        * @param string $id
        * @param string $path
        * @param bool $over
        * @access public
        * @return Bool
        */
       
        if( !$this->image[ $id ]['resource'] || $over )
        {
            $info = getimagesize( $path );

            switch( $info[2] )
            {
                case 1:
                    $this->image[ $id ][ 'resource' ] = @imagecreatefromgif( $path );
                break;
                case 2:
                case 9:
                case 10:
                case 11:
                case 12:
                    $this->image[ $id ][ 'resource' ] = @imagecreatefromjpeg( $path );
                break;
                case 3:
                    $this->image[ $id ][ 'resource' ] = @imagecreatefrompng( $path );
                break;
                case 15:
                case 16:
                    $this->image[ $id ][ 'resource' ] = @imagecreatefromwbmp( $path );
                break;
                default:
                    $this->image[ $id ][ 'resource' ] = @imagecreatefromstring( $path );
            }
           
           
            $this->image[ $id ][ 'width' ] = $info[0];
            $this->image[ $id ][ 'height' ] = $info[1];
        }
       
        if( !$this->image[ $id ]['resource'] )
        {
            unset( $this->image[ $id ] );
            $this->error->regError( 1 , '没有创建图像!' , __FILE__ ,'从图像创建失败,请检查文件是否正确!' , 'graphics',  __LINE__ );
            return( false );
        }

        $this->point = $id;
        return( true );
    }
   
    public function clones($dstId,$srcId)
    {
        /*
        * 图象复制
        *
        * @param string $dstId
        * @param string $srcId
        * @access public
        * @return Bool
        */
       
        $this->image[ $srcId ] = $this->image[ $dstId ];
        $this->point = $srcId;
        return( true );
    }
   
    public function getInfo(  )
    {
        /*
        * 取得当前图片的相关信息
        *
        * @access public
        * @return Bool
        */
       
        return( $this->image[ $this->point ] );
    }
   
    private function colorRgb( $code , &$rgb )
    {
        /*
        * 将十六进制颜色代码转换成rgb代码
        *
        * @param string $code
        * @param array $rgb
        * @access private
        * @return Bool
        */
       
        $rgb['r'] = hexdec( substr( $code , 0 , 2 ) );
        $rgb['g'] = hexdec( substr( $code , 2 , 2 ) );
        $rgb['b'] = hexdec( substr( $code , 4 , 2 ) );
        return( true );
    }
   
   
    private function setColor( $color )
    {
        /*
        * 加载颜色
        *
        * @param string $color
        * @access private
        * @return Bool
        */
       
        $rgb = array();
        self::colorRgb( $color , $rgb );
        $this->color[ $color ] = @imagecolorallocate( $this->image[ $this->point ][ 'resource' ] , $rgb['r'] , $rgb['g'] , $rgb['b'] );
        return( true );
    }
   
    private function setAlphaColor( $color , $alpha=0 )
    {
        /*
        * 加载Alpha颜色
        *
        * @param string $color
        * @access private
        * @return Bool
        */
       
        $rgb = array();
        self::colorRgb( $color , $rgb );
        $this->color[ $color ] = @imagecolorallocatealpha( $this->image[ $this->point ][ 'resource' ] , $rgb['r'] , $rgb['g'] , $rgb['b'] , $alpha );
        return( true );
    }
   
    public function setPoint( $id )
    {
        /*
        * 设置图片资源指针指向该ID
        *
        * @param string $id
        * @access private
        * @return Bool
        */
       
        $this->point = $id;
        return( true );
    }
   
    public function getColor( $color , $alpha=NULL )
    {
        /*
        * 获取颜色代码
        *
        * @param string $color
        * @access public
        * @return Bool
        */
       
        if( !$this->color[ $color ] )
        {
            is_integer( $alpha )? self::setAlphaColor( $color , $alpha ) : self::setColor( $color );
        }
       
        return( $this->color[ $color ] );
    }
   
    public function style( &$styles )
    {
        /*
        * 设定所有线条的样式
        *
        * @param array $styles
        * @access public
        * @return Bool
        */
       
        $newStyles = array();
       
        for( $i=0 ; $styles[ $i ] ; $i++ )
        {
            $newStyles[] = self::getColor( $styles[ $i ] );
        }
       
        return( @imagesetstyle( $this->image[ $this->point ][ 'resource' ] , $newStyles ) );
    }

    public function thickNess( $width )
    {
        /*
        * 设定所有线条的宽度
        *
        * @param integer $width
        * @access public
        * @return Bool
        */
       
        return( @imagesetthickness( $this->image[ $this->point ][ 'resource' ] , $width ) );
    }
   
    public function drawLine( $x1 , $y1 , $x2 , $y2 , $color=NULL )
    {
        /*
        * 绘制坐标线条
        *
        * @param float $x1
        * @param float $y1
        * @param float $x2
        * @param float $y2
        * @access public
        * @return Bool
        */
       
        $color = isset( $color ) ? self::getColor( $color ) : IMG_COLOR_STYLED;
        return( @imageline( $this->image[ $this->point ][ 'resource' ] , $x1 , $y1 , $x2 , $y2 , $color ) );
    }
   
    public function fillGraph( $x , $y , $borderColor , $fillColor )
    {
        /*
        * 根据坐标填充封闭的图形
        *
        * @param float $x
        * @param float $y1
        * @param string $borderColor
        * @param string $fillColor
        * @access public
        * @return Bool
        */
       
        return( @imagefilltoborder( $this->image[ $this->point ][ 'resource' ] , $x , $y , self::getColor( $borderColor ) , self::getColor( $fillColor ) ) );
    }
   
    public function drawGraph( &$points , $color , $fill=false )
    {
        /*
        * 绘制任意图形
        *
        * @param array $points
        * @param array $styles
        * @param string $color
        * @param bool $fill
        * @access public
        * @return Bool
        */
       
        $fill ? imagefilledpolygon( $this->image[ $this->point ][ 'resource' ] , $points , count( $points )/2 , self::getColor( $color ) ) : imagepolygon( $this->image[ $this->point ][ 'resource' ] , $points , count( $points )/2 , self::getColor( $color ) );
       
        return( true );
    }
   
    public function drawArc( $center , $width , $height , $angle , $color=NULL )
    {
        /*
        * 绘制圆弧
        *
        * @param array $center
        * @param int $width
        * @param int $height
        * @param array $angle
        * @param string $color
        * @access public
        * @return Bool
        */
       
        if( $color )
        {
            $fill = IMG_ARC_PIE;
            $color = self::getColor( $color );
        }
        else
        {
            $fill = IMG_ARC_NOFILL;
        }
       
        return( @imagefilledarc( $this->image[ $this->point ][ 'resource' ] , $center['x'] , $center['y'] , $width , $height , $angle['start'] , $angle['end'] , $color , $fill ) );
    }
   
    public function drawRound( $x , $y , $w , $h , $color )
    {
        /*
        * 绘制圆
        *
        * @param int $x
        * @param int $y
        * @param int $w
        * @param int $h
        * @param string $color
        * @access public
        * @return Bool
        */
       
        return( @imagefilledellipse( $this->image[ $this->point ][ 'resource' ] , $x , $y , $w , $h , self::getColor( $color ) ) );
    }
   
    public function drawPixel( $color , $x , $y )
    {
        /*
        * 绘制一个像素点
        *
        * @param string $color
        * @param int $x
        * @param int $y
        * @access public
        * @return Bool
        */
       
        return( @imagesetpixel( $this->image[ $this->point ][ 'resource' ] , $x , $y , self::getColor( $color ) ) );
    }
   
    public function addFont( $fontId , $path )
    {   
        /*
        * 添加字体
        *
        * @param string $fontId , string $path
        * @access public
        * @return Bool
        */
       
        $this->font[ $fontId ][ 'path' ] = $path;
        return( true );
    }
   
    public function setText( $fontId , $size , $angle , $color  )
    {
        /*
        * 设置文本输出格式
        *
        * @param string $fontId
        * @param int $size
        * @param float $angle
        * @param string $color
        * @access public
        * @return Bool
        */
       
        $this->font[ $fontId ][ 'color' ] = $color;
        $this->font[ $fontId ][ 'size' ] = $size;
        $this->font[ $fontId ][ 'angle' ] = $angle;
        return( true );
    }
   
    public function writeText( $fontId , $x , $y , $text )
    {
        /*
        * 向图片中写入文字
        *
        * @param string $fontId , string $text
        * @access public
        * @return Bool
        */
       
        $text = iconv( $this->encode , "utf-8" , $text );
        return( @imagettftext( $this->image[ $this->point ][ 'resource' ] , $this->font[ $fontId ][ 'size' ] , $this->font[ $fontId ][ 'angle' ] , $x , $y , self::getColor( $this->font[ $fontId ][ 'color' ] ) , $this->font[ $fontId ][ 'path' ] , $text ) );
    }
   
    public function writeIMG( $id , $x , $y , $imgWidth , $imgHeight )
    {
        /*
        * 向图片中写入图片
        *
        * @param string $id,int $x,int $y,int $imgWidth,int $imgHeight
        * @access public
        * @return Bool
        */
       
        return( @imagecopyresampled( $this->image[ $this->point ][ 'resource' ] , $this->image[ $id ][ 'resource' ] , 0 , 0 , $x , $y , $imgWidth , $imgHeight , $this->image[ $id ][ 'width' ] , $this->image[ $id ][ 'height' ] ) );
    }
   
    public function setThumbn( $imgWidth , $imgHeight )
    {
        /*
        * 设置当前图片的缩略图
        *
        * @param int $imgWidth,int $imgHeight
        * @access public
        * @return Bool
        */
       
        $temp = $this->point;
       
        if( !self::create( $this->point."Thumbn" , $imgWidth , $imgHeight , true , true ) )
        {
            return( false );
        }
       
        if( !@imagecopyresampled( $this->image[ $this->point ][ 'resource' ] , $this->image[ $temp ][ 'resource' ] , 0 , 0 , 0 , 0  , $imgWidth , $imgHeight , $this->image[ $temp ][ 'width' ] , $this->image[ $temp ][ 'height' ] ) )
        {
            @imagedestroy( $this->image[ $this->point ][ 'resource' ] );
            $this->point = $temp;
            return( false );
        }
       
        $this->point = $temp;
        return( true );
    }
   
    public function showThumbn( $quality , $path="" , $isSave=false )
    {
        /*
        * 显示当前图片的缩略图
        *
        * @param int $quality,string $path
        * @access public
        * @return Bool
        */
       
        return( $isSave ? self::save( $this->point."Thumbn" , "jpeg" , $path , $quality ) : self::show( $this->point."Thumbn" , "jpeg" , $quality ) );
    }
   
    public function show( $id="" , $type , $other="" )
    {
        /*
        * 显示当前图片
        *
        * @param string $id,string $type,void $other
        * @access public
        * @return Bool
        */
       
        $id = $id ? $id : $this->point;

        switch( $type )
        {
            case "wbmp":
                @header("Content-type: ".image_type_to_mime(IMAGETYPE_WBMP));
                @image2wbmp( $this->image[ $id ][ 'resource' ] , "" , $other );
            break;
            case "gif":
                @header("Content-type: image/gif");
                @imagegif( $this->image[ $id ][ 'resource' ] );
            break;
            case "png":
                @header("Content-type: image/png");
                @imagepng( $this->image[ $id ][ 'resource' ] );
            break;
            default:
                @header("Content-type: image/jpeg");
                @imagejpeg( $this->image[ $id ][ 'resource' ] , "" , $other );
        }
       
        return( true );
    }
   
    public function save( $id="" , $type , $path , $other="" )
    {
        /*
        * 保存当前图片到文件
        *
        * @param string $id,string $type,string $path,void $other
        * @access public
        * @return Bool
        */
       
        $id = $id ? $id : $this->point;
       
        switch( $type )
        {
            case "wbmp":
                @image2wbmp( $this->image[ $id ][ 'resource' ] , $path , $other );
            break;
            case "gif":
                @imagepng( $this->image[ $id ][ 'resource' ] , $path );
            break;
            case "png":
                @imagepng( $this->image[ $id ][ 'resource' ] , $path );
            break;
            default:
                @imagejpeg( $this->image[ $id ][ 'resource' ] , $path , $other );
        }
       
        return( true );
    }
   
    public function showError(  )
    {
        return( $this->error->pross( ) );
    }

    public function __destruct(  )
    {
        foreach( $this->image as $image )
        {
            @imagedestroy( $image['resource'] );
        }
       
        $this->image = NULL;
        $this->color = NULL;
        $this->font = NULL;
        $this->point = NULL;
        $this->error = NULL;
    }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值