views:

1634

answers:

3

I am working on a custom avatar system for a project, but I have never really done much with the image side of PHP. I assume I need to use GD in some way, but I have no idea where to even start.

Basically, there are a bunch of pre-made transparent PNG images. Users can select 2-3 of them to customize their avatar, and I want to be able to take these images and make a single image out of them to be stored in a folder.

A: 

What you want to use are the PHP ImageMagick utilities.

Specifically, the CombineImages command.

Mike Buckbee
+3  A: 
$image_1 = imagecreatefrompng('image_1.png');
$image_2 = imagecreatefrompng('image_2.png');
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);
imagepng($image_1, 'image_3.png');
Jonathan Patt
This is using GD, by the way.
Jonathan Patt
Well the thing is, I need the final image to be a transparent PNG as well.
James Simpson
I edited the above with two lines of code that allow the background, and final image, to be transparent as well.
Jonathan Patt
@Jonathan, Great answer! Wish I could give more credit.
Mark Tomlin
A: 

Definitely using GD Library.

<?php

$final_img = imagecreate($x, $y); // where x and y are the dimensions of the final image

$image_1 = imagecreatefrompng('image_1.png');
$image_2 = imagecreatefrompng('image_2.png');
$image_3 = imagecreatefrompng('image_3.png');
imagecopy($image_1, $final_img, 0, 0, 0, 0, $x, $y);
imagecopy($image_2, $final_img, 0, 0, 0, 0, $x, $y);
imagecopy($image_3, $final_img, 0, 0, 0, 0, $x, $y);

imagealphablending($final_img, false);
imagesavealpha($final_img, true);
if($output_to_browser){

header('Content-Type: image/png');
imagepng($final_img);

}else{
// output to file

imagepng($final_img, 'final_img.png');

}

?>
thephpdeveloper