function Watermarking()
 {
     if (!empty($this->wmf)) {
         $WatermarkFilename = phpthumb_functions::ResolveFilenameToAbsolute($this->wmf);
         if (is_readable($WatermarkFilename)) {
             if ($fp_watermark = @fopen($WatermarkFilename, 'rb')) {
                 $WatermarkImageData = fread($fp_watermark, filesize($WatermarkFilename));
                 fclose($fp_watermark);
                 if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
                     $watermark_source_x = 0;
                     $watermark_source_y = 0;
                     $watermark_source_width = ImageSX($img_watermark);
                     $watermark_source_height = ImageSY($img_watermark);
                     $watermark_opacity_percent = !empty($this->wmp) ? $this->wmp : 50;
                     $watermark_margin_percent = (100 - (!empty($this->wmm) ? $this->wmm : 5)) / 100;
                     switch (@$this->wma) {
                         case '*':
                             $gdimg_tiledwatermark = $this->ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height);
                             // set the tiled image transparent color to whatever the untiled image transparency index is
                             ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
                             // tile the image as many times as can fit
                             for ($x = round((1 - $watermark_margin_percent) * $this->thumbnail_width); $x < $this->thumbnail_width + $watermark_source_width; $x += round($watermark_source_width + (1 - $watermark_margin_percent) * $this->thumbnail_width)) {
                                 for ($y = round((1 - $watermark_margin_percent) * $this->thumbnail_height); $y < $this->thumbnail_height + $watermark_source_height; $y += round($watermark_source_height + (1 - $watermark_margin_percent) * $this->thumbnail_height)) {
                                     ImageCopy($gdimg_tiledwatermark, $img_watermark, $x, $y, 0, 0, $watermark_source_width, $watermark_source_height);
                                 }
                             }
                             $watermark_source_width = ImageSX($gdimg_tiledwatermark);
                             $watermark_source_height = ImageSY($gdimg_tiledwatermark);
                             $watermark_destination_x = 0;
                             $watermark_destination_y = 0;
                             ImageDestroy($img_watermark);
                             $img_watermark = $gdimg_tiledwatermark;
                             break;
                         case 'T':
                             $watermark_destination_x = round($this->thumbnail_width / 2 - $watermark_source_width / 2 + $watermark_margin_percent);
                             $watermark_destination_y = round((1 - $watermark_margin_percent) * $this->thumbnail_height);
                             break;
                         case 'B':
                             $watermark_destination_x = round($this->thumbnail_width / 2 - $watermark_source_width / 2 + $watermark_margin_percent);
                             $watermark_destination_y = round(($this->thumbnail_height - $watermark_source_height) * $watermark_margin_percent);
                             break;
                         case 'L':
                             $watermark_destination_x = round((1 - $watermark_margin_percent) * $this->thumbnail_width);
                             $watermark_destination_y = round($this->thumbnail_height / 2 - $watermark_source_height / 2 + $watermark_margin_percent);
                             break;
                         case 'R':
                             $watermark_destination_x = round(($this->thumbnail_width - $watermark_source_width) * $watermark_margin_percent);
                             $watermark_destination_y = round($this->thumbnail_height / 2 - $watermark_source_height / 2 + $watermark_margin_percent);
                             break;
                         case 'C':
                             $watermark_destination_x = round($this->thumbnail_width / 2 - $watermark_source_width / 2 + $watermark_margin_percent);
                             $watermark_destination_y = round($this->thumbnail_height / 2 - $watermark_source_height / 2 + $watermark_margin_percent);
                             break;
                         case 'TL':
                             $watermark_destination_x = round((1 - $watermark_margin_percent) * $this->thumbnail_width);
                             $watermark_destination_y = round((1 - $watermark_margin_percent) * $this->thumbnail_height);
                             break;
                         case 'TR':
                             $watermark_destination_x = round(($this->thumbnail_width - $watermark_source_width) * $watermark_margin_percent);
                             $watermark_destination_y = round((1 - $watermark_margin_percent) * $this->thumbnail_height);
                             break;
                         case 'BL':
                             $watermark_destination_x = round((1 - $watermark_margin_percent) * $this->thumbnail_width);
                             $watermark_destination_y = round(($this->thumbnail_height - $watermark_source_height) * $watermark_margin_percent);
                             break;
                         case 'BR':
                         default:
                             $watermark_destination_x = round(($this->thumbnail_width - $watermark_source_width) * $watermark_margin_percent);
                             $watermark_destination_y = round(($this->thumbnail_height - $watermark_source_height) * $watermark_margin_percent);
                             break;
                     }
                     ImageCopyMerge($this->gdimg_output, $img_watermark, $watermark_destination_x, $watermark_destination_y, $watermark_source_x, $watermark_source_y, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
                     return true;
                 }
             }
         }
         return false;
     }
     return true;
 }
}
////////////////////////////////////////////////////////////////
// Debug output, to try and help me diagnose problems
if (@$_REQUEST['phpThumbDebug'] == '2') {
    $phpThumb->phpThumbDebug();
}
////////////////////////////////////////////////////////////////
$phpThumb->GenerateThumbnail();
////////////////////////////////////////////////////////////////
// Debug output, to try and help me diagnose problems
if (@$_REQUEST['phpThumbDebug'] == '3') {
    $phpThumb->phpThumbDebug();
}
////////////////////////////////////////////////////////////////
if (!empty($_REQUEST['file'])) {
    $phpThumb->RenderToFile(phpthumb_functions::ResolveFilenameToAbsolute($_REQUEST['file']));
    if (!empty($_REQUEST['goto']) && substr(strtolower($_REQUEST['goto']), 0, strlen('http://')) == 'http://') {
        // redirect to another URL after image has been rendered to file
        header('Location: ' . $_REQUEST['goto']);
        exit;
    }
} elseif (empty($phpThumb->phpThumbDebug) && !empty($phpThumb->config_cache_directory) && is_writable($phpThumb->config_cache_directory)) {
    $phpThumb->RenderToFile($cache_filename);
}
////////////////////////////////////////////////////////////////
// Debug output, to try and help me diagnose problems
if (@$_REQUEST['phpThumbDebug'] == '4') {
    $phpThumb->phpThumbDebug();
}
////////////////////////////////////////////////////////////////
$phpThumb->OutputThumbnail();