I am using this code to overlap 3 partly transparent images into 1, also partly transparent.
$x=3843;
$y=3402;
$final_img = imagecreatetruecolor($x, $y); // where x and y are the dimensions of the final image
imagesavealpha($final_img, true);
$trans_colour = imagecolorallocatealpha($final_img, 0, 0, 0, 127);
imagefill($final_img, 0, 0, $trans_colour);
$image_1 = imagecreatefrompng('1.png');
$image_2 = imagecreatefrompng('3.png');
$image_3 = imagecreatefrompng('6.png');
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_3, 0, 0, 0, 0, $x, $y);
imagepng($final_img, 'final_img.png');
Each image was compressed before overlapping and has the size 100KB. Once overlapped and saved, the final_img.png
, has the size 1,1MB. Big difference. How can preserve the compression of the final image without the extra step of compression the image again? What methods or libraries should I use?