[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wysiwyg/shared/tiny_mce/plugins/ibrowser/scripts/phpThumb/ -> phpthumb.filters.php (source)

   1  <?php
   2  //////////////////////////////////////////////////////////////
   3  ///  phpThumb() by James Heinrich <info@silisoftware.com>   //
   4  //        available at http://phpthumb.sourceforge.net     ///
   5  //////////////////////////////////////////////////////////////
   6  ///                                                         //
   7  // phpthumb.filters.php - image processing filter functions //
   8  //                                                         ///
   9  //////////////////////////////////////////////////////////////
  10  
  11  class phpthumb_filters {
  12  
  13      var $phpThumbObject = null;
  14  
  15  	function phpthumb_filters() {
  16          return true;
  17      }
  18  
  19  	function ApplyMask(&$gdimg_mask, &$gdimg_image) {
  20          if (phpthumb_functions::gd_version() < 2) {
  21              $this->DebugMessage('Skipping ApplyMask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  22              return false;
  23          }
  24          if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
  25  
  26              $this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__);
  27              if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
  28  
  29                  ImageCopyResampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, ImageSX($gdimg_image), ImageSY($gdimg_image), ImageSX($gdimg_mask), ImageSY($gdimg_mask));
  30                  if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
  31  
  32                      $color_background = ImageColorAllocate($gdimg_mask_blendtemp, 0, 0, 0);
  33                      ImageFilledRectangle($gdimg_mask_blendtemp, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp), $color_background);
  34                      ImageAlphaBlending($gdimg_mask_blendtemp, false);
  35                      ImageSaveAlpha($gdimg_mask_blendtemp, true);
  36                      for ($x = 0; $x < ImageSX($gdimg_image); $x++) {
  37                          for ($y = 0; $y < ImageSY($gdimg_image); $y++) {
  38                              //$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y);
  39                              $RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y);
  40                              $MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y));
  41                              $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127)));
  42                              $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha);
  43                              ImageSetPixel($gdimg_mask_blendtemp, $x, $y, $newcolor);
  44                          }
  45                      }
  46                      ImageAlphaBlending($gdimg_image, false);
  47                      ImageSaveAlpha($gdimg_image, true);
  48                      ImageCopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp));
  49                      ImageDestroy($gdimg_mask_blendtemp);
  50  
  51                  } else {
  52                      $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  53                  }
  54                  ImageDestroy($gdimg_mask_resized);
  55  
  56              } else {
  57                  $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  58              }
  59  
  60          } else {
  61              // alpha merging requires PHP v4.3.2+
  62              $this->DebugMessage('Skipping ApplyMask() technique because PHP is v"'.phpversion().'"', __FILE__, __LINE__);
  63          }
  64          return true;
  65      }
  66  
  67  
  68  	function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) {
  69          $width     = ($width     ? $width     : 5);
  70          $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
  71          $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
  72  
  73          ImageAlphaBlending($gdimg, true);
  74          for ($i = 0; $i < $width; $i++) {
  75              $alpha = round(($i / $width) * 127);
  76              $color1[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha);
  77              $color2[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha);
  78  
  79              ImageLine($gdimg,                   $i,                   $i,                   $i, ImageSY($gdimg) - $i, $color1[$i]); // left
  80              ImageLine($gdimg,                   $i,                   $i, ImageSX($gdimg) - $i,                   $i, $color1[$i]); // top
  81              ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i,                   $i, $color2[$i]); // right
  82              ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i,                   $i, ImageSY($gdimg) - $i, $color2[$i]); // bottom
  83          }
  84          return true;
  85      }
  86  
  87  
  88  	function Blur(&$gdimg, $radius=0.5) {
  89          // Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php)
  90  
  91          $radius = round(max(0, min($radius, 50)) * 2);
  92          if (!$radius) {
  93              return false;
  94          }
  95  
  96          $w = ImageSX($gdimg);
  97          $h = ImageSY($gdimg);
  98          if ($imgBlur = ImageCreateTrueColor($w, $h)) {
  99              // Gaussian blur matrix:
 100              //    1    2    1
 101              //    2    4    2
 102              //    1    2    1
 103  
 104              // Move copies of the image around one pixel at the time and merge them with weight
 105              // according to the matrix. The same matrix is simply repeated for higher radii.
 106              for ($i = 0; $i < $radius; $i++)    {
 107                  ImageCopy     ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1);            // up left
 108                  ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w,     $h,     50.00000);  // down right
 109                  ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h,     33.33333);  // down left
 110                  ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w,     $h - 1, 25.00000);  // up right
 111                  ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h,     33.33333);  // left
 112                  ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w,     $h,     25.00000);  // right
 113                  ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w,     $h - 1, 20.00000);  // up
 114                  ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w,     $h,     16.666667); // down
 115                  ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w,     $h,     50.000000); // center
 116                  ImageCopy     ($gdimg, $imgBlur, 0, 0, 0, 0, $w,     $h);
 117              }
 118              return true;
 119          }
 120          return false;
 121      }
 122  
 123  
 124  	function BlurGaussian(&$gdimg) {
 125          if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
 126              if (ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) {
 127                  return true;
 128              }
 129              $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__);
 130              // fall through and try it the hard way
 131          }
 132          $this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [using phpthumb_filters::Blur() instead]', __FILE__, __LINE__);
 133          return phpthumb_filters::Blur($gdimg, 0.5);
 134      }
 135  
 136  
 137  	function BlurSelective(&$gdimg) {
 138          if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
 139              if (ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) {
 140                  return true;
 141              }
 142              $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__);
 143              // fall through and try it the hard way
 144          }
 145          // currently not implemented "the hard way"
 146          $this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__);
 147          return false;
 148      }
 149  
 150  
 151  	function Brightness(&$gdimg, $amount=0) {
 152          if ($amount == 0) {
 153              return true;
 154          }
 155          $amount = max(-255, min(255, $amount));
 156  
 157          if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
 158              if (ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) {
 159                  return true;
 160              }
 161              $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, '.$amount.')', __FILE__, __LINE__);
 162              // fall through and try it the hard way
 163          }
 164  
 165          $scaling = (255 - abs($amount)) / 255;
 166          $baseamount = (($amount > 0) ? $amount : 0);
 167          for ($x = 0; $x < ImageSX($gdimg); $x++) {
 168              for ($y = 0; $y < ImageSY($gdimg); $y++) {
 169                  $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
 170                  foreach ($OriginalPixel as $key => $value) {
 171                      $NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling));
 172                  }
 173                  $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
 174                  ImageSetPixel($gdimg, $x, $y, $newColor);
 175              }
 176          }
 177          return true;
 178      }
 179  
 180  
 181  	function Contrast(&$gdimg, $amount=0) {
 182          if ($amount == 0) {
 183              return true;
 184          }
 185          $amount = max(-255, min(255, $amount));
 186  
 187          if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
 188              if (ImageFilter($gdimg, IMG_FILTER_CONTRAST, $amount)) {
 189                  return true;
 190              }
 191              $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_CONTRAST, '.$amount.')', __FILE__, __LINE__);
 192              // fall through and try it the hard way
 193          }
 194  
 195          if ($amount > 0) {
 196              $scaling = 1 + ($amount / 255);
 197          } else {
 198              $scaling = (255 - abs($amount)) / 255;
 199          }
 200          for ($x = 0; $x < ImageSX($gdimg); $x++) {
 201              for ($y = 0; $y < ImageSY($gdimg); $y++) {
 202                  $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
 203                  foreach ($OriginalPixel as $key => $value) {
 204                      $NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling)));
 205                  }
 206                  $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
 207                  ImageSetPixel($gdimg, $x, $y, $newColor);
 208              }
 209          }
 210      }
 211  
 212  
 213  	function Colorize(&$gdimg, $amount, $targetColor) {
 214          $amount      = (is_numeric($amount)                          ? $amount      : 25);
 215          $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
 216  
 217          if ($amount == 0) {
 218              return true;
 219          }
 220  
 221          if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
 222              if ($targetColor == 'gray') {
 223                  $targetColor = '808080';
 224              }
 225              $r = substr($targetColor, 0, 2);
 226              $g = substr($targetColor, 2, 2);
 227              $b = substr($targetColor, 4, 2);
 228              if (ImageFilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
 229                  return true;
 230              }
 231              $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
 232              // fall through and try it the hard way
 233          }
 234  
 235          // overridden below for grayscale
 236          if ($targetColor != 'gray') {
 237              $TargetPixel['red']   = hexdec(substr($targetColor, 0, 2));
 238              $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
 239              $TargetPixel['blue']  = hexdec(substr($targetColor, 4, 2));
 240          }
 241  
 242          for ($x = 0; $x < ImageSX($gdimg); $x++) {
 243              for ($y = 0; $y < ImageSY($gdimg); $y++) {
 244                  $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
 245                  if ($targetColor == 'gray') {
 246                      $TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
 247                  }
 248                  foreach ($TargetPixel as $key => $value) {
 249                      $NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * ($amount / 100)))));
 250                  }
 251                  //$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
 252                  $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
 253                  ImageSetPixel($gdimg, $x, $y, $newColor);
 254              }
 255          }
 256          return true;
 257      }
 258  
 259  
 260  	function Crop(&$gdimg, $left=0, $right=0, $top=0, $bottom=0) {
 261          if (!$left && !$right && !$top && !$bottom) {
 262              return true;
 263          }
 264          $oldW = ImageSX($gdimg);
 265          $oldH = ImageSY($gdimg);
 266          if (($left   > 0) && ($left   < 1)) { $left   = round($left   * $oldW); }
 267          if (($right  > 0) && ($right  < 1)) { $right  = round($right  * $oldW); }
 268          if (($top    > 0) && ($top    < 1)) { $top    = round($top    * $oldH); }
 269          if (($bottom > 0) && ($bottom < 1)) {