$textarrs = imagettfbbox( $this->size, 0, dirname( __FILE__ ) . "/" . $this->font, $this->text );print_r($textarrs);打印出来有8个数据要计算 $this->text  宽 高 怎么写

解决方案 »

  1.   

    magettfbbox() returns an array with 8 elements representing four points making the bounding box of the text on success and FALSE on error.key contents
    0 lower left corner, X position
    1 lower left corner, Y position
    2 lower right corner, X position
    3 lower right corner, Y position
    4 upper right corner, X position
    5 upper right corner, Y position
    6 upper left corner, X position
    7 upper left corner, Y position
      

  2.   

        private function position( $num, $width, $height ){
            switch ( $num ) {
                case 1:
                    $positionarr[ 0 ] = 0;
                    $positionarr[ 1 ] = 0;
                    break;
                case 2:
                    $positionarr[ 0 ] = ( $this->width - $width ) / 2;
                    $positionarr[ 1 ] = 0;
                    break;
                case 3:
                    $positionarr[ 0 ] = $this->width - $width;
                    $positionarr[ 1 ] = 0;
                    break;
                case 4:
                    $positionarr[ 0 ] = 0;
                    $positionarr[ 1 ] = ( $this->height - $height ) / 2;
                    break;
                case 5:
                    $positionarr[ 0 ] = ( $this->width - $width ) / 2;
                    $positionarr[ 1 ] = ( $this->height - $height ) / 2;
                    break;
                case 6:
                    $positionarr[ 0 ] = $this->width - $width;
                    $positionarr[ 1 ] = ( $this->height - $height ) / 2;
                    break;
                case 7:
                    $positionarr[ 0 ] = 0;
                    $positionarr[ 1 ] = $this->height - $height;
                    break;
                case 8:
                    $positionarr[ 0 ] = ( $this->width - $width ) / 2;
                    $positionarr[ 1 ] = $this->height - $height;
                    break;
                case 9:
                    $positionarr[ 0 ] = $this->width - $width;
                    $positionarr[ 1 ] = $this->height - $height;
                    break;
                case 0:
                    $positionarr[ 0 ] = rand( 0, $this->width - $width );
                    $positionarr[ 1 ] = rand( 0, $this->height - $height );
                    break;
            }
            return $positionarr;
        }        $bbox = imagettfbbox( $this->size, 0, dirname( __FILE__ ) . "/" . $this->font, $this->text );        $width = $bbox[ 2 ] - $bbox[ 0 ] ;
            $height =  $bbox[ 1 ] - $bbox[ 7 ] ;        # 计算水印加载的位置
            $positionarr = $this->position( $index, $width, $height );imagettftext( $this->image, $this->size, 0, $positionarr[ 0 ], $positionarr[ 1 ], $color, dirname( __FILE__ ) . "/" . $this->font, $this->text );
     
    在$num = 1,2,3 时在图片上都看不到写入的水印文字456789 没问题
      

  3.   

            $index = mt_rand(1,3);
            $this->text = "test" . $index;
      

  4.   

    8个数据,2个为一组
    分别表示 左下、右下、右上、左上 的坐标(x,y)
    所以
    文字的高度可以是:
    $d[7] - $d[1]

    $d[5] - $d[3]文字的宽度可以是:
    $d[2] - $d[0]

    $d[4] - $d[6]