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:  * This class provides some utility functions, such as generating highlights
  4:  * of a color as well as a factory method responsible for creating a concrete
  5:  * Horde_Image driver.
  6:  *
  7:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  8:  *
  9:  * See the enclosed file COPYING for license information (LGPL). If you
 10:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 11:  *
 12:  * @author  Chuck Hagenbuch <chuck@horde.org>
 13:  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
 14:  * @package Image
 15:  */
 16: class Horde_Image
 17: {
 18:     /**
 19:      * Calculate a lighter (or darker) version of a color.
 20:      *
 21:      * @param string $color   An HTML color, e.g.: #ffffcc.
 22:      * @param string $factor  TODO
 23:      *
 24:      * @return string  A modified HTML color.
 25:      */
 26:     static public function modifyColor($color, $factor = 0x11)
 27:     {
 28:         list($r, $g, $b) = self::getColor($color);
 29: 
 30:         $r = min(max($r + $factor, 0), 255);
 31:         $g = min(max($g + $factor, 0), 255);
 32:         $b = min(max($b + $factor, 0), 255);
 33: 
 34:         return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
 35:     }
 36: 
 37:     /**
 38:      * Calculate a more intense version of a color.
 39:      *
 40:      * @param string $color   An HTML color, e.g.: #ffffcc.
 41:      * @param string $factor  TODO
 42:      *
 43:      * @return string  A more intense HTML color.
 44:      */
 45:     static public function moreIntenseColor($color, $factor = 0x11)
 46:     {
 47:         list($r, $g, $b) = self::getColor($color);
 48: 
 49:         if ($r >= $g && $r >= $b) {
 50:             $g = $g / $r;
 51:             $b = $b / $r;
 52: 
 53:             $r += $factor;
 54:             $g = floor($g * $r);
 55:             $b = floor($b * $r);
 56:         } elseif ($g >= $r && $g >= $b) {
 57:             $r = $r / $g;
 58:             $b = $b / $g;
 59: 
 60:             $g += $factor;
 61:             $r = floor($r * $g);
 62:             $b = floor($b * $g);
 63:         } else {
 64:             $r = $r / $b;
 65:             $g = $g / $b;
 66: 
 67:             $b += $factor;
 68:             $r = floor($r * $b);
 69:             $g = floor($g * $b);
 70:         }
 71: 
 72:         $r = min(max($r, 0), 255);
 73:         $g = min(max($g, 0), 255);
 74:         $b = min(max($b, 0), 255);
 75: 
 76:         return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
 77:     }
 78: 
 79:     /**
 80:      * Returns the brightness of a color.
 81:      *
 82:      * @param string $color  An HTML color, e.g.: #ffffcc.
 83:      *
 84:      * @return integer  The brightness on a scale of 0 to 255.
 85:      */
 86:     static public function brightness($color)
 87:     {
 88:         list($r, $g, $b) = self::getColor($color);
 89: 
 90:         return round((($r * 299) + ($g * 587) + ($b * 114)) / 1000);
 91:     }
 92: 
 93:     /**
 94:      * @TODO
 95:      */
 96:     static public function grayscaleValue($r, $g, $b)
 97:     {
 98:         return round(($r * 0.30) + ($g * 0.59) + ($b * 0.11));
 99:     }
100: 
101:     /**
102:      * @TODO
103:      */
104:     static public function grayscalePixel($originalPixel)
105:     {
106:         $gray = Horde_Image::grayscaleValue($originalPixel['red'], $originalPixel['green'], $originalPixel['blue']);
107:         return array('red'=>$gray, 'green'=>$gray, 'blue'=>$gray);
108:     }
109: 
110:     /**
111:      * Normalizes an HTML color.
112:      *
113:      * @param string $color  An HTML color, e.g.: #ffffcc or #ffc.
114:      *
115:      * @return array  Array with three elements: red, green, and blue.
116:      */
117:     static public function getColor($color)
118:     {
119:         if ($color[0] == '#') {
120:             $color = substr($color, 1);
121:         }
122: 
123:         if (strlen($color) == 3) {
124:             $color = str_repeat($color[0], 2) .
125:                 str_repeat($color[1], 2) .
126:                 str_repeat($color[2], 2);
127:         }
128: 
129:         return array(
130:             hexdec(substr($color, 0, 2)),
131:             hexdec(substr($color, 2, 2)),
132:             hexdec(substr($color, 4, 2))
133:         );
134:     }
135: 
136:     /**
137:      * Get the RGB value for a given colorname.
138:      *
139:      * @param string $colorname  The colorname
140:      *
141:      * @return array  An array of RGB values.
142:      */
143:     static public function getRGB($colorname)
144:     {
145:         require_once dirname(__FILE__) . '/Image/rgb.php';
146:         return isset($GLOBALS['horde_image_rgb_colors'][$colorname]) ?
147:             $GLOBALS['horde_image_rgb_colors'][$colorname] :
148:             array(0, 0, 0);
149:     }
150: 
151:     /**
152:      * Get the hex representation of the given colorname.
153:      *
154:      * @param string $colorname  The colorname
155:      *
156:      * @return string  The hex representation of the color.
157:      */
158:     static public function getHexColor($colorname)
159:     {
160:         require_once dirname(__FILE__) . '/Image/rgb.php';
161:         if (isset($GLOBALS['horde_image_rgb_colors'][$colorname])) {
162:             list($r, $g, $b) = $GLOBALS['horde_image_rgb_colors'][$colorname];
163:             return '#' . str_pad(dechex(min($r, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(min($g, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(min($b, 255)), 2, '0', STR_PAD_LEFT);
164:         } else {
165:             return 'black';
166:         }
167:     }
168: 
169:     /**
170:      * Get an x,y pair on circle, assuming center is 0,0.
171:      *
172:      * @access private
173:      *
174:      * @param double $degrees    The degrees of arc to get the point for.
175:      * @param integer $diameter  The diameter of the circle.
176:      *
177:      * @return array  (x coordinate, y coordinate) of the point.
178:      */
179:     static public function circlePoint($degrees, $diameter)
180:     {
181:         // Avoid problems with doubles.
182:         $degrees += 0.0001;
183: 
184:         return array(cos(deg2rad($degrees)) * ($diameter / 2),
185:                      sin(deg2rad($degrees)) * ($diameter / 2));
186:     }
187: 
188:     /**
189:      * Get point coordinates at the limits of an arc. Only valid for
190:      * angles ($end - $start) <= 45 degrees.
191:      *
192:      * @access private
193:      *
194:      * @param integer $r      The radius of the arc.
195:      * @param integer $start  The starting angle.
196:      * @param integer $end    The ending angle.
197:      *
198:      * @return array  The start point, end point, and anchor point.
199:      */
200:     static public function arcPoints($r, $start, $end)
201:     {
202:         // Start point.
203:         $pts['x1'] = $r * cos(deg2rad($start));
204:         $pts['y1'] = $r * sin(deg2rad($start));
205: 
206:         // End point.
207:         $pts['x2'] = $r * cos(deg2rad($end));
208:         $pts['y2'] = $r * sin(deg2rad($end));
209: 
210:         // Anchor point.
211:         $a3 = ($start + $end) / 2;
212:         $r3 = $r / cos(deg2rad(($end - $start) / 2));
213:         $pts['x3'] = $r3 * cos(deg2rad($a3));
214:         $pts['y3'] = $r3 * sin(deg2rad($a3));
215: 
216:         return $pts;
217:     }
218: 
219:     /**
220:      * Return point size for font
221:      */
222:     static public function getFontSize($fontsize)
223:     {
224:         switch ($fontsize) {
225:         case 'medium':
226:             $point = 18;
227:             break;
228:         case 'large':
229:             $point = 24;
230:             break;
231:         case 'giant':
232:             $point = 30;
233:             break;
234:         default:
235:             $point = 12;
236:         }
237: 
238:         return $point;
239:     }
240: 
241: }
242: 
API documentation generated by ApiGen