Esempio n. 1
0
 public function rotate($angle, $background = 'FFFFFF')
 {
     $angle = intval($angle);
     if (!is_int($angle) || $angle < 0 || $angle > 360) {
         throw new ezcBaseValueException('height', $angle, 'int > 0 && int < 360');
     }
     $resource = $this->getActiveResource();
     $bg = $this->bgArrayFromHex($background);
     $gdBackgroundColor = imagecolorallocatealpha($resource, $bg['r'], $bg['g'], $bg['b'], $bg['a']);
     $newResource = ImageRotate($resource, $angle, $gdBackgroundColor);
     if ($newResource === false) {
         throw new ezcImageFilterFailedException('rotate', 'Rotation of image failed.');
     }
     imagedestroy($resource);
     $this->setActiveResource($newResource);
 }
Esempio n. 2
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;
 }
Esempio n. 3
0
File: GD.php Progetto: joeymetal/v1
 /**
  * Rotates image by the given angle
  *
  * Uses a fast rotation algorythm for custom angles
  * or lines copy for multiple of 90 degrees
  *
  * @param int   $angle   Rotation angle
  * @param array $options array(
  *                             'canvasColor' => array(r ,g, b), named color or #rrggbb
  *                            )
  * @author Pierre-Alain Joye
  * @return bool|PEAR_Error TRUE or a PEAR_Error object on error
  * @access public
  */
 function rotate($angle, $options = null)
 {
     if ($angle % 360 == 0) {
         return true;
     }
     $color_mask = $this->_getColor('canvasColor', $options, array(255, 255, 255));
     $mask = imagecolorresolve($this->imageHandle, $color_mask[0], $color_mask[1], $color_mask[2]);
     $this->old_image = $this->imageHandle;
     // Multiply by -1 to change the sign, so the image is rotated clockwise
     $this->imageHandle = ImageRotate($this->imageHandle, $angle * -1, $mask);
     return true;
 }
 function Rotate()
 {
     if (!function_exists('ImageRotate')) {
         return false;
     }
     if (!empty($this->ra) || !empty($this->ar)) {
         $this->config_background_hexcolor = !empty($this->bg) ? $this->bg : $this->config_background_hexcolor;
         if (!eregi('^[0-9A-F]{6}$', $this->config_background_hexcolor)) {
             $this->ErrorImage('Invalid hex color string "' . $this->config_background_hexcolor . '" for parameter "bg"');
         }
         $rotate_angle = 0;
         if (!empty($this->ra)) {
             $rotate_angle = floatval($this->ra);
         } else {
             if ($this->ar == 'l' && $this->source_height > $this->source_width) {
                 $rotate_angle = 270;
             } elseif ($this->ar == 'L' && $this->source_height > $this->source_width) {
                 $rotate_angle = 90;
             } elseif ($this->ar == 'p' && $this->source_width > $this->source_height) {
                 $rotate_angle = 90;
             } elseif ($this->ar == 'P' && $this->source_width > $this->source_height) {
                 $rotate_angle = 270;
             }
         }
         while ($rotate_angle < 0) {
             $rotate_angle += 360;
         }
         $rotate_angle = $rotate_angle % 360;
         if ($rotate_angle != 0) {
             if (ImageColorTransparent($this->gdimg_source) >= 0) {
                 // ImageRotate() forgets all about an image's transparency and sets the transparent color to black
                 // To compensate, flood-fill the transparent color of the source image with the specified background color first
                 // then rotate and the colors should match
                 if (!function_exists('ImageIsTrueColor') || !ImageIsTrueColor($this->gdimg_source)) {
                     // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
                     $this->source_width = ImageSX($this->gdimg_source);
                     $this->source_height = ImageSY($this->gdimg_source);
                     $gdimg_newsrc = $this->ImageCreateFunction($this->source_width, $this->source_height);
                     $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $this->config_background_hexcolor);
                     ImageFilledRectangle($gdimg_newsrc, 0, 0, $this->source_width, $this->source_height, phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $this->config_background_hexcolor));
                     ImageCopy($gdimg_newsrc, $this->gdimg_source, 0, 0, 0, 0, $this->source_width, $this->source_height);
                     ImageDestroy($this->gdimg_source);
                     unset($this->gdimg_source);
                     $this->gdimg_source = $gdimg_newsrc;
                     unset($gdimg_newsrc);
                 } else {
                     ImageColorSet($this->gdimg_source, ImageColorTransparent($this->gdimg_source), hexdec(substr($this->config_background_hexcolor, 0, 2)), hexdec(substr($this->config_background_hexcolor, 2, 2)), hexdec(substr($this->config_background_hexcolor, 4, 2)));
                     ImageColorTransparent($this->gdimg_source, -1);
                 }
             }
             $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_source, $this->config_background_hexcolor);
             $this->gdimg_source = ImageRotate($this->gdimg_source, $rotate_angle, $background_color);
             $this->source_width = ImageSX($this->gdimg_source);
             $this->source_height = ImageSY($this->gdimg_source);
         }
     }
     return true;
 }
Esempio n. 5
0
 function Rotate()
 {
     if ($this->ra || $this->ar) {
         if (!function_exists('ImageRotate')) {
             $this->DebugMessage('!function_exists(ImageRotate)', __FILE__, __LINE__);
             return false;
         }
         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;
         }
         $this->config_background_hexcolor = $this->bg ? $this->bg : $this->config_background_hexcolor;
         if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
             return $this->ErrorImage('Invalid hex color string "' . $this->config_background_hexcolor . '" for parameter "bg"');
         }
         $rotate_angle = 0;
         if ($this->ra) {
             $rotate_angle = floatval($this->ra);
         } else {
             if ($this->ar == 'x') {
                 if (phpthumb_functions::version_compare_replacement(phpversion(), '4.2.0', '>=')) {
                     if ($this->sourceFilename) {
                         if (function_exists('exif_read_data')) {
                             if ($exif_data = @exif_read_data($this->sourceFilename, 'IFD0')) {
                                 // http://sylvana.net/jpegcrop/exif_orientation.html
                                 switch (@$exif_data['Orientation']) {
                                     case 1:
                                         $rotate_angle = 0;
                                         break;
                                     case 3:
                                         $rotate_angle = 180;
                                         break;
                                     case 6:
                                         $rotate_angle = 270;
                                         break;
                                     case 8:
                                         $rotate_angle = 90;
                                         break;
                                     default:
                                         $this->DebugMessage('EXIF auto-rotate failed because unknown $exif_data[Orientation] "' . @$exif_data['Orientation'] . '"', __FILE__, __LINE__);
                                         return false;
                                         break;
                                 }
                                 $this->DebugMessage('EXIF auto-rotate set to ' . $rotate_angle . ' degrees ($exif_data[Orientation] = "' . @$exif_data['Orientation'] . '")', __FILE__, __LINE__);
                             } else {
                                 $this->DebugMessage('failed: exif_read_data(' . $this->sourceFilename . ')', __FILE__, __LINE__);
                                 return false;
                             }
                         } else {
                             $this->DebugMessage('!function_exists(exif_read_data)', __FILE__, __LINE__);
                             return false;
                         }
                     } else {
                         $this->DebugMessage('Cannot auto-rotate from EXIF data because $this->sourceFilename is empty', __FILE__, __LINE__);
                         return false;
                     }
                 } else {
                     $this->DebugMessage('Cannot auto-rotate from EXIF data because PHP is less than v4.2.0 (' . phpversion() . ')', __FILE__, __LINE__);
                     return false;
                 }
             } elseif ($this->ar == 'l' && $this->source_height > $this->source_width) {
                 $rotate_angle = 270;
             } elseif ($this->ar == 'L' && $this->source_height > $this->source_width) {
                 $rotate_angle = 90;
             } elseif ($this->ar == 'p' && $this->source_width > $this->source_height) {
                 $rotate_angle = 90;
             } elseif ($this->ar == 'P' && $this->source_width > $this->source_height) {
                 $rotate_angle = 270;
             }
         }
         while ($rotate_angle < 0) {
             $rotate_angle += 360;
         }
         $rotate_angle = $rotate_angle % 360;
         if ($rotate_angle != 0) {
             $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_source, $this->config_background_hexcolor);
             if (phpthumb_functions::gd_version() >= 2 && $this->thumbnailFormat == 'png' && !$this->bg && $rotate_angle % 90) {
                 if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_source), ImageSY($this->gdimg_source))) {
                     $this->gdimg_source = ImageRotate($this->gdimg_source, $rotate_angle, $background_color);
                     $color_mask_opaque = ImageColorAllocate($gdimg_rotate_mask, 0xff, 0xff, 0xff);
                     $color_mask_transparent = ImageColorAllocate($gdimg_rotate_mask, 0x0, 0x0, 0x0);
                     ImageFilledRectangle($gdimg_rotate_mask, 0, 0, ImageSX($gdimg_rotate_mask), ImageSY($gdimg_rotate_mask), $color_mask_opaque);
                     $gdimg_rotate_mask = ImageRotate($gdimg_rotate_mask, $rotate_angle, $color_mask_transparent);
                     ImageAlphaBlending($this->gdimg_source, false);
                     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
                         ImageSaveAlpha($this->gdimg_source, true);
                     }
                     $this->is_alpha = true;
                     phpthumb_filters::ApplyMask($gdimg_rotate_mask, $this->gdimg_source);
                     ImageDestroy($gdimg_rotate_mask);
                     $this->source_width = ImageSX($this->gdimg_source);
                     $this->source_height = ImageSY($this->gdimg_source);
                 } else {
                     $this->DebugMessage('ImageCreateFromStringReplacement() failed for "' . $MaskFilename . '"', __FILE__, __LINE__);
                 }
             } else {
                 if (phpthumb_functions::gd_version() >= 2) {
                     $this->DebugMessage('Using non-alpha rotate because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
                 } else {
                     $this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "' . $this->thumbnailFormat . '"', __FILE__, __LINE__);
                 }
                 if (ImageColorTransparent($this->gdimg_source) >= 0) {
                     // ImageRotate() forgets all about an image's transparency and sets the transparent color to black
                     // To compensate, flood-fill the transparent color of the source image with the specified background color first
                     // then rotate and the colors should match
                     if (!function_exists('ImageIsTrueColor') || !ImageIsTrueColor($this->gdimg_source)) {
                         // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
                         $this->source_width = ImageSX($this->gdimg_source);
                         $this->source_height = ImageSY($this->gdimg_source);
                         $gdimg_newsrc = phpthumb_functions::ImageCreateFunction($this->source_width, $this->source_height);
                         $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $this->config_background_hexcolor);
                         ImageFilledRectangle($gdimg_newsrc, 0, 0, $this->source_width, $this->source_height, phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $this->config_background_hexcolor));
                         ImageCopy($gdimg_newsrc, $this->gdimg_source, 0, 0, 0, 0, $this->source_width, $this->source_height);
                         ImageDestroy($this->gdimg_source);
                         unset($this->gdimg_source);
                         $this->gdimg_source = $gdimg_newsrc;
                         unset($gdimg_newsrc);
                     } else {
                         ImageColorSet($this->gdimg_source, ImageColorTransparent($this->gdimg_source), hexdec(substr($this->config_background_hexcolor, 0, 2)), hexdec(substr($this->config_background_hexcolor, 2, 2)), hexdec(substr($this->config_background_hexcolor, 4, 2)));
                         ImageColorTransparent($this->gdimg_source, -1);
                     }
                 }
                 $this->gdimg_source = ImageRotate($this->gdimg_source, $rotate_angle, $background_color);
                 $this->source_width = ImageSX($this->gdimg_source);
                 $this->source_height = ImageSY($this->gdimg_source);
             }
         }
     }
     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;
 }
 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;
 }
Esempio n. 8
0
 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;
 }
Esempio n. 9
0
 /**
  * rotate an image using GD library;
  * @param string $filename to the source file
  * @param integer $degrees to rotate the image clockwise;
  * @return nothing;
  * @access private
  */
 function rotate_gd($sourceFileName, $degree)
 {
     if (!$this->gdInfo >= 1 || !$this->checkGdFileType($sourceFileName)) {
         return false;
     }
     if (!function_exists('imagerotate')) {
         return false;
     }
     $img =& $this->getImg($sourceFileName);
     if ($this->hasError()) {
         return false;
     }
     $srcWidth = ImageSX($img);
     $srcHeight = ImageSY($img);
     if ($srcWidth > $srcHeight) {
         $newd = $srcWidth;
         $corection = $srcWidth - $srcHeight;
         $landscape = 1;
     } else {
         $newd = $srcHeight;
         $corection = $srcHeight - $srcWidth;
         $landscape = 0;
     }
     $degree = 360 - $degree;
     switch ($degree) {
         case 360:
         case 0:
             $w = $srcWidth;
             $h = $srcHeight;
             $x1 = 0;
             $y1 = 0;
             break;
         case 180:
             $w = $srcWidth;
             $h = $srcHeight;
             if ($landscape) {
                 $x1 = 0;
                 $y1 = $corection;
             } else {
                 $x1 = $corection;
                 $y1 = 0;
             }
             break;
         case 90:
             $w = $srcHeight;
             $h = $srcWidth;
             if ($landscape) {
                 $x1 = 0;
                 $y1 = 0;
             } else {
                 $x1 = 0;
                 $y1 = $corection;
             }
             break;
         case 270:
             $w = $srcHeight;
             $h = $srcWidth;
             if ($landscape) {
                 $x1 = $corection;
                 $y1 = 0;
             } else {
                 $x1 = 0;
                 $y1 = 0;
             }
             break;
     }
     $destImage =& $this->getImageCreate($newd, $newd);
     $finalImage =& $this->getImageCreate($w, $h);
     $this->getImageCopyResampled($destImage, $img, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
     $rotatedImage = ImageRotate($destImage, $degree, 0);
     $this->getImageCopyResampled($finalImage, $rotatedImage, 0, 0, 0 + $x1, 0 + $y1, $w, $h, $w, $h);
     ImageDestroy($img);
     $this->createNewImg($finalImage, $sourceFileName, $this->qualityLevel);
     return true;
 }
Esempio n. 10
0
    /**
     * Output to the canvas
     * @param int $type The type of image to output, i.e. IMG_PNG (default) and IMG_JPEG
     * @access private
     */
    function _done($type = IMG_PNG)
    {
        $timeStart = $this->_getMicroTime();
        $this->_debug("Output started $timeStart");
        
        if ($this->_shadow) {
            $this->setPadding(20);
            $this->_setCoords($this->_left, $this->_top, $this->_right -10, $this->_bottom-10);
        }

        $this->_updateCoords();
        

        if ($this->_fillStyle) {
            ImageFilledRectangle($this->_canvas(), $this->_left, $this->_top, $this->_right, $this->_bottom, $this->_getFillStyle());
        }

        if (!file_exists(dirname(__FILE__)."/Graph/Images/logo.png")) {
            $error = "Could not find Logo your installation may be incomplete";
            ImageLine($this->_canvas(), 0, 0, $this->width(), $this->height(), $this->_color(IMAGE_GRAPH_RED));
            ImageLine($this->_canvas(), $this->width(), 0, 0, $this->height(), $this->_color(IMAGE_GRAPH_RED));
            ImageString($this->_canvas(), IMAGE_GRAPH_FONT, ($this->width() - ImageFontWidth(IMAGE_GRAPH_FONT) * strlen($error)) / 2, ($this->height() - ImageFontHeight(IMAGE_GRAPH_FONT)) / 2, $error, $this->_color(IMAGE_GRAPH_RED));
        } else {
            parent::_done();
        }
        
        if (isset($this->_borderStyle)) {
            $this->_debug("Drawing border");
            ImageRectangle($this->_canvas(), $this->_left, $this->_top, $this->_right, $this->_bottom, $this->_getBorderStyle());
        }

        if (($this->_outputImage) and (!IMAGE_GRAPH_DEBUG)) {
            header("Expires: Tue, 2 Jul 1974 17:41:00 GMT"); // Date in the past
            header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
            header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
            header("Pragma: no-cache");
            header("Content-type: image/". ($type == IMG_JPG ? "jpeg" : "png"));            
            header("Content-Disposition: attachment; filename = \"". (isset($_GET['thumb']) ? $_GET['thumb'] : (isset($_GET['image']) ? $_GET['image'] : ""))."\"");
        }

        if ($this->_rotation) {
            $this->_canvas = ImageRotate($this->_canvas(), $this->_rotation, $this->_getFillStyle());
        }

        $timeEnd = $this->_getMicroTime();
        $this->_debug("Output ended $timeEnd");

        if ($this->_showTime) {
            ImageString($this->_canvas(), FONT, $this->_left + $this->width() * 0.15, $this->_bottom - $this->_height * 0.1 - ImageFontHeight(IMAGE_GRAPH_FONT), "Generated in ".sprintf("%0.3f", $timeEnd - $timeStart)." sec", $this->_color(IMAGE_GRAPH_RED));
        }

        if (!$this->_hideLogo) {
            $logo = new Image_Graph_Logo(dirname(__FILE__)."/Graph/Images/logo.png", IMAGE_GRAPH_ALIGN_TOP_RIGHT);
            $logo->_setParent($this);
            $logo->_done();
        }

        if ($this->_antialias) {
            $this->_performAntialias();
        }

        if ($this->_fileName) {
            if (strtolower(substr($this->_fileName, -4)) == ".png") {
                ImagePNG($this->_canvas(), $this->_fileName);
            } else {
                ImageJPEG($this->_canvas(), $this->_fileName);
            }
        }

        if (($this->_thumbWidth) and ($this->_thumbHeight)) {
            if (isset($GLOBALS['_Image_Graph_gd2'])) {
                $thumbnail = ImageCreateTrueColor($this->_thumbWidth, $this->_thumbHeight);
                ImageCopyResampled($thumbnail, $this->_canvas(), 0, 0, 0, 0, $this->_thumbWidth, $this->_thumbHeight, $this->width(), $this->height());
            } else {
                $thumbnail = ImageCreate($this->_thumbWidth, $this->_thumbHeight);
                ImageCopyResized($thumbnail, $this->_canvas(), 0, 0, 0, 0, $this->_thumbWidth, $this->_thumbHeight, $this->width(), $this->height());
            }

            if ($this->_thumbFileName) {
                if (strtolower(substr($this->_thumbFileName, -4)) == ".png") {
                    ImagePNG($thumbnail, $this->_thumbFileName);
                } else {
                    ImageJPEG($thumbnail, $this->_thumbFileName);
                }
                ImageDestroy($thumbnail);
            } else {
                ImageDestroy($this->_canvas());
                $this->_canvas = $thumbnail;
            }
        }
	
        if (($this->_outputImage) and (!IMAGE_GRAPH_DEBUG)) {
            if ($type == IMG_JPG) {
                ImageJPEG($this->_canvas());
            } else {
                ImagePNG($this->_canvas());
            }
        }

        //ImageDestroy($this->_canvas());
        $this->_debug("Completely done", true);
    }
Esempio n. 11
0
 function rotate($d)
 {
     $this->image = ImageRotate($this->image, $d, 0);
 }
Esempio n. 12
0
 function OutputImage($im, $format, $quality, $uuid)
 {
     switch ($format) {
         case "JPEG":
             $im = ImageRotate($im, 0, 0);
             ImageJPEG($im, "/usr/share/cpm/tmp/{$uuid}.jpg", $quality);
             break;
         case "PNG":
             ImagePNG($im);
             break;
         case "GIF":
             ImageGIF($im);
             break;
     }
 }
 public static function edit_image($imagedata, $output_format = "jpg", $output_filename = "", $output_quality = 75, $width = "", $height = "", $keep_aspect_ratio = true, $interlace = true, $crop_x = 0, $crop_y = 0, $crop_width = -1, $crop_height = -1, $rotate_angle = 0, $fitinside = false)
 {
     $output_format = strtolower($output_format);
     if ($output_format === 'jpeg') {
         $output_format = "jpg";
     }
     $_fromFile = strlen($imagedata) < 255 && @file_exists($imagedata);
     // Output format is available
     if (in_array($output_format, self::supported_image_types())) {
         // Set quality for JPG images
         if ($output_format === 'jpg') {
             // Keep quality between 1 and 99
             $output_quality = max(1, min(99, is_int($output_quality) ? $output_quality : 75));
         }
         $_gdimg = $_fromFile ? self::ImageCreateFromFileReplacement($imagedata) : self::ImageCreateFromStringReplacement($imagedata);
         // Now we need to ensure that we could read the file
         if ($_gdimg) {
             // Detect dimension of image
             $_width = ImageSX($_gdimg);
             $_height = ImageSY($_gdimg);
             if ($rotate_angle != 0 && function_exists("ImageRotate")) {
                 $rotate_angle = floatval($rotate_angle);
                 while ($rotate_angle < 0) {
                     $rotate_angle += 360;
                 }
                 $rotate_angle = $rotate_angle % 360;
                 if ($rotate_angle != 0) {
                     $_gdimg = ImageRotate($_gdimg, $rotate_angle, 0);
                     $_width = ImageSX($_gdimg);
                     $_height = ImageSY($_gdimg);
                 }
             }
             $_outsize = self::calculate_image_size($_width, $_height, $width, $height, $keep_aspect_ratio, true, $fitinside);
             // Decide, which functions to use (depends on version of GD library)
             $_image_create_function = self::gd_version() >= 2.0 ? "imagecreatetruecolor" : "imagecreate";
             $_image_resize_function = function_exists('imagecopyresampled') ? "imagecopyresampled" : "imagecopyresized";
             $_outsize["width"] = max(1, $_outsize["width"]);
             $_outsize["height"] = max(1, $_outsize["height"]);
             // Now create the image
             $_output_gdimg = $_image_create_function($_outsize["width"], $_outsize["height"]);
             // this image is always black
             /* $GDInfo = self::gd_info();
             		  // DEBIAN EDGE FIX => crashes at imagefill, so use old Method
             		  if($GDInfo["GD Version"] === '2.0 or higher' && !function_exists("imagerotate")){
             		  // set black to transparent!
             		  if($output_format === 'gif' || $output_format === 'png'){ // transparency with gifs
             		  imagecolortransparent($_output_gdimg, imagecolorallocate($_output_gdimg, 0, 0, 0)); // set this color to transparent - done
             		  }
             		  } else {
             		 */
             // preserve transparency of png and gif images:
             switch ($output_format) {
                 case "gif":
                     $colorTransparent = imagecolortransparent($_gdimg);
                     imagepalettecopy($_gdimg, $_output_gdimg);
                     imagefill($_output_gdimg, 0, 0, $colorTransparent);
                     imagecolortransparent($_output_gdimg, $colorTransparent);
                     imagetruecolortopalette($_output_gdimg, true, 256);
                     break;
                 case "png":
                     imagealphablending($_output_gdimg, false);
                     //$transparent = imagecolorallocatealpha($_output_gdimg, 0, 0, 0, 127);
                     $transparent = imagecolorallocatealpha($_output_gdimg, 255, 255, 255, 127);
                     imagefill($_output_gdimg, 0, 0, $transparent);
                     imagesavealpha($_output_gdimg, true);
                     break;
                 default:
             }
             //}
             // Resize image
             //if($_outsize["width"] == "1")
             if ($fitinside && $keep_aspect_ratio && $width && $height) {
                 $wratio = $width / $_width;
                 $hratio = $height / $_height;
                 $ratio = max($width / $_width, $height / $_height);
                 $h = $height / $ratio;
                 $w = $width / $ratio;
                 if ($wratio < $hratio) {
                     $x = ($_width - $width / $ratio) / 2;
                     $y = 0;
                 } else {
                     $x = 0;
                     $y = ($_height - $height / $ratio) / 2;
                 }
                 // Set thumbnail focus point -ah2015
                 if (true) {
                     echo '<script>console.log("TX:"+' . $x . ', "TY:"+' . $y . ', "OWidth:"+' . $_width . ', "TWidth"+' . $width . ', "OHeight:"+' . $_height . ', "THeight"+' . $height . ');</script>';
                     $x_focus = $crop_x;
                     // von -1.0 bis 1.0
                     $y_focus = $crop_y;
                     // von -1.0 bis 1.0
                     $x = $x + $x * $x_focus;
                     $y = $y + $y * $y_focus;
                 }
                 $_image_resize_function($_output_gdimg, $_gdimg, 0, 0, $x, $y, $width, $height, $w, $h);
             } else {
                 $_image_resize_function($_output_gdimg, $_gdimg, 0, 0, 0, 0, $_outsize["width"], $_outsize["height"], $_width, $_height);
             }
             // PHP 4.4.1 GDLIB-Bug/Safemode - Workarround
             if ($output_filename != "" && file_exists($output_filename)) {
                 touch($output_filename);
             }
             ImageInterlace($_output_gdimg, $interlace ? 1 : 0);
             switch ($output_format) {
                 case 'jpg':
                     // Output to a filename or directly
                     if ($output_filename != "") {
                         $_gdimg = imagejpeg($_output_gdimg, $output_filename, $output_quality);
                         if ($_gdimg) {
                             $_gdimg = basename($output_filename);
                         }
                     } elseif ($_tempfilename = tempnam(TEMP_PATH, "")) {
                         imagejpeg($_output_gdimg, $_tempfilename, $output_quality);
                         $_gdimg = we_base_file::load($_tempfilename);
                         // As we read the temporary file we no longer need it
                         //unlink($_tempfilename);
                     }
                     break;
                 case 'png':
                 case 'gif':
                     // Set output function
                     $_image_out_function = 'image' . $output_format;
                     // Output to a filename or directly
                     if ($output_filename) {
                         $_gdimg = $_image_out_function($_output_gdimg, $output_filename);
                         if ($_gdimg) {
                             $_gdimg = basename($output_filename);
                         }
                     } elseif ($_tempfilename = tempnam(TEMP_PATH, "")) {
                         $_image_out_function($_output_gdimg, $_tempfilename);
                         $_gdimg = we_base_file::load($_tempfilename);
                         // As we read the temporary file we no longer need it
                         unlink($_tempfilename);
                     }
                     break;
             }
             ImageDestroy($_output_gdimg);
         }
         return isset($_gdimg) ? array($_gdimg, $_outsize["width"], $_outsize["height"]) : array(false, -1, -1);
     }
     return array(false, -1, -1);
 }