Image transparency and alpha when merging images w

2019-02-14 19:20发布

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.

Firstly my code:

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

Pattern:

pattern

Frame:

frame

Result:

result

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.

2条回答
虎瘦雄心在
2楼-- · 2019-02-14 20:03

Your image is not transparent as you described, try using this instead if I understood what you described correctly. enter image description here

also you should find a program which does not transform transparency to white when saving (or check for options regarding this) if you really made those transparent in the first place.

查看更多
何必那么认真
3楼-- · 2019-02-14 20:04

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

Output

enter image description here


You can also have

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

Output

enter image description here

查看更多
登录 后发表回答