creating an image with multiple elements in PHP
I have an array of objects ( image, text or rectangle) and I'm trying to create an image out of them. But it seems to me that whenever I insert one of the elements, the previous elements get wiped out. here is my code :
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,开发者_如何学JAVA$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);
}
The code is used as a webservice, here is an example of the $elements array:
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
)
)
do I have to use imagecopymerge or is there any easier way, considering that I have a variable number of elements in the image ?
***UPDATE :
I figured that if I remove the "case 'image': ..." part , i.e. just creating it from box and text it works fine, and also if I have multiple images in my array, it includes all of them correctly, so could it be because of "imagecopyresampled" ?
I believe there is a scope issue with the $gfx variable. Try returning it from each internal function and then pass it around again to wherever you need it.
function text_thumb ($element,$gfx){
// do stuff
return $gfx;
}
Alright I figured it, it was working as Frode said, but the problem was that it didn't pass the colors to the functions for some reason, so it would draw the box, but in white, and because the background was white as well, you could see the box, so I changed the code to this :
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');
}
and the rest pretty is much the same ...
精彩评论