使用PHP合并图像时的图像透明度和alpha [英] Image transparency and alpha when merging images with PHP

查看:104
本文介绍了使用PHP合并图像时的图像透明度和alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在PHP Doc上找到了一些代码,并稍微编辑它以合并我拥有的两个图像。然后将图像保存在服务器上的文件夹中。然而,有一个小问题,我无法弄清楚为什么会发生这种情况。

So I found some code on PHP Doc, and edited it slightly to merge two images I have. The image is then saved in a folder on the server. However there is a slight problem and I am unable to figure out why it is happening.

首先我的代码:

 $glassurl = $_GET['GlassImg'];
    $frameurl = $_GET['FrameImg'];
    $filename = (int)date("H:i:s");

    $src = imagecreatefromgif($frameurl);
    $dest = imagecreatefromjpeg($glassurl);

    imagecolortransparent($src, imagecolorat($src, 0, 0));

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

    $src_x = imagesx($src);
    $src_y = imagesy($src);
    imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);

    // Output and free from memory
    imagepng($dest, 'uploads/imagetest.png');
    imagegif($dest);

    imagedestroy($dest);
    imagedestroy($src

);

其次有关图片的一些信息:

Secondly some information about the images:


  1. 两个图像大小完全相同

  2. 'pattern'图像只是一个块颜色/图案

  3. 框架图像在框架内有透明部分(允许图案显示)

  4. 框架周围的区域是白色到多余的图案

  1. Both Images are exactly the same size
  2. The 'pattern' image is just a block colour/pattern
  3. The frame image has transparent parts within the frame (to allow pattern to show through)
  4. The area around the frame is white to hise the excess pattern

我希望当我将框架重叠到图案上时,因为这些部件会产生一个窗框,里面有玻璃图案,白色会隐藏剩下的图案。

I was hoping that when I overlayed the frame onto the pattern because of these parts that it would produce a window frame, with the glass pattern inside, and the white would hide the remaining patten.

为了说明我提供了图片。会发生什么。

To illustrate I have provided the images. and what happens.

模式:

框架:

结果:

正如您所见它不会导致我的预期。谁能告诉我哪里出错了?我想将框架叠加到图案上,保持透明中心并使用多余的白色覆盖其余的图案。非常感谢任何帮助。

As you can see it doesn't result in what I expected. Can anyone please tell me where I am going wrong? I want to overlay the frame onto the pattern, keeping the transparent center and using the excess white to cover the rest of the patter. Any help is greatly appreciated.

推荐答案

请注意,框架有白边,如果你是希望窗户是你需要裁剪它并删除下面添加的 imagecolortransparent 如果不是你可以使用这个

Please note that your frame has white edges and if you sill want the windows to be wite you need to crop it and remove the imagecolortransparent added below if not you can use this

$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";

$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));

$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);

// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);

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

输出


您还可以

$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";

$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);

$src_x = imagesx($src);
$src_y = imagesy($src);

$srcNew = imagecreatetruecolor($src_x, $src_y);
ImageColorTransparent($srcNew, imageColorAllocate($srcNew, 0, 0, 0));
imagecopy($srcNew, $src, 70, 50, 78, 60, 473, 293);
imagecopymerge($dest, $srcNew, 0, 0, 0, 0, $src_x, $src_y, 100);

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

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

输出

这篇关于使用PHP合并图像时的图像透明度和alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆