douzhuo2722 2013-06-19 21:41
浏览 67

PHP GD用于合并两个图标

I am using PHP GD library to merge two icons into one. But in one of the cases the smaller icon goes into back and bigger icon overlaps it completely, I need the small icon to be on top.

Here is my code for merging two icons, MergeIcons.php

$firstIcon = $_GET['icon1'];   
$secondIcon = $_GET['icon2'];  

    $image = imagecreatefrompng($firstIcon);

    $x1 = -1;
    $y1 = -1;

    $i = 0;

    $xCords = array(); // Array to save non-transperent x cords
    $yCords = array(); // Array to save non-transperent y cords

    for($x=0;$x<16;$x++)
    {
          for($y=0;$y<16;$y++)
        {
              if (!transparent(imagecolorat($image, $x, $y)))
      {
                $xCords[$i] = $x;
        $yCords[$i] = $y;
        $i++;
              }
           }
     }

     $minX = min($xCords);
     $minY = min($yCords);

     $width = 16 - $minX;
     $height = 16 - $minY;

 $canvas = imagecreatetruecolor(16,16);
 $col = imagecolorallocatealpha($canvas,0,0,0,127);
 imagefilledrectangle($canvas,0,0,16,16,$col);
 imagealphablending($canvas, true);
 imagesavealpha($canvas, true);
 imagefill($canvas, 0, 0, $col); 

 imagecopy($canvas, $image, 0, 0, $minX , $minY, $width, $height);

     $dest = $canvas;
     $src =  imagecreatefrompng($secondIcon);

     imagealphablending($dest, true);
     imagesavealpha($dest, true);

     $swidth = imagesx($src); 
     $sheight = imagesy($src); 

     imagecopy($dest, $src, 0,0,0,0,$swidth,$sheight); 

     header('Content-Type: image/png');
     imagepng($dest);

     imagedestroy($dest);
     imagedestroy($src);

     function transparent($pixelValue)
     {
        $alpha = ($pixelValue & 0x7F000000) >> 24;
        $red = ($pixelValue & 0xFF0000) >> 16;
        $green = ($pixelValue & 0x00FF00) >> 8;
        $blue = ($pixelValue & 0x0000FF);

        if($alpha == 127)
    return true;
        else
    return false;
     }

Here is how I call the mergeicons.php

echo '<li><a href="MergeIcons.php?icon1='.$secondIconPath.'&icon2='.$firstIconPath.'" download="'.$IconNameQuery.'"><img src="MergeIcons.php?icon1='.$secondIconPath.'&icon2='.$firstIconPath.'"/></a></li>';

In this case, second icon is a small icon and first icon is bigger icon, I want smaller icon on top of bigger icon ( assume its like "Bring it to Front ").

Is that possible?

  • 写回答

1条回答 默认 最新

  • douba6361 2013-06-19 22:43
    关注

    "bring it front " - i think you want to put one image as a layer on the other

    http://phpimageworkshop.com/

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算