title: creating an image with multiple elements in PHP
body: <p>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 :</p>
  
  <pre><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-&gt;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] &gt; $width ){
                  $ret.=($ret==""?"":"\n").$word;
              } else {
                  $ret.=($ret==""?"":' ').$word;
              }
          }  
          return $ret;
      }
      $width = $element-&gt;width/5;
      $left =$element-&gt;left/5;
      $top =$element-&gt;top/5;
      $string = $element-&gt;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-&gt;width/5;
      $height = $element-&gt;height/5;
      $left =$element-&gt;left/5;
      $top =$element-&gt;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-&gt;height;
      $width = $element-&gt;width;
      $left =$element-&gt;left/5;
      $top =$element-&gt;top/5;
      $src =$element-&gt;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);
  }
  </code></pre>
  
  <p>The code is used as a webservice, here is an example of the $elements array:</p>
  
  <pre><code>Array
  (
      [0] =&gt; 
      [1] =&gt; 
      [2] =&gt; 
      [3] =&gt; 
      [4] =&gt; 
      [5] =&gt; 
      [6] =&gt; 
      [7] =&gt; stdClass Object
          (
              [src] =&gt; 019.png
              [id] =&gt; 7
              [type] =&gt; image
              [width] =&gt; 635
              [height] =&gt; 205
              [top] =&gt; 395
              [left] =&gt; 84
              [page] =&gt; 2
          )
  
      [8] =&gt; stdClass Object
          (
              [type] =&gt; rectangle
              [top] =&gt; 33
              [left] =&gt; 90
              [page] =&gt; 2
              [width] =&gt; 602
              [height] =&gt; 128
              [id] =&gt; 8
          )
  
      [9] =&gt; stdClass Object
          (
              [type] =&gt; textBox
              [top] =&gt; 182
              [left] =&gt; 171
              [page] =&gt; 2
              [width] =&gt; 539
              [height] =&gt; 154
              [id] =&gt; 9
              [text] =&gt; SINA
          )
  
  )
  </code></pre>
  
  <p>do I have to use imagecopymerge or is there any easier way, considering that I have a variable number of elements in the image ?</p>
  
  <p>***UPDATE :</p>
  
  <p>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" ?</p>