将图像添加到另一个图像中

问题描述:

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:

<?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');
?>

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:

<?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');
?>

-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;

Any help is appreciated!

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


Thanks sinni800,

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.

I tried the following code too in which I used "imagecreatefromjpeg"; my code:

<?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');
?>

but still not working!

I think @sinni800 wanted you to do something like this.

<?php
function open_image ($file) {
    $size=getimagesize($file);
    switch($size["mime"]){
        case "image/jpeg":
            $im = imagecreatefromjpeg($file); //jpeg file
        break;
        case "image/gif":
            $im = imagecreatefromgif($file); //gif file
      break;
      case "image/png":
          $im = imagecreatefrompng($file); //png file
      break;
      default: 
          $im=false;
      break;
    }
    return $im;
}


function attachIcon($imgname)
{
    $mark = open_image($imgname);
    if($mark !== false){
        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
    }
}

header('Content-Type: image/png');
attachIcon('icon.png');
?>

It seems obvious your problem is that you are trying to use imagecreatefrompng() for a JPEG.

Please look at the comments at http://www.php.net/manual/de/function.imagecreatefromjpeg.php. Especially the one by juozaspo at gmail dot com. It has a function to open any image type, because PHP's internal image function do not support choosing the file type automatically.

function open_image ($file) {
    //detect type and process accordinally
    global $type;
    $size=getimagesize($file);
    switch($size["mime"]){
        case "image/jpeg":
            $im = imagecreatefromjpeg($file); //jpeg file
        break;
        case "image/gif":
            $im = imagecreatefromgif($file); //gif file
      break;
      case "image/png":
          $im = imagecreatefrompng($file); //png file
      break;
    default: 
        $im=false;
    break;
    }
    return $im;
}