Overview

Packages

  • Image
  • None

Classes

  • Horde_Image
  • Horde_Image_Base
  • Horde_Image_Effect
  • Horde_Image_Effect_Border
  • Horde_Image_Effect_Gd_DropShadow
  • Horde_Image_Effect_Gd_RoundCorners
  • Horde_Image_Effect_Gd_TextWatermark
  • Horde_Image_Effect_Gd_Unsharpmask
  • Horde_Image_Effect_Im_Border
  • Horde_Image_Effect_Im_CenterCrop
  • Horde_Image_Effect_Im_Composite
  • Horde_Image_Effect_Im_DropShadow
  • Horde_Image_Effect_Im_LiquidResize
  • Horde_Image_Effect_Im_PhotoStack
  • Horde_Image_Effect_Im_PolaroidImage
  • Horde_Image_Effect_Im_RoundCorners
  • Horde_Image_Effect_Im_TextWatermark
  • Horde_Image_Effect_Im_Unsharpmask
  • Horde_Image_Effect_Imagick_Border
  • Horde_Image_Effect_Imagick_CenterCrop
  • Horde_Image_Effect_Imagick_Composite
  • Horde_Image_Effect_Imagick_DropShadow
  • Horde_Image_Effect_Imagick_LiquidResize
  • Horde_Image_Effect_Imagick_PhotoStack
  • Horde_Image_Effect_Imagick_PolaroidImage
  • Horde_Image_Effect_Imagick_RoundCorners
  • Horde_Image_Effect_Imagick_SmartCrop
  • Horde_Image_Effect_Imagick_TextWatermark
  • Horde_Image_Effect_Imagick_Unsharpmask
  • Horde_Image_Exception
  • Horde_Image_Exif
  • Horde_Image_Exif_Base
  • Horde_Image_Exif_Bundled
  • Horde_Image_Exif_Exiftool
  • Horde_Image_Exif_Parser_Base
  • Horde_Image_Exif_Php
  • Horde_Image_Gd
  • Horde_Image_Im
  • Horde_Image_Imagick
  • Horde_Image_Png
  • Horde_Image_Svg
  • Horde_Image_Swf
  • Horde_Image_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Unsharp mask Image effect.
  4:  *
  5: 
  6:  * From: http://www.vikjavev.com/hovudsida/umtestside.php
  7:  *
  8:  * @package Image
  9:  */
 10: class Horde_Image_Effect_Gd_Unsharpmask extends Horde_Image_Effect
 11: {
 12:     /**
 13:      * Valid parameters:
 14:      *
 15:      * @TODO
 16:      *
 17:      * @var array
 18:      */
 19:     protected $_params = array('amount' => 0,
 20:                                'radius' => 0,
 21:                                'threshold' => 0);
 22: 
 23:     /**
 24:      * Apply the unsharp_mask effect.
 25:      *
 26:      * @return mixed true
 27:      */
 28:     public function apply()
 29:     {
 30:         $amount = $this->_params['amount'];
 31:         $radius = $this->_params['radius'];
 32:         $threshold = $this->_params['threshold'];
 33: 
 34:         // Attempt to calibrate the parameters to Photoshop:
 35:         $amount = min($amount, 500);
 36:         $amount = $amount * 0.016;
 37:         if ($amount == 0) {
 38:             return true;
 39:         }
 40: 
 41:         $radius = min($radius, 50);
 42:         $radius = $radius * 2;
 43: 
 44:         $threshold = min($threshold, 255);
 45: 
 46:         $radius = abs(round($radius));  // Only integers make sense.
 47:         if ($radius == 0) {
 48:             return true;
 49:         }
 50: 
 51:         $img = $this->_image->_im;
 52:         $w = ImageSX($img);
 53:         $h = ImageSY($img);
 54:         $imgCanvas  = ImageCreateTrueColor($w, $h);
 55:         $imgCanvas2 = ImageCreateTrueColor($w, $h);
 56:         $imgBlur    = ImageCreateTrueColor($w, $h);
 57:         $imgBlur2   = ImageCreateTrueColor($w, $h);
 58:         ImageCopy($imgCanvas,  $img, 0, 0, 0, 0, $w, $h);
 59:         ImageCopy($imgCanvas2, $img, 0, 0, 0, 0, $w, $h);
 60: 
 61:         // Gaussian blur matrix:
 62:         //
 63:         //  1   2   1
 64:         //  2   4   2
 65:         //  1   2   1
 66:         //
 67:         //////////////////////////////////////////////////
 68: 
 69:         // Move copies of the image around one pixel at the time and merge them with weight
 70:         // according to the matrix. The same matrix is simply repeated for higher radii.
 71:         for ($i = 0; $i < $radius; $i++)    {
 72:             ImageCopy     ($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1);            // up left
 73:             ImageCopyMerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w,     $h,     50);        // down right
 74:             ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h,     33.33333);  // down left
 75:             ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w,     $h - 1, 25);        // up right
 76:             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h,     33.33333);  // left
 77:             ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w,     $h,     25);        // right
 78:             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w,     $h - 1, 20 );       // up
 79:             ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w,     $h,     16.666667); // down
 80:             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w,     $h,     50);        // center
 81:             ImageCopy     ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w,     $h);
 82: 
 83:             // During the loop above the blurred copy darkens, possibly due to a roundoff
 84:             // error. Therefore the sharp picture has to go through the same loop to
 85:             // produce a similar image for comparison. This is not a good thing, as processing
 86:             // time increases heavily.
 87:             ImageCopy     ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h);
 88:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
 89:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
 90:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
 91:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
 92:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
 93:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20 );
 94:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667);
 95:             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
 96:             ImageCopy     ($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h);
 97:         }
 98: 
 99:         // Calculate the difference between the blurred pixels and the original
100:         // and set the pixels
101:         for ($x = 0; $x < $w; $x++) { // each row
102:             for ($y = 0; $y < $h; $y++) { // each pixel
103: 
104:                 $rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
105:                 $rOrig = (($rgbOrig >> 16) & 0xFF);
106:                 $gOrig = (($rgbOrig >>  8) & 0xFF);
107:                 $bOrig =  ($rgbOrig        & 0xFF);
108: 
109:                 $rgbBlur = ImageColorAt($imgCanvas, $x, $y);
110:                 $rBlur = (($rgbBlur >> 16) & 0xFF);
111:                 $gBlur = (($rgbBlur >>  8) & 0xFF);
112:                 $bBlur =  ($rgbBlur        & 0xFF);
113: 
114:                 // When the masked pixels differ less from the original
115:                 // than the threshold specifies, they are set to their original value.
116:                 $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
117:                 $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
118:                 $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
119: 
120:                 if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
121:                     $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
122:                     ImageSetPixel($img, $x, $y, $pixCol);
123:                 }
124:             }
125:         }
126:         ImageDestroy($imgCanvas);
127:         ImageDestroy($imgCanvas2);
128:         ImageDestroy($imgBlur);
129:         ImageDestroy($imgBlur2);
130: 
131:         return true;
132:     }
133: 
134: }
135: 
API documentation generated by ApiGen