title: Adding an image into another image
body: <p>I'm trying to add an image to a template which is another image too; I'm using PHP codeigniter ; I have got a nice snippet of code that works well for this purpose when both files are .PNG files; my code is as following:</p>
  
  <pre><code>&lt;?php
  
  function attachIcon($imgname)
  {
      $mark = imagecreatefrompng($imgname);
   imagesavealpha($mark, true);
  
      list($icon_width, $icon_height) = getimagesize($imgname);
  
      $img = imagecreatefrompng('images/sprites/navIcons.png');
   imagesavealpha($img, true);
  
      $move_left = 10;
      $move_up = 9;
  
      list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
      imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
      imagepng($img);  // display the image + positioned icon in the browser
    //imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
  }
  
  header('Content-Type: image/png');
   attachIcon('icon.png');
  ?&gt;
  </code></pre>
  
  <p>However if the template 'images/sprites/navIcons.png' is a png file and the other image is another type (lets say .JPG) my follwoing code is not working:</p>
  
  <pre><code>&lt;?php
  
  function attachIcon($imgname)
  {
      $mark = imagecreatefrompng($imgname);
   imagesavealpha($mark, true);
  
      list($icon_width, $icon_height) = getimagesize($imgname);
  
      $img = imagecreatefrompng('images/sprites/navIcons.png');
   imagesavealpha($img, true);
  
      $move_left = 10;
      $move_up = 9;
  
      list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
      imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
      imagepng($img);  // display the image + positioned icon in the browser
    //imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
  }
  
  header('Content-Type: image/jpg');
   attachIcon('icon.jpg');
  ?&gt;
  </code></pre>
  
  <blockquote>
    <p>-Am I missing anything? 
    - Is it supposed to work with any file extensions? 
    - can the template be .PNG file and the other image another type (lets say .JPG)!May be I need to change something;</p>
  </blockquote>
  
  <p>Any help is appreciated!</p>
  
  <p>PS: I got the code from a very old post in this forum ; but I thought it was pretty old and no one checks it, so thats why I ask my question in a new thread! Thanks</p>
  
  <hr>
  
  <p>Thanks sinni800,</p>
  
  <p>I was supposed to reply to you in comments, but since I want to add my code I added a new message here to reply.</p>
  
  <p>I tried the following code too in which I used "imagecreatefromjpeg"; my code:</p>
  
  <pre><code>&lt;?php
  
         function attachIcon($imgname) {
              $mark = imagecreatefromjpeg($imgname);
              imagesavealpha($mark, true);
  
              list($icon_width, $icon_height) = getimagesize($imgname);
  
              $img = imagecreatefrompng(base_url() . '/uploaded_images/output/viewer.png');
              imagesavealpha($img, true);
  
              $move_left = 280;
              $move_up = 450;
  
              list($mainpic_width, $mainpic_height) = getimagesize(base_url() . '/uploaded_images/output/viewer.png');
              imagecopy($img, $mark, $mainpic_width - $icon_width - $move_left, $mainpic_height - $icon_height - $move_up, 0, 0, $icon_width, $icon_height);
              imagepng($img);  // display the image + positioned icon in the browser
              //imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
          }
  
          header('Content-Type: image/jpeg');
  
          attachIcon( base_url().'/uploaded_images/output/koala.jpg');
  ?&gt;
  </code></pre>
  
  <p>but still not working!</p>