Jump to content

[GD] Merging Multiple Images


entity_azirius
 Share

Recommended Posts

I've been working with PHP's GD Library for a few weeks now and I've found something that maybe able to be helpful to certain aspects of my site. This is the imagecopymerge() function.I've succesfully merged two images together.http://entitys-arena.co.uk/images/RoE/GD.phpNow, what I am trying to build here is a battle scene, but as it is, I've hit a wall when trying to merge more than two items together, so I could have more than one monster/thing on the image at once. Is there anyway I could merge more than two images together?Here is the current code:

<? header('content-type: image/jpeg'); 	$max_monsters = 3; 	$monster_img = array('01', '02', '03', '04'); 	$monster_base_url = "/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/"; 	$monster_back_url = "back/"; 	$monsters_url = "monsters/"; 	$count = count($monster_img); 	$select = rand(0, ($count - 1));	$mons_src = $monster_img[$select];		$monster = imagecreatefrompng("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/monsters/$mons_src.png");	$monster_width = imagesx($monster);  	$monster_height = imagesy($monster);  	$image = imagecreatetruecolor($monster_width, $monster_height);  	$image = imagecreatefromjpeg("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");	$size = getimagesize("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");  	$dest_x = $size[0] / 8;  	$dest_y = $size[1] / 4;	        imagecopymerge($image, $monster, $dest_x, $dest_y, 0, 0, $monster_width, $monster_height, 100);            imagejpeg($image);  	imagedestroy($image);  	imagedestroy($monster);?>

If there is someone who thinks they can help me, I'll be very greatful :)

Link to comment
Share on other sites

Literally the exact same way you are doing it with 1 image. Think of your code as doing something like this:

Open the source imageOpen the image to mergeCalculate sizeMerge the imagesOutput the image

You want to do this:

Open the source imageOpen the first image to mergeCalculate sizeMerge the imagesOpen the second image to mergeCalculate sizeMerge the imagesOpen the third image to mergeCalculate sizeMerge the imagesOutput the image

It is literally the exact same code you are using now, you will just want to rearrange it so it's a little easier to read logically. So this is your code rearranged:

<?php//open source image$image = imagecreatefromjpeg("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");$size = getimagesize("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");$dest_x = $size[0] / 8;$dest_y = $size[1] / 4;$max_monsters = 3;$monster_img = array('01', '02', '03', '04');$monster_base_url = "/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/";$monster_back_url = "back/";$monsters_url = "monsters/";$count = count($monster_img);$select = rand(0, ($count - 1));$mons_src = $monster_img[$select];//open an image to merge$monster = imagecreatefrompng("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/monsters/$mons_src.png");$monster_width = imagesx($monster);$monster_height = imagesy($monster);//mergeimagecopymerge($image, $monster, $dest_x, $dest_y, 0, 0, $monster_width, $monster_height, 100);header('content-type: image/jpeg');imagejpeg($image);imagedestroy($image);imagedestroy($monster);?>

If you want to merge multiple images, then just duplicate the code that opens an image and merges it. If you change the filename, it will open another image to merge.

<?php//open source image$image = imagecreatefromjpeg("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");$size = getimagesize("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/back/01.jpg");$dest_x = $size[0] / 8;$dest_y = $size[1] / 4;$max_monsters = 3;$monster_img = array('01', '02', '03', '04');$monster_base_url = "/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/";$monster_back_url = "back/";$monsters_url = "monsters/";$count = count($monster_img);$select = rand(0, ($count - 1));$mons_src = $monster_img[$select];//open an image to merge$monster = imagecreatefrompng("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/monsters/$mons_src.png");$monster_width = imagesx($monster);$monster_height = imagesy($monster);//mergeimagecopymerge($image, $monster, $dest_x, $dest_y, 0, 0, $monster_width, $monster_height, 100);//open an image to merge$monster = imagecreatefrompng("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/monsters/$mons_src.png");$monster_width = imagesx($monster);$monster_height = imagesy($monster);//mergeimagecopymerge($image, $monster, $dest_x, $dest_y, 0, 0, $monster_width, $monster_height, 100);//open an image to merge$monster = imagecreatefrompng("/home/.flirt/entity_azirius/entitys-arena.co.uk/images/RoE/monsters/$mons_src.png");$monster_width = imagesx($monster);$monster_height = imagesy($monster);//mergeimagecopymerge($image, $monster, $dest_x, $dest_y, 0, 0, $monster_width, $monster_height, 100);header('content-type: image/jpeg');imagejpeg($image);imagedestroy($image);imagedestroy($monster);?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...