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


Adding an image into another image

我正在尝试将图像添加到模板中,该模板也是另一个图像;我正在使用 PHP 代码点火器;我有一个很好的代码片段,当两个文件都是.PNG文件时,它非常适合此目的;我的代码如下:

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

但是,如果模板"images/sprites/navIcons.png"是 png 文件,而另一个图像是另一种类型(假设.JPG(,我的以下代码不起作用:

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

-我错过了什么吗? - 它应该与任何文件扩展名一起使用吗? - 模板可以是.PNG文件,另一个图像可以是另一种类型(假设.JPG(!也许我需要改变一些东西;

任何帮助不胜感激!

PS:我从这个论坛的一个非常旧的帖子中得到了代码;但我认为它很旧,没有人检查它,所以这就是为什么我在一个新的线程中提出我的问题!谢谢


谢谢sinni800,

应该在评论中回复你,但由于我想添加我的代码,我在这里添加了一条新消息来回复。

我也尝试了以下代码,其中使用了"imagecreatefromjpeg"; 我的代码:

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

但仍然不起作用!

我想@sinni800希望你做这样的事情。

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

很明显,您的问题是您正在尝试将imagecreatefrompng()用于 JPEG。

请查看 http://www.php.net/manual/de/function.imagecreatefromjpeg.php 的评论。尤其是juozaspo at gmail dot com的那个.它具有打开任何图像类型的功能,因为 PHP 的内部图像函数不支持自动选择文件类型。

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