doupi4649 2013-10-09 02:50
浏览 37
已采纳

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

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!

  • 写回答

2条回答 默认 最新

  • drv16821 2013-10-09 14:38
    关注

    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');
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备