I have been trying to merge two images using gd lib. A card (background), and a "new" frame for that card (foreground, with transparent middle).
This is my code:
<?php
$card = imagecreatefrompng('img/folder1/card.png');
$frame = imagecreatefrompng('img/folder2/frame.png');
list($width,$height) = getimagesize($frame);
imagealphablending($card, true);
imagesavealpha($card, true);
imagecopy($card, $frame, 0, 0, 0, 0, $width, $height, 100);
header('Content-Type: image/png');
imagepng($card);
I do get an output, but it is only the card without the frame.
Kind regards, ZF
Edit (12h later):
I figured out why my output is the card (background) only, regardless of my changes.
I deleted the last value 100 after $height:
imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);
Now my output is only frame.png. It doesn't matter if I use ImageAlphaBlending.
But I know that card.png is definitely under frame.png, because I moved the frame.png to the side on an axis, and the card.png gets visible. So it fails on merging the images together, respecting the transparency of frame.png
If I save the output to a file, it is a transparent .png tho.
Is imagecopy even suitable to merge frame.png with a transparent middle onto a background (card.png)?
Edit: Sorry for the mess! I was working with two versions at the same time. I have cleared up my current workspace now and wrote a new version from scratch.
I did delete the 100 after $height in the version I posted here:
imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);
Clarification to previous edit: Nothing changed at all. The output was still only card.png.
BUT the code of my new version is (Final & Working):
<?php
$card = imagecreatefrompng('img/folder1/card.png');
$frame = imagecreatefrompng('img/folder2/frame.png');
$width = "402"; $height = "599";
imagealphablending($card, true);
imagesavealpha($card, true);
imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);
header('Content-Type: image/png');
imagepng($card);
imagedestroy($card);
imagedestroy($frame);
As you may notice I hardcoded the image width and height.
Instead of using getimagesize
list($width,$height) = getimagesize($frame);
I did check the output from $width, $height before and they are correct, but that seemed to be my problem. If someone has an answer to that, feel free to explain!