php 合并透明图片大小,如何使用PHP GD库将不同大小的多个透明PNG合并到一个图像中而不裁剪?...

这个博客介绍了如何避免在合并图像队列时最后一张图像因尺寸小而遮挡其他图像的问题。作者提供了一个简单的方法,根据用户需求在指定画布上以透明度合并和定位每个图像。代码示例中展示了如何加载和处理不同类型的图像(如handle、glass和door),并使用了不同的调整和合并策略,包括缩放、填充和应用遮罩。
摘要由CSDN通过智能技术生成

我不知道这是什么,我试过才达到最佳的解决方案,但多次尝试和一些研究之后,我用了一个相当简单的方法来避免合并队列中的最后一张图像,用其相对较小的尺寸来掩盖其他图像。通过这种方式,每个图像都会根据用户需要在指定的画布大小内以其透明度合并和定位。

如果有人需要类似的解决方案,我会发布该函数的全部内容。这是一个类中的函数。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值