在PHP中创建包含多个元素的图像

时间:2010-11-29 13:07:00

标签: php gd

我有一个对象数组(图像,文本或矩形),我正在尝试用它们创建一个图像。但在我看来,无论何时插入其中一个元素,前面的元素都会被删除。这是我的代码:

if(isset($_GET['elements'])){
    $elements = json_decode(stripslashes($_GET['elements']));
    $gfx = imagecreate(160,120);
    $width = 160;
    $height = 120;
    $white = imagecolorallocate($gfx, 255, 255, 255);
    $id = $_GET['id'];
    $username=$_GET['username'];
    $name=$_GET['name'];
    foreach ($elements as $element){
        $type = $element->type;
        switch($type){
            case 'textBox':
                text_thumb( $element,$gfx);
                break;
            case 'image':
            image_thumb( $element,$gfx,$name,$username);
            break;
            case 'rectangle':
                box_thumb( $element,$gfx);
                break;
        }
    }
    header('Content-type: image/png');
    imagepng($gfx,'../thumbnail/'.$username.'_'.$name.'_'.$id.'.png');

}

function text_thumb ($element,$gfx){
    function wrap($fontSize, $angle, $fontFace, $string, $width){
        $ret = "";
        $arr = explode(' ', $string);
        foreach ( $arr as $word ){
            $teststring = $ret.' '.$word;
            $testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
            if ( $testbox[4]-$testbox[0] > $width ){
                $ret.=($ret==""?"":"\n").$word;
            } else {
                $ret.=($ret==""?"":' ').$word;
            }
        }  
        return $ret;
    }
    $width = $element->width/5;
    $left =$element->left/5;
    $top =$element->top/5;
    $string = $element->text;
    $fontsize = 2.4;
    $font = "../fonts/arial.TTF";
    $fonth = imagefontheight($fontsize);
    $text = wrap($fontsize, 0, $font, $string, $width);
    imagettftext($gfx,$fontsize,0,$left,$top,$black,$font,$text);
}

function box_thumb ($element,$gfx){
    $width = $element->width/5;
    $height = $element->height/5;
    $left =$element->left/5;
    $top =$element->top/5;
    $x2=$left+$width;
    $y2=$top+$height;
    $black = imagecolorallocate($gfx, 0, 0, 0);
    imagefilledrectangle($gfx,$left, $top,$x2,$y2,$black);
}

function image_thumb ($element,$gfx,$name,$username){
    $height = $element->height;
    $width = $element->width;
    $left =$element->left/5;
    $top =$element->top/5;
    $src =$element->src;
    $extype = explode(".", $src);
    $type=$extype[1];
    if($type=="jpg"||$type=="JPG")$type="jpeg";
    $creat="imagecreatefrom".$type;
    $insert=$creat("../user_pics/view/{$username}_{$name}_{$src}");
    imagecopyresampled($gfx, $insert, $left, $top, 0, 0, $width/5, $height/5, $width, $height);
}

代码用作web服务,这里是$ elements数组的一个例子:

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => stdClass Object
        (
            [src] => 019.png
            [id] => 7
            [type] => image
            [width] => 635
            [height] => 205
            [top] => 395
            [left] => 84
            [page] => 2
        )

    [8] => stdClass Object
        (
            [type] => rectangle
            [top] => 33
            [left] => 90
            [page] => 2
            [width] => 602
            [height] => 128
            [id] => 8
        )

    [9] => stdClass Object
        (
            [type] => textBox
            [top] => 182
            [left] => 171
            [page] => 2
            [width] => 539
            [height] => 154
            [id] => 9
            [text] => SINA
        )

)

我是否必须使用imagecopymerge或者是否有更简单的方法,考虑到图像中有可变数量的元素?

***更新:

我想如果我删除“case'图像':...”部分,即只是从框和文本创建它就可以正常工作,而且如果我的数组中有多个图像,它包含所有这些图像这是否正确,因为“imagecopyresampled”?

2 个答案:

答案 0 :(得分:0)

我认为$ gfx变量存在范围问题。 尝试从每个内部函数返回它,然后再将它传递到你需要的任何地方。

function text_thumb ($element,$gfx){
    // do stuff
    return $gfx;
}

答案 1 :(得分:0)

好吧,我想,它就像Frode说的那样工作,但问题是由于某些原因它没有将颜色传递给函数,所以它会画出盒子,但是是白色的,因为背景是白色,你可以看到框,所以我把代码更改为:

if(isset($_GET['elements'])){
    $elements = json_decode(stripslashes($_GET['elements']));
    $gfx = imagecreate(160,120);
    $width = 160;
    $height = 120;
    $white = imagecolorallocate($gfx, 255, 255, 255);
    $black = imagecolorallocate($gfx, 0, 0, 0);
    $id = $_GET['id'];
    $username=$_GET['username'];
    $name=$_GET['name'];
    foreach ($elements as $element){
        $type = $element->type;
        switch($type){
            case 'textBox':
                text_thumb( $element,$gfx,$white);
                break;
            case 'image':
                image_thumb( $element,$gfx,$name,$username);
                break;
            case 'rectangle':
                box_thumb( $element,$gfx,$black);
                break;
        }
    }
    header('Content-type: image/png');
    imagepng($gfx,'../thumbnail/'.$username.'_'.$name.'_'.$id.'.png');
}

其余的很漂亮......

相关问题