/**
  * Merge captcha image with background
  *
  * @param int $width: width of the image in pixels
  * @param int $height: width of the image in pixels
  * @param string $captchaImage: a GD image identifier
  * @param string $backgroundImage: a GD image identifier
  * @param string $backgroundType (see constants)
  * @param int $mergeType: 0 - Background over captcha, 1 - Captcha over background (see constants)
  * @param int $backgroundFadePercentage: fading factor for background	 
  * @return string GD image identifier of merged image
  */
 public static function mergeCaptchaWithBackground($width, $height, $captchaImage, $backgroundImage, $backgroundType, $mergeType)
 {
     if ($backgroundType != self::BACKGROUND_TYPE_TRANSPARENT) {
         // How faded should the background be? (100=totally gone, 0=bright as the day)
         // to test how much protection the bg noise gives, take a screenshot of the freeCap image
         // and take it into a photo editor. play with contrast and brightness.
         // If you can remove most of the background, then it's not a good enough percentage
         switch ($backgroundType) {
             case self::BACKGROUND_TYPE_WHITE_WITH_GRID:
             case self::BACKGROUND_TYPE_WHITE_WITH_SQUIGGLES:
                 $backgroundFadePercentage = 65;
                 break;
             case self::BACKGROUND_TYPE_MORPHED_IMAGE_BLOCKS:
                 $backgroundFadePercentage = 50;
                 break;
         }
         // Slightly randomize the background fade
         $backgroundFadePercentage += RandomContentUtility::getRandomNumberInRange(-2, 2);
         // Fade background
         if ($backgroundType != self::BACKGROUND_TYPE_MORPHED_IMAGE_BLOCKS) {
             $tempImage = ImageCreateTrueColor($width, $height);
             $white = ImageColorAllocate($tempImage, 255, 255, 255);
             ImageFill($tempImage, 0, 0, $white);
             ImageCopyMerge($backgroundImage, $tempImage, 0, 0, 0, 0, $width, $height, $backgroundFadePercentage);
             ImageDestroy($tempImage);
             $colorFadePercentage = 50;
         } else {
             $colorFadePercentage = $backgroundFadePercentage;
         }
         // Merge background image with CAPTCHA image to create smooth background
         if ($mergeType == self::MERGE_CAPTCHA_OVER_BACKGROUND) {
             // Might want to not blur if using this method, otherwise leaves white-ish border around each letter
             ImageCopyMerge($backgroundImage, $captchaImage, 0, 0, 0, 0, $width, $height, 100);
             ImageCopy($captchaImage, $backgroundImage, 0, 0, 0, 0, $width, $height);
         } else {
             // Background over captcha
             ImageCopyMerge($captchaImage, $backgroundImage, 0, 0, 0, 0, $width, $height, $colorFadePercentage);
         }
     }
     return $captchaImage;
 }