title: imageMagick command consolidaton?
body: <p>I'm looking for a way to consolidate my imagemagick command lines and fix the aberrations to give me the desired effect which is a graph on a background from the image.</p>
  
  <p>You can see the image files <a href="http://www.automated.it/stack/" rel="nofollow">here</a> - I think embedding them here would make a mess of the layout in stack overflow.</p>
  
  <p>The first image to  you can see is the source graph. 
  The 2nd and 3rd images are what I would deem failures because in:</p>
  
  <p>2 The background is cut off as a result of trying to get the background and graph sizes to 
  match properly.</p>
  
  <p>3 where I didn't add the black backdrop you can see the text has no background</p>
  
  <p>The 4th image is the background I used in the examples.</p>
  
  <p>What I'm aiming for is a graph with the background scaled to fit, but not stretched or squashed to fit. The background file will always have larger dimensions than the graph.</p>
  
  <p>Below is the script I knocked up to get the examples with some notes to explain what doing.</p>
  
  <p>Essentially what I'm after is scale the background image until it fills the graph size, crop any excess.</p>
  
  <p>Anyone help?</p>
  
  <pre><code>
  #!/bin/bash
  
  if [ -z "$3" ]
  then
      echo "usage: $0 background.png foreground.png output.png"
      exit 1
  fi
  
  
  orig_size=`identify -format '%wx%h' "$2"`
  bg_size=`identify -format '%wx%h' "$1"`
  
  
  # make a black background size of graph
  convert -size $orig_size xc:black ./thisblack.png
  
  # resize background image to size of graph
  # this might result in areas with no background
  convert -resize $orig_size "$1" "_$1"
  
  # make the graph the background to force size
  # by merging the graph and resized background.
  # By using the graph as first parameter the size
  # is always correct (even though you can't see
  # the graph in this image)
  convert -composite "$2" "_$1" -depth 8 "_$3"
  
  # overlay graph onto the composite background and graph
  # so we can see the graph again
  convert -size $orig_size -composite "_$3" "$2" -depth 8 "__$3"
  
  # merge the black and final graph for end image and fill 
  # areas with no background with black.
  convert -composite "thisblack.png" "__$3" -depth 8 "$3"
  
  # Clean up
  rm -f "__$3"
  rm -f "_$3"
  rm -f "_$1"
  rm -f thisblack.png
  
  </code></pre>