Adding multiple images to a background image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bbunlock
Posts: 8
Joined: 2006-06-04T08:39:46-07:00
Location: England

Adding multiple images to a background image

Post by bbunlock »

Hi and thanks for taking the time read this, I am trying to add multiple images to a background image, I have this working fine if I do each image to be added one at a time, but that means running the command serveral times in order for it to work, I was hoping to do it all in one command but I get a bit of a mess as a result.

Here is the code I have tried to use but it just doesnt work, the images all see to be in the same place.

Code: Select all

	$command = $imageMagickPath."composite -geometry +16+1 {$mainPath}{$clock} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +1+1 {$mainPath}{$signal1} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +224+1 {$mainPath}{$signal2} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +5+82 {$mainPath}{$icon_select} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +7+83 {$mainPath}{$icon1} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +45+83 {$mainPath}{$icon2} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +82+83 {$mainPath}{$icon3} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +122+83 {$mainPath}{$icon4} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +160+83 {$mainPath}{$icon5} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +200+83 {$mainPath}{$icon6} {$mainPath}{$output} {$mainPath}{$output} "
	." composite -geometry +7+130 {$mainPath}{$icon7} {$mainPath}{$output} {$mainPath}{$output}";
	
	exec($command);
basicaly its opening the output file ( the background image) adding an icon image to it and saving it as output, overwriting the original output, then move onto the next image to add and then the next etc, at least thats the effect im looking for.

there are no effects added to the images they are simply placed on the output image in the correct position using the -geometry +?+?, very similar to what adobe photoshop would do via layers.

I have looked at god knows how many examples to do this but im just not getting it to work using one command like above, however it does work if I do each image serperate like below.

Code: Select all

		// add the clock
		$command = $imageMagickPath."composite -geometry +16+1 "
              		." {$mainPath}{$clock} {$mainPath}{$output} {$mainPath}{$output}";              		
		exec($command);
		
		
		
		// add the signal strenghs
		$command = $imageMagickPath."composite -geometry +1+1 "
              		." {$mainPath}{$signal1} {$mainPath}{$output} {$mainPath}{$output}";              		
		exec($command);
		
		$command = $imageMagickPath."composite -geometry +224+1 "
              		." {$mainPath}{$signal2} {$mainPath}{$output} {$mainPath}{$output}";              		
		exec($command);

etc etc
could anyone pleae tell me where im going wrong? ive been at this for over 2 days now and i have looked at god knows how many examples and posts but no joy, so have asked for help as a last resort (cos im just not getting anywhere)

any help appriciated

regards
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Adding multiple images to a background image

Post by Bonzo »

Composite only works with 2 images you want to use convert. I have not tried it all in one comand but see what happens if you change your code to:

Code: Select all

<?php
$command = "convert {$mainPath}{$clock} "
   ." {$mainPath}{$signal1} -geometry +1+1 -composite "
   ." {$mainPath}{$signal2} -geometry +224+1 -composite "
   ." {$mainPath}{$icon_select} -geometry +5+82 -composite "
   ." {$mainPath}{$icon1} -geometry +7+83 -composite "
   ." {$mainPath}{$icon2} -geometry +45+83 -composite "
   ." {$mainPath}{$icon3} -geometry +82+83 -composite "
   ." {$mainPath}{$icon4} -geometry +122+83 -composite "
   ." {$mainPath}{$icon5} -geometry +160+83 -composite "
   ." {$mainPath}{$icon6} -geometry +200+83 -composite "
   ." {$mainPath}{$icon7} -geometry +7+130 -composite output.jpg";
   
   exec($command);

   ?>
   
   <img src="output.jpg">
I do not like all the { } I would have defined the variables outside the $command and you would not need them.
bbunlock
Posts: 8
Joined: 2006-06-04T08:39:46-07:00
Location: England

Re: Adding multiple images to a background image

Post by bbunlock »

Bonzo thank you very much for the help, it worked perfectly :D

very much appriciated
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding multiple images to a background image

Post by fmw42 »

why don't you use convert ... -composite syntax in stead of composite? then you can put it all in one command line.

see http://www.imagemagick.org/Usage/layers/#convert
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding multiple images to a background image

Post by anthony »

For better still use -flatten, -mosaic, and the ultimate -layers merge. techniques
http://imagemagick.org/Usage/layers/#example

This is designed with multiple image 'layering' in mind!!!! Just what you are wanting.

PS: don't forget to look at the other examples referred to by this one.


PPS: your original example was missing either a newline or a semi-colon (;) to seperate the multiple commands you have in your single 'shell script' call. It is not however a 'single' command, such as the 'layering' technqiues, or other techniques on the layering page.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply