douzhi1879 2016-07-14 12:30
浏览 178
已采纳

如何使用PHP GD库将不同大小的多个透明png合并到一个图像中而不进行裁剪?

I would like to merge multiple pngs with transparent background into one image. Images have different sizes and because of this, when images are placed over the top of each other, only within the size of the uppermost image are shown the parts of the merged images.

It's like the last image applies like a mask for the images merged before. I would like to see all the images merged with their original size, without cropping the parts hanging over the size of the last image.

Here is the code I'm using currently:

$images = array();
    foreach (scandir($this->img_dir) as $key => $dirname) {
        if(!strstr($dirname, "."))
        {
            if(isset($_GET[$dirname])) 
            {
                foreach ($this->layer_order as $lkey => $order) {
                    if($lkey == $dirname)
                        $images[$order] = glob($this->img_dir . "/" . $dirname . "/" . $_GET[$dirname] . ".png");
                }
            }
        }
    }


    $destination = imagecreatetruecolor(458, 600);


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

    ksort($images);
    foreach($images as $key => $value) {        
        foreach ($value as $fn) {
            // Load image
            $source = imagecreatefrompng($fn);
            //$source = $this->resize_image($source, 50, 50, 2);

            // Copy over image
            imagecopy($destination, $source, 10, 50, 0, 0, 458, 600);

            // Free memory
            imagedestroy($source);
        }
    }

    return $destination;
  • 写回答

1条回答 默认 最新

  • donglian5309 2016-11-01 08:24
    关注

    I don't know if this is the best solution for what I tried to achive, but after several attempts and some research I used a fairly simple method to avoid the last image in the merging queue, mask the other images with it's relatively small size. This way every image will be merged and positioned accordingly the user wants, inside the specified canvas size, with their transparency.

    I post the whole content of the function in case if someone needs a similar solution. This is a function inside a class.

        $images = array();
    
        // Foreach loop to set images order and creating an array with image paths using params from $_GET before merging
        foreach (scandir($this->img_dir) as $key => $dirname) {
            if(!strstr($dirname, "."))
            {
                if(isset($_GET[$dirname])) 
                {
                    foreach ($this->layer_order as $lkey => $order) {
                        if($lkey == $dirname) {
                            $imageArray = glob($this->img_dir . "/" . $dirname . "/" . ($dirname == "door" && isset($_GET['type']) ? $_GET['type']."/" : "") . $_GET[$dirname] . ".png");
                            foreach ($imageArray as $imgPath) {
                                $images[$order] = $imgPath;
                            }
                        }       
                    }
                }
            }
        }
    
        // Allocate new image
        $destination = imagecreatetruecolor($this->canvas_width, $this->canvas_height);
    
        imagealphablending($destination, false);
        $col = imagecolorallocatealpha($destination, 255, 255, 255, 127);
        imagefilledrectangle($destination, 0, 0, $this->canvas_width, $this->canvas_height, $col);
        imagealphablending($destination, true);
    
        // Sort order
        ksort($images);
    
        // Merging images
        foreach($images as $key => $fn) {
    
            // Load image
            if(strstr($fn, "handle")) 
            {
                $source = $this->resizePng($fn, 0.18, 100, 205);
                imagecopy($destination, $source, 0, 0, 0, 0, $this->canvas_width, $this->canvas_height);
            }
            elseif (strstr($fn, "glass")) 
            {
                // Create a background image by filling a canvas with small image tiles. Params: $file, $shrink_ratio_percent, $column_size, $stretch
                $source = $this->multiplyMergeAndResizePngs($fn, 0.2095, 3, 3.6);
                // Applying mask on images based on black and white patterns
                if ($this->glass_mask)
                    $source = $this->createMask($source, $this->glass_mask);
                imagecopy($destination, $source, 118, 28, 0, 0, $this->canvas_width, $this->canvas_height);
            }
            elseif (strstr($fn, "door")) 
            {    
                $source = imagecreatefrompng($fn);
                imagecopy($destination, $source, 32, 0, 0, 0, $this->canvas_width, $this->canvas_height);
            } else {
                $source = imagecreatefrompng($fn);
                imagecopy($destination, $source, 0, 0, 0, 0, $this->canvas_width, $this->canvas_height);
            }
    
            imagealphablending($destination, true);
    
            // Free memory
            imagedestroy($source);
        }
    
        return $destination;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大