Beispiel #1
0
 public function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color = '000000', $ttffont = '', $opacity = 100, $margin = 5, $angle = 0, $bg_color = false, $bg_opacity = 0, $fillextend = '')
 {
     // text watermark requested
     if (!$text) {
         return false;
     }
     ImageAlphaBlending($gdimg, true);
     if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)(@[LCR])?$#i', $alignment, $matches)) {
         $originOffsetX = intval($matches[1]);
         $originOffsetY = intval($matches[2]);
         $alignment = @$matches[4] ? $matches[4] : 'L';
         $margin = 0;
     } else {
         $originOffsetX = 0;
         $originOffsetY = 0;
     }
     $metaTextArray = array('^Fb' => $this->phpThumbObject->getimagesizeinfo['filesize'], '^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024), '^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576), '^X' => $this->phpThumbObject->getimagesizeinfo[0], '^Y' => $this->phpThumbObject->getimagesizeinfo[1], '^x' => ImageSX($gdimg), '^y' => ImageSY($gdimg), '^^' => '^');
     $text = strtr($text, $metaTextArray);
     $text = str_replace("\r\n", "\n", $text);
     $text = str_replace("\r", "\n", $text);
     $textlines = explode("\n", $text);
     $this->DebugMessage('Processing ' . count($textlines) . ' lines of text', __FILE__, __LINE__);
     if (@is_readable($ttffont) && is_file($ttffont)) {
         $opacity = 100 - intval(max(min($opacity, 100), 0));
         $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
         $this->DebugMessage('Using TTF font "' . $ttffont . '"', __FILE__, __LINE__);
         $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
         $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         //$text_width = round($max_x - $min_x + ($size * 0.5));
         $text_width = round($max_x - $min_x);
         $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         //$text_height = round($max_y - $min_y + ($size * 0.5));
         $text_height = round($max_y - $min_y);
         $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'jH');
         $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_height = round($char_max_y - $char_min_y);
         if ($alignment == '*') {
             $text_origin_y = $char_height + $margin;
             while ($text_origin_y - $text_height < ImageSY($gdimg)) {
                 $text_origin_x = $margin;
                 while ($text_origin_x < ImageSX($gdimg)) {
                     ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
                     $text_origin_x += $text_width + $margin;
                 }
                 $text_origin_y += $text_height + $margin;
             }
         } else {
             // this block for background color only
             switch ($alignment) {
                 case '*':
                     // handled separately
                     break;
                 case 'T':
                     $text_origin_x = $originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2);
                     $text_origin_y = $char_height + $margin + $originOffsetY;
                     break;
                 case 'B':
                     $text_origin_x = $originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2);
                     $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
                     break;
                 case 'L':
                     $text_origin_x = $margin + $originOffsetX;
                     $text_origin_y = $originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                     break;
                 case 'R':
                     $text_origin_x = $originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                     $text_origin_y = $originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                     break;
                 case 'C':
                     $text_origin_x = $originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2);
                     $text_origin_y = $originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                     break;
                 case 'TL':
                     $text_origin_x = $margin + $originOffsetX;
                     $text_origin_y = $char_height + $margin + $originOffsetY;
                     break;
                 case 'TR':
                     $text_origin_x = $originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                     $text_origin_y = $char_height + $margin + $originOffsetY;
                     break;
                 case 'BL':
                     $text_origin_x = $margin + $originOffsetX;
                     $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
                     break;
                 case 'BR':
                 default:
                     $text_origin_x = $originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                     $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
                     break;
             }
             //ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
             if (phpthumb_functions::IsHexColor($bg_color)) {
                 $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, $bg_color, false, $text_background_alpha);
             } else {
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, 'FFFFFF', false, 127);
             }
             $x1 = $text_origin_x + $min_x;
             $y1 = $text_origin_y + $TTFbox[1];
             $x2 = $text_origin_x + $min_x + $text_width;
             $y2 = $text_origin_y + $TTFbox[1] - $text_height;
             $x_TL = preg_match('#x#i', $fillextend) ? 0 : min($x1, $x2);
             $y_TL = preg_match('#y#i', $fillextend) ? 0 : min($y1, $y2);
             $x_BR = preg_match('#x#i', $fillextend) ? ImageSX($gdimg) : max($x1, $x2);
             $y_BR = preg_match('#y#i', $fillextend) ? ImageSY($gdimg) : max($y1, $y2);
             //while ($y_BR > ImageSY($gdimg)) {
             //	$y_TL--;
             //	$y_BR--;
             //	$text_origin_y--;
             //}
             $this->DebugMessage('WatermarkText() calling ImageFilledRectangle($gdimg, ' . $x_TL . ', ' . $y_TL . ', ' . $x_BR . ', ' . $y_BR . ', $text_color_background)', __FILE__, __LINE__);
             ImageFilledRectangle($gdimg, $x_TL, $y_TL, $x_BR, $y_BR, $text_color_background);
             // end block for background color only
             $y_offset = 0;
             foreach ($textlines as $dummy => $line) {
                 $TTFboxLine = ImageTTFbBox($size, $angle, $ttffont, $line);
                 $min_x_line = min($TTFboxLine[0], $TTFboxLine[2], $TTFboxLine[4], $TTFboxLine[6]);
                 $max_x_line = max($TTFboxLine[0], $TTFboxLine[2], $TTFboxLine[4], $TTFboxLine[6]);
                 //$text_width = round($max_x - $min_x + ($size * 0.5));
                 $text_width_line = round($max_x_line - $min_x_line);
                 $min_y_line = min($TTFboxLine[1], $TTFboxLine[3], $TTFboxLine[5], $TTFboxLine[7]);
                 $max_y_line = max($TTFboxLine[1], $TTFboxLine[3], $TTFboxLine[5], $TTFboxLine[7]);
                 //$text_height = round($max_y - $min_y + ($size * 0.5));
                 $text_height_line = round($max_y_line - $min_y_line);
                 switch ($alignment) {
                     // $text_origin_y set above, just re-set $text_origin_x here as needed
                     case 'L':
                     case 'TL':
                     case 'BL':
                         // no change neccesary
                         break;
                     case 'C':
                     case 'T':
                     case 'B':
                         $text_origin_x = $originOffsetX ? $originOffsetX - round($text_width_line / 2) : round((ImageSX($gdimg) - $text_width_line) / 2);
                         break;
                     case 'R':
                     case 'TR':
                     case 'BR':
                         $text_origin_x = $originOffsetX ? $originOffsetX - $text_width_line : ImageSX($gdimg) - $text_width_line + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                         break;
                 }
                 //ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
                 $this->DebugMessage('WatermarkText() calling ImageTTFtext($gdimg, ' . $size . ', ' . $angle . ', ' . $text_origin_x . ', ' . ($text_origin_y + $y_offset) . ', $letter_color_text, ' . $ttffont . ', ' . $line . ')', __FILE__, __LINE__);
                 ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y + $y_offset, $letter_color_text, $ttffont, $line);
                 $y_offset += $char_height;
             }
         }
         return true;
     } else {
         $size = min(5, max(1, $size));
         $this->DebugMessage('Using built-in font (size=' . $size . ') for text watermark' . ($ttffont ? ' because $ttffont !is_readable(' . $ttffont . ')' : ''), __FILE__, __LINE__);
         $text_width = 0;
         $text_height = 0;
         foreach ($textlines as $dummy => $line) {
             $text_width = max($text_width, ImageFontWidth($size) * strlen($line));
             $text_height += ImageFontHeight($size);
         }
         if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
             ImageAlphaBlending($img_watermark, false);
             if (phpthumb_functions::IsHexColor($bg_color)) {
                 $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, $bg_color, false, $text_background_alpha);
             } else {
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, 'FFFFFF', false, 127);
             }
             $this->DebugMessage('WatermarkText() calling ImageFilledRectangle($img_watermark, 0, 0, ' . ImageSX($img_watermark) . ', ' . ImageSY($img_watermark) . ', $text_color_background)', __FILE__, __LINE__);
             ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
             if ($angle && function_exists('ImageRotate')) {
                 // using $img_watermark_mask is pointless if ImageRotate function isn't available
                 if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
                     $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
                     ImageAlphaBlending($img_watermark_mask, false);
                     ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
                     $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
                 }
             }
             $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
             foreach ($textlines as $key => $line) {
                 switch ($alignment) {
                     case 'C':
                         $x_offset = round(($text_width - ImageFontWidth($size) * strlen($line)) / 2);
                         $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
                         $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
                         break;
                     case 'T':
                         $x_offset = round(($text_width - ImageFontWidth($size) * strlen($line)) / 2);
                         $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
                         $originOffsetY = $margin;
                         break;
                     case 'B':
                         $x_offset = round(($text_width - ImageFontWidth($size) * strlen($line)) / 2);
                         $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
                         $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
                         break;
                     case 'L':
                         $x_offset = 0;
                         $originOffsetX = $margin;
                         $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
                         break;
                     case 'TL':
                         $x_offset = 0;
                         $originOffsetX = $margin;
                         $originOffsetY = $margin;
                         break;
                     case 'BL':
                         $x_offset = 0;
                         $originOffsetX = $margin;
                         $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
                         break;
                     case 'R':
                         $x_offset = $text_width - ImageFontWidth($size) * strlen($line);
                         $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
                         $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
                         break;
                     case 'TR':
                         $x_offset = $text_width - ImageFontWidth($size) * strlen($line);
                         $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
                         $originOffsetY = $margin;
                         break;
                     case 'BR':
                     default:
                         if (!empty($originOffsetX) || !empty($originOffsetY)) {
                             // absolute pixel positioning
                         } else {
                             $x_offset = $text_width - ImageFontWidth($size) * strlen($line);
                             $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
                             $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
                         }
                         break;
                 }
                 $this->DebugMessage('WatermarkText() calling ImageString($img_watermark, ' . $size . ', ' . $x_offset . ', ' . $key * ImageFontHeight($size) . ', ' . $line . ', $text_color_watermark)', __FILE__, __LINE__);
                 ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
                 if ($angle && $img_watermark_mask) {
                     $this->DebugMessage('WatermarkText() calling ImageString($img_watermark_mask, ' . $size . ', ' . $x_offset . ', ' . $key * ImageFontHeight($size) . ', ' . $text . ', $mask_color_watermark)', __FILE__, __LINE__);
                     ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
                 }
             }
             if ($angle && $img_watermark_mask) {
                 $img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
                 $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
                 phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
             }
             //phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
             $this->DebugMessage('WatermarkText() calling phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, ' . ($originOffsetX . 'x' . $originOffsetY) . ', ' . $opacity . ', 0)', __FILE__, __LINE__);
             phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $originOffsetX . 'x' . $originOffsetY, $opacity, 0);
             ImageDestroy($img_watermark);
             return true;
         }
     }
     return false;
 }
Beispiel #2
0
 function ApplyFilters()
 {
     if ($this->fltr && is_array($this->fltr)) {
         if (!(include_once dirname(__FILE__) . '/phpthumb.filters.php')) {
             $this->DebugMessage('Error including "' . dirname(__FILE__) . '/phpthumb.filters.php" which is required for applying filters (' . implode(';', $this->fltr) . ')', __FILE__, __LINE__);
             return false;
         }
         $phpthumbFilters = new phpthumb_filters();
         $phpthumbFilters->phpThumbObject =& $this;
         foreach ($this->fltr as $filtercommand) {
             @(list($command, $parameter) = explode('|', $filtercommand, 2));
             $this->DebugMessage('Attempting to process filter command "' . $command . '(' . $parameter . ')"', __FILE__, __LINE__);
             switch ($command) {
                 case 'brit':
                     // Brightness
                     $phpthumbFilters->Brightness($this->gdimg_output, $parameter);
                     break;
                 case 'cont':
                     // Contrast
                     $phpthumbFilters->Contrast($this->gdimg_output, $parameter);
                     break;
                 case 'ds':
                     // Desaturation
                     $phpthumbFilters->Desaturate($this->gdimg_output, $parameter, '');
                     break;
                 case 'sat':
                     // Saturation
                     $phpthumbFilters->Saturation($this->gdimg_output, $parameter, '');
                     break;
                 case 'gray':
                     // Grayscale
                     $phpthumbFilters->Grayscale($this->gdimg_output);
                     break;
                 case 'clr':
                     // Colorize
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Colorize() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($amount, $color) = explode('|', $parameter, 2));
                     $phpthumbFilters->Colorize($this->gdimg_output, $amount, $color);
                     break;
                 case 'sep':
                     // Sepia
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Sepia() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($amount, $color) = explode('|', $parameter, 2));
                     $phpthumbFilters->Sepia($this->gdimg_output, $amount, $color);
                     break;
                 case 'gam':
                     // Gamma correction
                     $phpthumbFilters->Gamma($this->gdimg_output, $parameter);
                     break;
                 case 'neg':
                     // Negative colors
                     $phpthumbFilters->Negative($this->gdimg_output);
                     break;
                 case 'th':
                     // Threshold
                     $phpthumbFilters->Threshold($this->gdimg_output, $parameter);
                     break;
                 case 'rcd':
                     // ReduceColorDepth
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($colors, $dither) = explode('|', $parameter, 2));
                     $colors = $colors ? (int) $colors : 256;
                     $dither = strlen($dither) > 0 ? (bool) $dither : true;
                     $phpthumbFilters->ReduceColorDepth($this->gdimg_output, $colors, $dither);
                     break;
                 case 'flip':
                     // Flip
                     $phpthumbFilters->Flip($this->gdimg_output, strpos(strtolower($parameter), 'x') !== false, strpos(strtolower($parameter), 'y') !== false);
                     break;
                 case 'edge':
                     // EdgeDetect
                     $phpthumbFilters->EdgeDetect($this->gdimg_output);
                     break;
                 case 'emb':
                     // Emboss
                     $phpthumbFilters->Emboss($this->gdimg_output);
                     break;
                 case 'bvl':
                     // Bevel
                     @(list($width, $color1, $color2) = explode('|', $parameter, 3));
                     $phpthumbFilters->Bevel($this->gdimg_output, $width, $color1, $color2);
                     break;
                 case 'lvl':
                     // autoLevels
                     @(list($band, $method, $threshold) = explode('|', $parameter, 3));
                     $band = $band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*';
                     $method = strlen($method) > 0 ? intval($method) : 2;
                     $threshold = strlen($threshold) > 0 ? floatval($threshold) : 0.1;
                     $phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold);
                     break;
                 case 'wb':
                     // WhiteBalance
                     $phpthumbFilters->WhiteBalance($this->gdimg_output, $parameter);
                     break;
                 case 'hist':
                     // Histogram overlay
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping HistogramOverlay() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y) = explode('|', $parameter, 8));
                     $bands = $bands ? $bands : '*';
                     $colors = $colors ? $colors : '';
                     $width = $width ? $width : 0.25;
                     $height = $height ? $height : 0.25;
                     $alignment = $alignment ? $alignment : 'BR';
                     $opacity = $opacity ? $opacity : 50;
                     $margin_x = $margin_x ? $margin_x : 5;
                     $margin_y = $margin_y;
                     // just to note it wasn't forgotten, but let the value always pass unchanged
                     $phpthumbFilters->HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y);
                     break;
                 case 'fram':
                     // Frame
                     @(list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter, 5));
                     $phpthumbFilters->Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2);
                     break;
                 case 'drop':
                     // DropShadow
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping DropShadow() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $this->is_alpha = true;
                     @(list($distance, $width, $color, $angle, $fade) = explode('|', $parameter, 5));
                     $phpthumbFilters->DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade);
                     break;
                 case 'mask':
                     // Mask cropping
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Mask() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $mask_filename = $this->ResolveFilenameToAbsolute($parameter);
                     if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) {
                         $MaskImageData = '';
                         do {
                             $buffer = fread($fp_mask, 8192);
                             $MaskImageData .= $buffer;
                         } while (strlen($buffer) > 0);
                         fclose($fp_mask);
                         if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) {
                             $this->is_alpha = true;
                             $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
                             ImageDestroy($gdimg_mask);
                         } else {
                             $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $mask_filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('Cannot open mask file "' . $mask_filename . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'elip':
                     // Elipse cropping
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Elipse() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $this->is_alpha = true;
                     $phpthumbFilters->Elipse($this->gdimg_output);
                     break;
                 case 'ric':
                     // RoundedImageCorners
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     @(list($radius_x, $radius_y) = explode('|', $parameter, 2));
                     if ($radius_x < 1 || $radius_y < 1) {
                         $this->DebugMessage('Skipping RoundedImageCorners(' . $radius_x . ', ' . $radius_y . ') because x/y radius is less than 1', __FILE__, __LINE__);
                         break;
                     }
                     $this->is_alpha = true;
                     $phpthumbFilters->RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y);
                     break;
                 case 'crop':
                     // Crop
                     @(list($left, $right, $top, $bottom) = explode('|', $parameter, 4));
                     $phpthumbFilters->Crop($this->gdimg_output, $left, $right, $top, $bottom);
                     break;
                 case 'bord':
                     // Border
                     @(list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter, 4));
                     $this->is_alpha = true;
                     $phpthumbFilters->ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border);
                     break;
                 case 'over':
                     // Overlay
                     @(list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4));
                     $underlay = (bool) ($underlay ? $underlay : false);
                     $margin = strlen($margin) > 0 ? $margin : ($underlay ? 0.1 : 0.0);
                     $opacity = strlen($opacity) > 0 ? $opacity : 100;
                     if ($margin > 0 && $margin < 1) {
                         $margin = min(0.499, $margin);
                     } elseif ($margin > -1 && $margin < 0) {
                         $margin = max(-0.499, $margin);
                     }
                     $filename = $this->ResolveFilenameToAbsolute($filename);
                     if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
                         $WatermarkImageData = '';
                         do {
                             $buffer = fread($fp_watermark, 8192);
                             $WatermarkImageData .= $buffer;
                         } while (strlen($buffer) > 0);
                         fclose($fp_watermark);
                         if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
                             if ($margin < 1) {
                                 $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * (ImageSX($this->gdimg_output) * $margin)));
                                 $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * (ImageSY($this->gdimg_output) * $margin)));
                             } else {
                                 $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * $margin));
                                 $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * $margin));
                             }
                             if ($underlay) {
                                 if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
                                     ImageAlphaBlending($img_watermark_resized, false);
                                     ImageSaveAlpha($img_watermark_resized, true);
                                     $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark_resized), ImageSY($img_watermark_resized), ImageSX($img_watermark), ImageSY($img_watermark));
                                     if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
                                         ImageAlphaBlending($img_source_resized, false);
                                         ImageSaveAlpha($img_source_resized, true);
                                         $this->ImageResizeFunction($img_source_resized, $this->gdimg_output, 0, 0, 0, 0, ImageSX($img_source_resized), ImageSY($img_source_resized), ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
                                         $phpthumbFilters->WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin);
                                         ImageCopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
                                     } else {
                                         $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . $resized_x . ', ' . $resized_y . ')', __FILE__, __LINE__);
                                     }
                                     ImageDestroy($img_watermark_resized);
                                 } else {
                                     $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . ImageSX($this->gdimg_output) . ', ' . ImageSY($this->gdimg_output) . ')', __FILE__, __LINE__);
                                 }
                             } else {
                                 // overlay
                                 if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
                                     ImageAlphaBlending($img_watermark_resized, false);
                                     ImageSaveAlpha($img_watermark_resized, true);
                                     $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark_resized), ImageSY($img_watermark_resized), ImageSX($img_watermark), ImageSY($img_watermark));
                                     $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin);
                                     ImageDestroy($img_watermark_resized);
                                 } else {
                                     $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . $resized_x . ', ' . $resized_y . ')', __FILE__, __LINE__);
                                 }
                             }
                             ImageDestroy($img_watermark);
                         } else {
                             $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('Cannot open overlay file "' . $filename . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'wmi':
                     // WaterMarkImage
                     @(list($filename, $alignment, $opacity, $margin['x'], $margin['y'], $rotate_angle) = explode('|', $parameter, 6));
                     // $margin can be pixel margin or percent margin if $alignment is text, or max width/height if $alignment is position like "50x75"
                     $alignment = $alignment ? $alignment : 'BR';
                     $opacity = strlen($opacity) ? intval($opacity) : 50;
                     $rotate_angle = strlen($rotate_angle) ? intval($rotate_angle) : 0;
                     if (!preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
                         $margins = array('x', 'y');
                         foreach ($margins as $xy) {
                             $margin[$xy] = strlen($margin[$xy]) ? $margin[$xy] : 5;
                             if ($margin[$xy] > 0 && $margin[$xy] < 1) {
                                 $margin[$xy] = min(0.499, $margin[$xy]);
                             } elseif ($margin[$xy] > -1 && $margin[$xy] < 0) {
                                 $margin[$xy] = max(-0.499, $margin[$xy]);
                             }
                         }
                     }
                     $filename = $this->ResolveFilenameToAbsolute($filename);
                     if (@is_readable($filename)) {
                         if ($img_watermark = $this->ImageCreateFromFilename($filename)) {
                             if ($rotate_angle !== 0) {
                                 $phpthumbFilters->ImprovedImageRotate($img_watermark, $rotate_angle);
                             }
                             if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
                                 $watermark_max_width = intval($margin['x'] ? $margin['x'] : ImageSX($img_watermark));
                                 $watermark_max_height = intval($margin['y'] ? $margin['y'] : ImageSY($img_watermark));
                                 $scale = phpthumb_functions::ScaleToFitInBox(ImageSX($img_watermark), ImageSY($img_watermark), $watermark_max_width, $watermark_max_height, true, true);
                                 $this->DebugMessage('Scaling watermark by a factor of ' . number_format($scale, 4), __FILE__, __LINE__);
                                 if ($scale > 1 || $scale < 1) {
                                     if ($img_watermark2 = phpthumb_functions::ImageCreateFunction($scale * ImageSX($img_watermark), $scale * ImageSY($img_watermark))) {
                                         ImageAlphaBlending($img_watermark2, false);
                                         ImageSaveAlpha($img_watermark2, true);
                                         $this->ImageResizeFunction($img_watermark2, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark2), ImageSY($img_watermark2), ImageSX($img_watermark), ImageSY($img_watermark));
                                         $img_watermark = $img_watermark2;
                                     } else {
                                         $this->DebugMessage('ImageCreateFunction(' . $scale * ImageSX($img_watermark) . ', ' . $scale * ImageSX($img_watermark) . ') failed', __FILE__, __LINE__);
                                     }
                                 }
                                 $watermark_dest_x = round($matches[1] - ImageSX($img_watermark) / 2);
                                 $watermark_dest_y = round($matches[2] - ImageSY($img_watermark) / 2);
                                 $alignment = $watermark_dest_x . 'x' . $watermark_dest_y;
                             }
                             $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin['x'], $margin['y']);
                             ImageDestroy($img_watermark);
                             if (isset($img_watermark2) && is_resource($img_watermark2)) {
                                 ImageDestroy($img_watermark2);
                             }
                         } else {
                             $this->DebugMessage('ImageCreateFromFilename() failed for "' . $filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('!is_readable(' . $filename . ')', __FILE__, __LINE__);
                     }
                     break;
                 case 'wmt':
                     // WaterMarkText
                     @(list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend) = explode('|', $parameter, 11));
                     $text = $text ? $text : '';
                     $size = $size ? $size : 3;
                     $alignment = $alignment ? $alignment : 'BR';
                     $hex_color = $hex_color ? $hex_color : '000000';
                     $ttffont = $ttffont ? $ttffont : '';
                     $opacity = strlen($opacity) ? $opacity : 50;
                     $margin = strlen($margin) ? $margin : 5;
                     $angle = strlen($angle) ? $angle : 0;
                     $bg_color = $bg_color ? $bg_color : false;
                     $bg_opacity = $bg_opacity ? $bg_opacity : 0;
                     $fillextend = $fillextend ? $fillextend : '';
                     if (basename($ttffont) == $ttffont) {
                         $ttffont = realpath($this->config_ttf_directory . DIRECTORY_SEPARATOR . $ttffont);
                     } else {
                         $ttffont = $this->ResolveFilenameToAbsolute($ttffont);
                     }
                     $phpthumbFilters->WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend);
                     break;
                 case 'blur':
                     // Blur
                     @(list($radius) = explode('|', $parameter, 1));
                     $radius = $radius ? $radius : 1;
                     if (phpthumb_functions::gd_version() >= 2) {
                         $phpthumbFilters->Blur($this->gdimg_output, $radius);
                     } else {
                         $this->DebugMessage('Skipping Blur() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'gblr':
                     // Gaussian Blur
                     $phpthumbFilters->BlurGaussian($this->gdimg_output);
                     break;
                 case 'sblr':
                     // Selective Blur
                     $phpthumbFilters->BlurSelective($this->gdimg_output);
                     break;
                 case 'mean':
                     // MeanRemoval blur
                     $phpthumbFilters->MeanRemoval($this->gdimg_output);
                     break;
                 case 'smth':
                     // Smooth blur
                     $phpthumbFilters->Smooth($this->gdimg_output, $parameter);
                     break;
                 case 'usm':
                     // UnSharpMask sharpening
                     @(list($amount, $radius, $threshold) = explode('|', $parameter, 3));
                     $amount = $amount ? $amount : 80;
                     $radius = $radius ? $radius : 0.5;
                     $threshold = strlen($threshold) ? $threshold : 3;
                     if (phpthumb_functions::gd_version() >= 2.0) {
                         ob_start();
                         if (!@(include_once dirname(__FILE__) . '/phpthumb.unsharp.php')) {
                             $include_error = ob_get_contents();
                             if ($include_error) {
                                 $this->DebugMessage('include_once("' . dirname(__FILE__) . '/phpthumb.unsharp.php") generated message: "' . $include_error . '"', __FILE__, __LINE__);
                             }
                             $this->DebugMessage('Error including "' . dirname(__FILE__) . '/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__);
                             ob_end_clean();
                             return false;
                         }
                         ob_end_clean();
                         phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold);
                     } else {
                         $this->DebugMessage('Skipping unsharp mask because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     break;
                 case 'size':
                     // Resize
                     @(list($newwidth, $newheight, $stretch) = explode('|', $parameter));
                     $newwidth = !$newwidth ? ImageSX($this->gdimg_output) : ($newwidth > 0 && $newwidth < 1 ? round($newwidth * ImageSX($this->gdimg_output)) : round($newwidth));
                     $newheight = !$newheight ? ImageSY($this->gdimg_output) : ($newheight > 0 && $newheight < 1 ? round($newheight * ImageSY($this->gdimg_output)) : round($newheight));
                     $stretch = $stretch ? true : false;
                     if ($stretch) {
                         $scale_x = phpthumb_functions::ScaleToFitInBox(ImageSX($this->gdimg_output), ImageSX($this->gdimg_output), $newwidth, $newwidth, true, true);
                         $scale_y = phpthumb_functions::ScaleToFitInBox(ImageSY($this->gdimg_output), ImageSY($this->gdimg_output), $newheight, $newheight, true, true);
                     } else {
                         $scale_x = phpthumb_functions::ScaleToFitInBox(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output), $newwidth, $newheight, true, true);
                         $scale_y = $scale_x;
                     }
                     $this->DebugMessage('Scaling watermark (' . ($stretch ? 'with' : 'without') . ' stretch) by a factor of "' . number_format($scale_x, 4) . ' x ' . number_format($scale_y, 4) . '"', __FILE__, __LINE__);
                     if ($scale_x > 1 || $scale_x < 1 || $scale_y > 1 || $scale_y < 1) {
                         if ($img_temp = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
                             ImageCopy($img_temp, $this->gdimg_output, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
                             //ImageDestroy($this->gdimg_output);
                             if ($this->gdimg_output = phpthumb_functions::ImageCreateFunction($scale_x * ImageSX($img_temp), $scale_y * ImageSY($img_temp))) {
                                 ImageAlphaBlending($this->gdimg_output, false);
                                 ImageSaveAlpha($this->gdimg_output, true);
                                 $this->ImageResizeFunction($this->gdimg_output, $img_temp, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output), ImageSX($img_temp), ImageSY($img_temp));
                             } else {
                                 $this->DebugMessage('ImageCreateFunction(' . $scale_x * ImageSX($img_temp) . ', ' . $scale_y * ImageSY($img_temp) . ') failed', __FILE__, __LINE__);
                             }
                             ImageDestroy($img_temp);
                         } else {
                             $this->DebugMessage('ImageCreateFunction(' . ImageSX($this->gdimg_output) . ', ' . ImageSY($this->gdimg_output) . ') failed', __FILE__, __LINE__);
                         }
                     }
                     break;
                 case 'rot':
                     // ROTate
                     @(list($angle, $bgcolor) = explode('|', $parameter, 2));
                     $phpthumbFilters->ImprovedImageRotate($this->gdimg_output, $angle, $bgcolor);
                     break;
                 case 'stc':
                     // Source Transparent Color
                     @(list($hexcolor, $min_limit, $max_limit) = explode('|', $parameter, 3));
                     if (!phpthumb_functions::IsHexColor($hexcolor)) {
                         $this->DebugMessage('Skipping SourceTransparentColor hex color is invalid (' . $hexcolor . ')', __FILE__, __LINE__);
                         return false;
                     }
                     $min_limit = strlen($min_limit) ? $min_limit : 5;
                     $max_limit = strlen($max_limit) ? $max_limit : 10;
                     if ($gdimg_mask = $phpthumbFilters->SourceTransparentColorMask($this->gdimg_output, $hexcolor, $min_limit, $max_limit)) {
                         $this->is_alpha = true;
                         $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
                         ImageDestroy($gdimg_mask);
                     } else {
                         $this->DebugMessage('SourceTransparentColorMask() failed for "' . $mask_filename . '"', __FILE__, __LINE__);
                     }
                     break;
             }
             $this->DebugMessage('Finished processing filter command "' . $command . '(' . $parameter . ')"', __FILE__, __LINE__);
         }
     }
     return true;
 }
 function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color = '000000', $ttffont = '', $opacity = 100, $margin = 5, $angle = 0)
 {
     // text watermark requested
     if (!$text) {
         return false;
     }
     ImageAlphaBlending($gdimg, true);
     $text = str_replace("\r\n", "\n", $text);
     $text = str_replace("\r", "\n", $text);
     $textlines = explode("\n", $text);
     if (@is_readable($ttffont) && is_file($ttffont)) {
         $opacity = 100 - intval(max(min($opacity, 100), 0));
         $this->DebugMessage('Using TTF font "' . $ttffont . '"', __FILE__, __LINE__);
         $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
         $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         //$text_width = round($max_x - $min_x + ($size * 0.5));
         $text_width = round($max_x - $min_x);
         $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         //$text_height = round($max_y - $min_y + ($size * 0.5));
         $text_height = round($max_y - $min_y);
         $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'pH');
         $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_height = round($char_max_y - $char_min_y);
         switch ($alignment) {
             case 'T':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'B':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
                 break;
             case 'L':
                 $text_origin_x = $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'R':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'C':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'TL':
                 $text_origin_x = $margin;
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'TR':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'BL':
                 $text_origin_x = $margin;
                 $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
                 break;
             case 'BR':
             default:
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
                 break;
         }
         $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
         if ($alignment == '*') {
             $text_origin_y = $char_height + $margin;
             while ($text_origin_y - $text_height < ImageSY($gdimg)) {
                 $text_origin_x = $margin;
                 while ($text_origin_x < ImageSX($gdimg)) {
                     ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
                     $text_origin_x += $text_width + $margin;
                 }
                 $text_origin_y += $text_height + $margin;
             }
         } else {
             //ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
             ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
         }
         return true;
         //if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
         //	if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
         //		$letter_color_background      = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark,      127, 127, 127, 127);
         //		$letter_color_background_mask = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark_mask, 127, 127, 127, 127);
         //		ImageFilledRectangle($img_watermark,      0, 0, ImageSX($img_watermark),      ImageSY($img_watermark),      $letter_color_background);
         //		ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $letter_color_background_mask);
         //
         //		$letter_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
         //		$letter_color_watermark_mask = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
         //
         //		ImageTTFtext($img_watermark,      $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y,  $letter_color_watermark,      $ttffont, $text);
         //		ImageTTFtext($img_watermark_mask, $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y, -$letter_color_watermark_mask, $ttffont, $text);
         //
         //		$this->ApplyMask($img_watermark_mask, $img_watermark);
         //		$this->WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
         //
         //		ImageDestroy($img_watermark);
         //		ImageDestroy($img_watermark_mask);
         //		return true;
         //	}
         //}
     } else {
         $size = min(5, max(1, $size));
         $this->DebugMessage('Using built-in font (size=' . $size . ') for text watermark' . ($ttffont ? ' because $ttffont !is_readable(' . $ttffont . ')' : ''), __FILE__, __LINE__);
         $text_width = 0;
         $text_height = 0;
         foreach ($textlines as $line) {
             $text_width = max($text_width, ImageFontWidth($size) * strlen($line));
             $text_height += ImageFontHeight($size);
         }
         if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
             ImageAlphaBlending($img_watermark, false);
             $text_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark, 255, 255, 255, 127);
             ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
             if ($angle && function_exists('ImageRotate')) {
                 // using $img_watermark_mask is pointless if ImageRotate function isn't available
                 if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
                     $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
                     ImageAlphaBlending($img_watermark_mask, false);
                     ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
                     $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
                 }
             }
             $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
             foreach ($textlines as $key => $line) {
                 switch ($alignment) {
                     case 'C':
                     case 'T':
                     case 'B':
                         $x_offset = round(($text_width - ImageFontWidth($size) * strlen($line)) / 2);
                         break;
                     case 'L':
                     case 'TL':
                     case 'BL':
                         $x_offset = 0;
                         break;
                     case 'R':
                     case 'TR':
                     case 'BR':
                     default:
                         $x_offset = $text_width - ImageFontWidth($size) * strlen($line);
                         break;
                 }
                 ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
                 if ($angle && $img_watermark_mask) {
                     ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
                 }
             }
             if ($angle && $img_watermark_mask) {
                 $img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
                 $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
                 phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
             }
             phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
             ImageDestroy($img_watermark);
             return true;
         }
     }
     return false;
 }
Beispiel #4
0
 function ApplyFilters()
 {
     if ($this->fltr && is_array($this->fltr)) {
         if (!(include_once dirname(__FILE__) . '/phpthumb.filters.php')) {
             $this->DebugMessage('Error including "' . dirname(__FILE__) . '/phpthumb.filters.php" which is required for applying filters (' . implode(';', $this->fltr) . ')', __FILE__, __LINE__);
             return false;
         }
         foreach ($this->fltr as $filtercommand) {
             @(list($command, $parameter) = explode('|', $filtercommand, 2));
             $this->DebugMessage('Attempting to process filter command "' . $command . '"', __FILE__, __LINE__);
             switch ($command) {
                 case 'brit':
                     phpthumb_filters::Brightness($this->gdimg_output, $parameter);
                     break;
                 case 'cont':
                     phpthumb_filters::Contrast($this->gdimg_output, $parameter);
                     break;
                 case 'ds':
                     phpthumb_filters::Desaturate($this->gdimg_output, $parameter, '');
                     break;
                 case 'sat':
                     phpthumb_filters::Saturation($this->gdimg_output, $parameter, '');
                     break;
                 case 'gray':
                     phpthumb_filters::Grayscale($this->gdimg_output);
                     break;
                 case 'clr':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Colorize() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($amount, $color) = explode('|', $parameter));
                     phpthumb_filters::Colorize($this->gdimg_output, $amount, $color);
                     break;
                 case 'sep':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Sepia() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($amount, $color) = explode('|', $parameter));
                     phpthumb_filters::Sepia($this->gdimg_output, $amount, $color);
                     break;
                 case 'gam':
                     phpthumb_filters::Gamma($this->gdimg_output, $parameter);
                     break;
                 case 'neg':
                     phpthumb_filters::Negative($this->gdimg_output);
                     break;
                 case 'th':
                     phpthumb_filters::Threshold($this->gdimg_output, $parameter);
                     break;
                 case 'rcd':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($colors, $dither) = explode('|', $parameter));
                     $colors = $colors ? (int) $colors : 256;
                     $dither = strlen($dither) > 0 ? (bool) $dither : true;
                     phpthumb_filters::ReduceColorDepth($this->gdimg_output, $colors, $dither);
                     break;
                 case 'flip':
                     phpthumb_filters::Flip($this->gdimg_output, strpos(strtolower($parameter), 'x') !== false, strpos(strtolower($parameter), 'y') !== false);
                     break;
                 case 'edge':
                     phpthumb_filters::EdgeDetect($this->gdimg_output);
                     break;
                 case 'emb':
                     phpthumb_filters::Emboss($this->gdimg_output);
                     break;
                 case 'bvl':
                     @(list($width, $color1, $color2) = explode('|', $parameter));
                     phpthumb_filters::Bevel($this->gdimg_output, $width, $color1, $color2);
                     break;
                 case 'lvl':
                     @(list($band, $min, $max) = explode('|', $parameter));
                     $band = $band ? $band : '*';
                     $min = strlen($min) > 0 ? $min : '-1';
                     $max = strlen($max) > 0 ? $max : '-1';
                     phpthumb_filters::HistogramStretch($this->gdimg_output, $band, $min, $max);
                     break;
                 case 'wb':
                     phpthumb_filters::WhiteBalance($this->gdimg_output, $parameter);
                     break;
                 case 'hist':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping HistogramOverlay() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         break;
                     }
                     @(list($bands, $colors, $width, $height, $alignment, $opacity, $margin) = explode('|', $parameter));
                     $bands = $bands ? $bands : '*';
                     $colors = $colors ? $colors : '';
                     $width = $width ? $width : 0.25;
                     $height = $height ? $height : 0.25;
                     $alignment = $alignment ? $alignment : 'BR';
                     $opacity = $opacity ? $opacity : 50;
                     $margin = $margin ? $margin : 5;
                     phpthumb_filters::HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin);
                     break;
                 case 'fram':
                     @(list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter));
                     phpthumb_filters::Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2);
                     break;
                 case 'drop':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping DropShadow() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $this->is_alpha = true;
                     @(list($distance, $width, $color, $angle, $fade) = explode('|', $parameter));
                     phpthumb_filters::DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade);
                     break;
                 case 'mask':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Mask() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $mask_filename = $this->ResolveFilenameToAbsolute($parameter);
                     if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) {
                         $MaskImageData = fread($fp_mask, filesize($mask_filename));
                         fclose($fp_mask);
                         if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) {
                             $this->is_alpha = true;
                             phpthumb_filters::ApplyMask($gdimg_mask, $this->gdimg_output);
                             ImageDestroy($gdimg_mask);
                         } else {
                             $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $mask_filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('Cannot open mask file "' . $mask_filename . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'elip':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Elipse() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     $this->is_alpha = true;
                     phpthumb_filters::Elipse($this->gdimg_output);
                     break;
                 case 'ric':
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     @(list($radius_x, $radius_y) = explode('|', $parameter));
                     if ($radius_x < 1 || $radius_y < 1) {
                         $this->DebugMessage('Skipping RoundedImageCorners(' . $radius_x . ', ' . $radius_y . ') because x/y radius is less than 1', __FILE__, __LINE__);
                         break;
                     }
                     $this->is_alpha = true;
                     phpthumb_filters::RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y);
                     break;
                 case 'bord':
                     @(list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter));
                     $this->is_alpha = true;
                     phpthumb_filters::ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border);
                     break;
                 case 'over':
                     @(list($filename, $underlay, $margin, $opacity) = explode('|', $parameter));
                     $underlay = (bool) ($underlay ? $underlay : false);
                     $margin = strlen($margin) > 0 ? $margin : ($underlay ? 0.1 : 0.0);
                     $opacity = strlen($opacity) > 0 ? $opacity : 100;
                     if ($margin > 0 && $margin < 1) {
                         $margin = min(0.499, $margin);
                     } elseif ($margin > -1 && $margin < 0) {
                         $margin = max(-0.499, $margin);
                     }
                     $filename = $this->ResolveFilenameToAbsolute($filename);
                     if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
                         $WatermarkImageData = fread($fp_watermark, filesize($filename));
                         fclose($fp_watermark);
                         if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
                             if ($margin < 1) {
                                 $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * (ImageSX($this->gdimg_output) * $margin)));
                                 $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * (ImageSY($this->gdimg_output) * $margin)));
                             } else {
                                 $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * $margin));
                                 $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * $margin));
                             }
                             if ($underlay) {
                                 if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
                                     ImageAlphaBlending($img_watermark_resized, false);
                                     ImageSaveAlpha($img_watermark_resized, true);
                                     phpthumb_functions::ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark_resized), ImageSY($img_watermark_resized), ImageSX($img_watermark), ImageSY($img_watermark));
                                     if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
                                         ImageAlphaBlending($img_source_resized, false);
                                         ImageSaveAlpha($img_source_resized, true);
                                         phpthumb_functions::ImageResizeFunction($img_source_resized, $this->gdimg_output, 0, 0, 0, 0, ImageSX($img_source_resized), ImageSY($img_source_resized), ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
                                         phpthumb_filters::WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin);
                                         ImageCopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
                                     } else {
                                         $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . $resized_x . ', ' . $resized_y . ')', __FILE__, __LINE__);
                                     }
                                     ImageDestroy($img_watermark_resized);
                                 } else {
                                     $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . ImageSX($this->gdimg_output) . ', ' . ImageSY($this->gdimg_output) . ')', __FILE__, __LINE__);
                                 }
                             } else {
                                 // overlay
                                 if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
                                     ImageAlphaBlending($img_watermark_resized, false);
                                     ImageSaveAlpha($img_watermark_resized, true);
                                     phpthumb_functions::ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark_resized), ImageSY($img_watermark_resized), ImageSX($img_watermark), ImageSY($img_watermark));
                                     phpthumb_filters::WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin);
                                     ImageDestroy($img_watermark_resized);
                                 } else {
                                     $this->DebugMessage('phpthumb_functions::ImageCreateFunction(' . $resized_x . ', ' . $resized_y . ')', __FILE__, __LINE__);
                                 }
                             }
                             ImageDestroy($img_watermark);
                         } else {
                             $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('Cannot open overlay file "' . $filename . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'wmi':
                     @(list($filename, $alignment, $opacity, $margin) = explode('|', $parameter));
                     $alignment = $alignment ? $alignment : 'BR';
                     $opacity = strlen($opacity) ? $opacity : 50;
                     $margin = strlen($margin) ? $margin : 5;
                     $filename = $this->ResolveFilenameToAbsolute($filename);
                     if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
                         $WatermarkImageData = fread($fp_watermark, filesize($filename));
                         fclose($fp_watermark);
                         if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
                             // great
                             phpthumb_filters::WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin);
                             ImageDestroy($img_watermark);
                         } else {
                             $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $filename . '"', __FILE__, __LINE__);
                         }
                     } else {
                         $this->DebugMessage('Cannot open watermark file "' . $filename . '"', __FILE__, __LINE__);
                     }
                     break;
                 case 'wmt':
                     @(list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle) = explode('|', $parameter));
                     $text = $text ? $text : '';
                     $size = $size ? $size : 3;
                     $alignment = $alignment ? $alignment : 'BR';
                     $hex_color = $hex_color ? $hex_color : '000000';
                     $ttffont = $ttffont ? $ttffont : '';
                     $opacity = strlen($opacity) ? $opacity : 50;
                     $margin = strlen($margin) ? $margin : 5;
                     $angle = strlen($angle) ? $angle : 0;
                     if (basename($ttffont) == $ttffont) {
                         $ttffont = realpath($this->config_ttf_directory . $this->osslash . $ttffont);
                     } else {
                         $ttffont = $this->ResolveFilenameToAbsolute($ttffont);
                     }
                     phpthumb_filters::WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle);
                     break;
                 case 'blur':
                     @(list($radius) = explode('|', $parameter));
                     $radius = $radius ? $radius : 1;
                     if (phpthumb_functions::gd_version() < 2) {
                         $this->DebugMessage('Skipping Blur() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     phpthumb_filters::Blur($this->gdimg_output, $radius);
                     break;
                 case 'gblr':
                     phpthumb_filters::BlurGaussian($this->gdimg_output);
                     break;
                 case 'sblr':
                     phpthumb_filters::BlurSelective($this->gdimg_output);
                     break;
                 case 'mean':
                     phpthumb_filters::MeanRemoval($this->gdimg_output);
                     break;
                 case 'smth':
                     phpthumb_filters::Smooth($this->gdimg_output, $parameter);
                     break;
                 case 'usm':
                     @(list($amount, $radius, $threshold) = explode('|', $parameter));
                     $amount = $amount ? $amount : 80;
                     $radius = $radius ? $radius : 0.5;
                     $threshold = strlen($threshold) ? $threshold : 3;
                     if (phpthumb_functions::gd_version() >= 2.0) {
                         ob_start();
                         if (!@(include_once dirname(__FILE__) . '/phpthumb.unsharp.php')) {
                             $include_error = ob_get_contents();
                             if ($include_error) {
                                 $this->DebugMessage('include_once("' . dirname(__FILE__) . '/phpthumb.unsharp.php") generated message: "' . $include_error . '"', __FILE__, __LINE__);
                             }
                             ob_end_clean();
                             $this->DebugMessage('Error including "' . dirname(__FILE__) . '/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__);
                             return false;
                         }
                         ob_end_clean();
                         phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold);
                     } else {
                         $this->DebugMessage('Skipping unsharp mask because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                         return false;
                     }
                     break;
             }
         }
     }
     return true;
 }
 function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color = '000000', $ttffont = '', $opacity = 100, $margin = 5, $angle = 0, $bg_color = false, $bg_opacity = 0, $fillextend = '')
 {
     // text watermark requested
     if (!$text) {
         return false;
     }
     ImageAlphaBlending($gdimg, true);
     $metaTextArray = array('^Fb' => $this->phpThumbObject->getimagesizeinfo['filesize'], '^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024), '^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576), '^X' => $this->phpThumbObject->getimagesizeinfo[0], '^Y' => $this->phpThumbObject->getimagesizeinfo[1], '^x' => ImageSX($gdimg), '^y' => ImageSY($gdimg), '^^' => '^');
     $text = strtr($text, $metaTextArray);
     $text = str_replace("\r\n", "\n", $text);
     $text = str_replace("\r", "\n", $text);
     $textlines = explode("\n", $text);
     if (@is_readable($ttffont) && is_file($ttffont)) {
         $opacity = 100 - intval(max(min($opacity, 100), 0));
         $this->DebugMessage('Using TTF font "' . $ttffont . '"', __FILE__, __LINE__);
         $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
         $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         //$text_width = round($max_x - $min_x + ($size * 0.5));
         $text_width = round($max_x - $min_x);
         $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         //$text_height = round($max_y - $min_y + ($size * 0.5));
         $text_height = round($max_y - $min_y);
         $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'jH');
         $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
         $char_height = round($char_max_y - $char_min_y);
         switch ($alignment) {
             case 'T':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'B':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
                 break;
             case 'L':
                 $text_origin_x = $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'R':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'C':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
                 break;
             case 'TL':
                 $text_origin_x = $margin;
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'TR':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = $char_height + $margin;
                 break;
             case 'BL':
                 $text_origin_x = $margin;
                 $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
                 break;
             case 'BR':
             default:
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
                 break;
         }
         $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
         if ($alignment == '*') {
             $text_origin_y = $char_height + $margin;
             while ($text_origin_y - $text_height < ImageSY($gdimg)) {
                 $text_origin_x = $margin;
                 while ($text_origin_x < ImageSX($gdimg)) {
                     ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
                     $text_origin_x += $text_width + $margin;
                 }
                 $text_origin_y += $text_height + $margin;
             }
         } else {
             //ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
             if (phpthumb_functions::IsHexColor($bg_color)) {
                 $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, $bg_color, false, $text_background_alpha);
             } else {
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, 'FFFFFF', false, 127);
             }
             $x1 = $text_origin_x + $min_x;
             $y1 = $text_origin_y + $TTFbox[1];
             $x2 = $text_origin_x + $min_x + $text_width;
             $y2 = $text_origin_y + $TTFbox[1] - $text_height;
             $x_TL = eregi('x', $fillextend) ? 0 : min($x1, $x2);
             $y_TL = eregi('y', $fillextend) ? 0 : min($y1, $y2);
             $x_BR = eregi('x', $fillextend) ? ImageSX($gdimg) : max($x1, $x2);
             $y_BR = eregi('y', $fillextend) ? ImageSY($gdimg) : max($y1, $y2);
             //while ($y_BR > ImageSY($gdimg)) {
             //	$y_TL--;
             //	$y_BR--;
             //	$text_origin_y--;
             //}
             ImageFilledRectangle($gdimg, $x_TL, $y_TL, $x_BR, $y_BR, $text_color_background);
             ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
         }
         return true;
     } else {
         $size = min(5, max(1, $size));
         $this->DebugMessage('Using built-in font (size=' . $size . ') for text watermark' . ($ttffont ? ' because $ttffont !is_readable(' . $ttffont . ')' : ''), __FILE__, __LINE__);
         $text_width = 0;
         $text_height = 0;
         foreach ($textlines as $dummy => $line) {
             $text_width = max($text_width, ImageFontWidth($size) * strlen($line));
             $text_height += ImageFontHeight($size);
         }
         if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
             ImageAlphaBlending($img_watermark, false);
             if (phpthumb_functions::IsHexColor($bg_color)) {
                 $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, $bg_color, false, $text_background_alpha);
             } else {
                 $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, 'FFFFFF', false, 127);
             }
             ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
             if ($angle && function_exists('ImageRotate')) {
                 // using $img_watermark_mask is pointless if ImageRotate function isn't available
                 if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
                     $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
                     ImageAlphaBlending($img_watermark_mask, false);
                     ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
                     $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
                 }
             }
             $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
             foreach ($textlines as $key => $line) {
                 switch ($alignment) {
                     case 'C':
                     case 'T':
                     case 'B':
                         $x_offset = round(($text_width - ImageFontWidth($size) * strlen($line)) / 2);
                         break;
                     case 'L':
                     case 'TL':
                     case 'BL':
                         $x_offset = 0;
                         break;
                     case 'R':
                     case 'TR':
                     case 'BR':
                     default:
                         $x_offset = $text_width - ImageFontWidth($size) * strlen($line);
                         break;
                 }
                 ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
                 if ($angle && $img_watermark_mask) {
                     ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
                 }
             }
             if ($angle && $img_watermark_mask) {
                 $img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
                 $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
                 phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
             }
             phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
             ImageDestroy($img_watermark);
             return true;
         }
     }
     return false;
 }
 function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color = '000000', $ttffont = '', $opacity = 100, $margin = 5, $angle = 0)
 {
     // text watermark requested
     if (!$text) {
         return false;
     }
     $opacity = max(min($opacity, 100), 0);
     if (@is_readable($ttffont) && is_file($ttffont)) {
         $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
         $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
         //$text_width = round($max_x - $min_x + ($size * 0.5));
         $text_width = round($max_x - $min_x);
         $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
         //$text_height = round($max_y - $min_y + ($size * 0.5));
         $text_height = round($max_y - $min_y);
         switch ($alignment) {
             case 'T':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = $text_height + $margin;
                 break;
             case 'B':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = ImageSY($gdimg) - $text_height + $TTFbox[1] - $min_y - $margin;
                 break;
             case 'L':
                 $text_origin_x = $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $text_height;
                 break;
             case 'R':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $text_height;
                 break;
             case 'C':
                 $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
                 $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $text_height;
                 break;
             case 'TL':
                 $text_origin_x = $margin;
                 $text_origin_y = $text_height + $margin;
                 break;
             case 'TR':
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = $text_height + $margin;
                 break;
             case 'BL':
                 $text_origin_x = $margin;
                 $text_origin_y = ImageSY($gdimg) - $text_height + $TTFbox[1] - $min_y - $margin;
                 break;
             case 'BR':
             default:
                 $text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
                 $text_origin_y = ImageSY($gdimg) - $text_height + $TTFbox[1] - $min_y - $margin;
                 break;
         }
         $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, $opacity * 1.27);
         if ($alignment == '*') {
             $text_origin_y = $text_height + $margin;
             while ($text_origin_y - $text_height < ImageSY($gdimg)) {
                 $text_origin_x = $margin;
                 while ($text_origin_x < ImageSX($gdimg)) {
                     ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
                     $text_origin_x += $text_width + $margin;
                 }
                 $text_origin_y += $text_height + $margin;
             }
         } else {
             ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
         }
         return true;
         //if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
         //	if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
         //		$letter_color_background      = ImageColorAllocateAlpha($img_watermark,      127, 127, 127, 127);
         //		$letter_color_background_mask = ImageColorAllocateAlpha($img_watermark_mask, 127, 127, 127, 127);
         //		ImageFilledRectangle($img_watermark,      0, 0, ImageSX($img_watermark),      ImageSY($img_watermark),      $letter_color_background);
         //		ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $letter_color_background_mask);
         //
         //		$letter_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
         //		$letter_color_watermark_mask = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
         //
         //		ImageTTFtext($img_watermark,      $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y,  $letter_color_watermark,      $ttffont, $text);
         //		ImageTTFtext($img_watermark_mask, $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y, -$letter_color_watermark_mask, $ttffont, $text);
         //
         //		$this->ApplyMask($img_watermark_mask, $img_watermark);
         //		$this->WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
         //
         //		ImageDestroy($img_watermark);
         //		ImageDestroy($img_watermark_mask);
         //		return true;
         //	}
         //}
     } else {
         if ($ttffont) {
             //$this->DebugMessage('Using built-in font for text watermark because $ttffont !is_readable('.$ttffont.')', __FILE__, __LINE__);
         }
         $size = min(5, max(1, $size));
         $text_width = ImageFontWidth($size) * strlen($text);
         $text_height = ImageFontHeight($size);
         if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
             ImageAlphaBlending($img_watermark, false);
             $text_color_background = ImageColorAllocateAlpha($img_watermark, 255, 255, 255, 127);
             ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
             $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
             ImageString($img_watermark, $size, 0, 0, $text, $text_color_watermark);
             if ($angle) {
                 if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
                     $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
                     ImageAlphaBlending($img_watermark_mask, false);
                     ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
                     $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
                     ImageString($img_watermark_mask, $size, 0, 0, $text, $mask_color_watermark);
                     $img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
                     $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
                 }
                 phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
             }
             phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
             ImageDestroy($img_watermark);
             return true;
         }
     }
     return false;
 }