Changeset 13174
- Timestamp:
- 11/20/08 06:02:45 (4 years ago)
- Files:
-
- plugins/gsImageProcessorPlugin/trunk/lib/ImageProcessorHelper.class.php (modified) (7 diffs)
- plugins/gsImageProcessorPlugin/trunk/lib/gdImage.class.php (modified) (1 diff)
- plugins/gsImageProcessorPlugin/trunk/lib/gsBorderProcessor.class.php (modified) (2 diffs)
- plugins/gsImageProcessorPlugin/trunk/lib/gsColorizeProcessor.class.php (modified) (2 diffs)
- plugins/gsImageProcessorPlugin/trunk/lib/gsRotationProcessor.class.php (modified) (1 diff)
- plugins/gsImageProcessorPlugin/trunk/lib/gsShadowProcessor.class.php (modified) (5 diffs)
- plugins/gsImageProcessorPlugin/trunk/lib/gsTextOverlayProcessor.class.php (modified) (2 diffs)
- plugins/gsImageProcessorPlugin/trunk/lib/gsWatermarkProcessor.class.php (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/gsImageProcessorPlugin/trunk/lib/ImageProcessorHelper.class.php
r13172 r13174 32 32 throw new sfException('Missing required params dimensions array!'); 33 33 34 // determines if we are to save the processed image before returning 34 // determines if we are to save the processed image before returning and if so, where 35 35 if(isset($params['save']) && !empty($output_file)) 36 36 { … … 39 39 40 40 // determines the output file type 41 if(isset($params[' output_type']))42 $output_type = $params[' output_type'];41 if(isset($params['save']['type'])) 42 $output_type = $params['save']['type']; 43 43 else 44 44 { … … 52 52 $output_type = 'png'; 53 53 54 $quality = (isset($params[' quality']))?$params['quality']:90;55 } 56 else if(isset($params['save']) && isset($params[' output_type']))57 { 58 if(in_array($params[' output_type'],array('png','jpg','gif')))59 $output_type = $params[' output_type'];54 $quality = (isset($params['save']['quality']))?$params['save']['quality']:90; 55 } 56 else if(isset($params['save']) && isset($params['save']['type'])) 57 { 58 if(in_array($params['save']['type'],array('png','jpg','gif'))) 59 $output_type = $params['save']['type']; 60 60 else 61 61 $save = false; … … 69 69 $cparams = &$params['create']; 70 70 71 $imageWidth = null; 72 $imageHeight = null; 73 74 if(isset($cparams['auto-size'])) 75 { 76 $dims = self::autoSize($params[$cparams['auto-size']['field']]); 77 $imageWidth = $dims['width']; 78 $imageHeight = $dims['height']; 79 } 80 else if(isset($cparams['width']) && isset($cparams['height'])) 81 { 82 $imageWidth = $cparams['width']; 83 $imageHeight = $cparams['height']; 84 } 85 86 if($imageWidth == null) 87 throw new sfException('Image auto-create requested but no params provided'); 88 89 $depth = isset($cparams['depth']) ? $cparams['depth']:32; 71 /*START HERE*/ 72 /* First, we find the bounding box of the text: */ 73 $dims = imagettfbbox($params['font-size'], 0, $params['font'], $params['text']); 74 75 $imageWidth = $cparams['width']; 76 $imageHeight = $cparams['height']; 77 78 $depth = isset($cparams['depth']) ? $cparams['depth']:null; 90 79 switch($depth) 91 80 { 92 81 case '8': 93 82 $im = imagecreate($imageWidth, $imageHeight); 83 break; 94 84 case '32': 95 85 default: … … 98 88 } 99 89 90 if(!$im) 91 throw new sfException('Unable to create image from scratch'); 92 100 93 // set values for transparency and opacity 101 $transparency = isset($ params['transparency']);102 $opacity = ($transparency) ? 127: 0;94 $transparency = isset($cparams['transparent']); 95 $opacity = ($transparency) ? (isset($cparams['opacity']) ? $cparams['opacity']: 127): 0; 103 96 104 97 if(isset($cparams['bgcolor']) || isset($cparams['bgcolour'])) … … 109 102 @imagefill($im, 0, 0, $bgcolor); 110 103 } 111 112 if(!$im)113 throw new sfException('Unable to create image from scratch');114 104 115 105 $img = new gdImage($im); … … 123 113 } 124 114 125 if(isset($params['resize']) && !isset($params['max_width'])) 126 throw new sfException('Resize specified but no max_width provided!'); 127 128 if(isset($params['resize']) && $params['resize'] && (!isset($params['strict']) || $params['strict'] == false)) 129 { 130 $img = gsResizeProcessor::resizeToSize($img,$params['max_width']); 131 132 if(isset($params['max_height']) && $img->getHeight() > $params['max_height']) // still too long 133 { 134 if( isset($params['crop']) && $params['crop'] ) // crop to size 135 $img = gsCroppingProcessor::cropToSize($img,$params['max_width'],$params['max_height'], gsCroppingProcessor::ccCENTER); 136 else // resize height to max 137 $img = gsResizeProcessor::resizeToSize($img,0,$params['max_height']); 138 } 139 } 140 else if(isset($params['resize']) && $params['resize'] && isset($params['strict']) && $params['strict'] == true) 141 { 142 $dims1 = gsResizeProcessor::getProportionalSize($img,$params['max_width'],0); 143 144 if(isset($params['max_height']) && $dims1[1] < $params['max_height']) // resizing would make the height smaller than the max. 145 { 146 $dims2 = gsResizeProcessor::getProportionalSize($img,0,$params['max_height']); 147 if($dims2[0] <$params['max_width']) // resizing would make the width smaller than the max 148 throw new sfException('Unable to resize image strictly'); 115 116 if(isset($params['resize'])) 117 { 118 $rparams = &$params['resize']; 119 120 if(!isset($rparams['width'])) 121 throw new sfException('Resize specified but no width provided!'); 122 123 if(!isset($rparams['strict']) || $rparams['strict'] == false) 124 { 125 $img = gsResizeProcessor::resizeToSize($img,$rparams['width']); 126 127 if(isset($rparams['height']) && $img->getHeight() > $rparams['height']) // still too long 128 { 129 if( isset($rparams['crop']) && $rparams['crop'] ) // crop to size 130 $img = gsCroppingProcessor::cropToSize($img,$rparams['width'],$rparams['height'], gsCroppingProcessor::ccCENTER); 131 else // resize height to max 132 $img = gsResizeProcessor::resizeToSize($img,0,$rparams['height']); 133 } 134 } 135 else if(isset($rparams['strict']) && $rparams['strict'] == true) 136 { 137 $dims1 = gsResizeProcessor::getProportionalSize($img,$rparams['width'],0); 138 if(isset($rparams['height']) && $dims1[1] < $rparams['height']) // resizing would make the height smaller than the max. 139 { 140 $dims2 = gsResizeProcessor::getProportionalSize($img,0,$rparams['height']); 141 if($dims2[0] < $rparams['width']) // resizing would make the width smaller than the max 142 throw new sfException('Unable to resize image strictly'); 143 else 144 { 145 $img = gsResizeProcessor::resizeToSize($img,0,$rparams['height']); 146 147 if($img->getWidth() > $rparams['width']) // still too wide 148 $img = gsCroppingProcessor::cropToSize($img,$rparams['width'],$rparams['height'], gsCroppingProcessor::ccCENTER); 149 } 150 } 149 151 else 150 152 { 151 $img = gsResizeProcessor::resizeToSize($img, 0,$params['max_height']);152 153 if($img->get Width() > $params['max_width']) // still too wide154 $img = gsCroppingProcessor::cropToSize($img,$ params['max_width'],$params['max_height'], gsCroppingProcessor::ccCENTER);153 $img = gsResizeProcessor::resizeToSize($img,$rparams['width']); 154 155 if($img->getHeight() > $rparams['height']) // still too long 156 $img = gsCroppingProcessor::cropToSize($img,$rparams['width'],$rparams['height'], gsCroppingProcessor::ccCENTER); 155 157 } 156 158 } 157 else 158 { 159 $img = gsResizeProcessor::resizeToSize($img,$params['max_width']); 160 161 if($img->getHeight() > $params['max_height']) // still too long 162 $img = gsCroppingProcessor::cropToSize($img,$params['max_width'],$params['max_height'], gsCroppingProcessor::ccCENTER); 163 } 164 } 165 else if( isset($params['crop']) && $params['crop'] && isset($params['max_height'])) //just crop 166 $img = gsCroppingProcessor::cropToSize($img,$params['max_width'],$params['max_height'], gsCroppingProcessor::ccCENTER); 167 159 } 160 161 if( isset($params['crop']) ) //just crop 162 { 163 $cparams = &$params['crop']; 164 165 if(isset($cparams['height']) && $cparams['width']) 166 $img = gsCroppingProcessor::cropToSize($img,$cparams['width'],$cparams['height'], gsCroppingProcessor::ccCENTER); 167 } 168 169 /* 170 * colorize: 171 * color: [ R,G,B ] or FFAAFF 172 * clone: false 173 */ 168 174 if( isset($params['colorize']) && is_array($params['colorize']) ) 169 175 $img = gsColorizeProcessor::colorize($img,$params['colorize']); 170 176 177 /* 178 * border: 179 * width: 10 (default if not specified) 180 * color: [ R,G,B ] or FFAAFF 181 * clone: false (apply to passed in 182 */ 171 183 if( isset($params['border']) && $params['border'] ) 172 $img = gsBorderProcessor::addBorder($img, $params['border_width'], ((isset($params['border_colour']))?$params['border_colour']:$params['border_color']) ); 173 184 $img = gsBorderProcessor::addBorder($img, $params['border']); 185 186 /* 187 * shadow: 188 * color: [ R,G,B ] or 'FFAAFF' (default 000000) 189 * path: /full/path/to/dropshadow/images.png 190 * 191 */ 174 192 if( isset($params['shadow']) && $params['shadow'] ) 175 $img = gsShadowProcessor::applyShadow($img, (isset($params['shadow_color'])?$params['shadow_color']:$params['shadow_colour'])); 176 177 if( isset($params['rotate']) && $params['rotate'] && isset($params['degrees']) ) 178 gsRotationProcessor::rotate($img, $params['degrees'],$params['background']); 179 193 $img = gsShadowProcessor::applyShadow($img, $params['shadow']); 194 195 /* 196 * rotate: 197 * degrees: int 198 * background: [R,G,B] or FFAAFF 199 * clone: false 200 */ 201 if( isset($params['rotate']) ) 202 gsRotationProcessor::rotate($img, $params); 203 204 /* 205 * watermark: 206 */ 180 207 if( isset($params['watermark']) && is_array($params['watermark'])) 181 208 $img = gsWatermarkProcessor::createWatermark($img, $params['watermark']); 182 209 210 /* 211 * textoverlay: 212 * 213 */ 183 214 if( isset($params['textoverlay']) && is_array($params['textoverlay'])) 184 215 $img = gsTextOverlayProcessor::overlayText($img, $params['textoverlay']); 185 216 186 217 if($save) 187 $img->save($full_path,$output_type,$quality,(isset($params[' transparent']))?$params['transparent']:null);218 $img->save($full_path,$output_type,$quality,(isset($params['save']['transparent'])) ? $params['save']['transparent']:null); 188 219 189 220 return $img; 190 221 } 191 192 static public function autoSize($params = array())193 {194 /*195 * First, we find the bounding box of the text:196 */197 198 $dims = imagettfbbox($params['font']['size'], 0, $params['font']['file'], $params['text']['content']);199 200 /* Then we calculate the total width of the image (the x-coord of201 * the left corners of the image may be negative, due to anti-aliasing202 * and various other font metrics issues, so we need to use absolute203 * values)204 */205 206 $width = abs($dims[2] - $dims[0]);207 208 /*209 * Playing around with dimensions depending on just HOW far the text210 * moves off the left side211 */212 213 if($dims[0] < -1)214 $boxWidth = abs($dims[2]) + abs($dims[0]) - 1;215 216 /*217 * Then we calculate the total height of the image using the same218 * logic219 */220 221 $height = abs($dims[7]) - abs($dims[1]);222 223 /* Account for AA again */224 if($dims[3] > 0)225 $boxHeight = abs($dims[7] - $dims[1]) - 1;226 227 /*228 * Now, we find where we need to place the x-coord of the font's baseline.229 * This is usually the same as the x-coord of the bottom-left corner,230 * give or take a pixel or two231 */232 233 $x = ($dims[0] >= -1) ? abs($dims[0] + 1) * -1: abs($dims[0] + 2);234 235 /* This is the important one. For reasons I admittedly don't fully understand,236 * the y-coord of the top right (or left) corner of the bounding box is237 * a negative value equal to the inverse of the baseline height of the238 * font: I.E. if the b-line height = 24px, then the y-coord of the top-239 * right = -24. So we set the baseline-y position of the text to the240 * absolute value of the y-coord of the top-right corner, plus one pixel241 * to account for anti-aliasing.242 */243 244 $y = abs($dims[5] + 1);245 246 return array('width'=>$boxWidth,'height'=>$boxHeight);247 }248 222 } plugins/gsImageProcessorPlugin/trunk/lib/gdImage.class.php
r13172 r13174 226 226 } 227 227 228 if ($ext == 'png') 228 if ($ext == 'jpeg') 229 $this->_is_saved= $func($this->_img, $filename, $quality); 230 else 229 231 $this->_is_saved = $func($this->_img, $filename); 230 if ($ext == 'jpeg') 231 $this->_is_saved= @$func($this->_img, $filename, $quality); 232 233 if (!$this->_is_saved) { 232 233 if (!$this->_is_saved) 234 { 234 235 $err = error_get_last(); 235 236 throw new sfException('Unable to save image to filesystem. '.$err['message']); plugins/gsImageProcessorPlugin/trunk/lib/gsBorderProcessor.class.php
r13172 r13174 3 3 /** 4 4 * gsBorderProcessor 5 * 5 * 6 6 * @package gsImageProcessorPlugin 7 7 * @version $id$ 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 10 10 * @license GPL Version 2 11 11 */ … … 13 13 class gsBorderProcessor 14 14 { 15 static public function addBorder(&$i mg, $size = 10, $colour= null)15 static public function addBorder(&$input_img, $params = array()) //$width = 10, $color= null) 16 16 { 17 if($colour == null) 18 $colour = array(0,0,0); 17 $img = (isset($params['clone'])) ? new gdImage($input_img->getData()): $input_img; 19 18 20 if(!is_array($colour)) 21 $colour =gsImageHelper::HTMLHexToBinArray($colour); 19 // defaults 20 $width = (isset($params['width']) ? $params['width']:10); 21 $color = (isset($params['color']) ? $params['color']:array(0,0,0)); 22 22 23 $mask = @imagecolorallocate($img->getData(),$colour[0],$colour[1],$colour[2]); 23 if(!is_array($color)) 24 $color = gsImageHelper::HTMLHexToBinArray($color); 24 25 25 @imagefilledrectangle($img->getData(),0,0,$size,$img->getHeight(),$mask);26 $mask = @imagecolorallocate($img->getData(),$color[0],$color[1],$color[2]); 26 27 27 @imagefilledrectangle($img->getData(),0,0,$ img->getWidth(),$size,$mask);28 @imagefilledrectangle($img->getData(),0,0,$width,$img->getHeight(),$mask); 28 29 29 @imagefilledrectangle($img->getData(), ($img->getWidth()-$size),0,$img->getWidth(),$img->getHeight(),$mask);30 @imagefilledrectangle($img->getData(),0,0,$img->getWidth(),$width,$mask); 30 31 31 @imagefilledrectangle($img->getData(),0,($img->getHeight()-$size),$img->getWidth(),$img->getHeight(),$mask); 32 @imagefilledrectangle($img->getData(),($img->getWidth()-$width),0,$img->getWidth(),$img->getHeight(),$mask); 33 34 @imagefilledrectangle($img->getData(),0,($img->getHeight()-$width),$img->getWidth(),$img->getHeight(),$mask); 32 35 33 36 return $img; plugins/gsImageProcessorPlugin/trunk/lib/gsColorizeProcessor.class.php
r13172 r13174 10 10 * @license GPL Version 2 11 11 * @description : colourizes photos. 12 * @param $colo ur - array of RBG values, from -255 to +255 where 0 == no change12 * @param $color - array of RBG values, from -255 to +255 where 0 == no change 13 13 to get these numbers, use photoshop, grayscale your image 14 14 duplicate onto a new layer & colourize as you wish. Use … … 17 17 */ 18 18 19 class gsColorizeProcessor 19 class gsColorizeProcessor 20 20 { 21 static public function colourize(&$i mg,$colour = null,$ret_new=false)21 static public function colourize(&$input_img,$params = array()) //$color = null,$ret_new=false) 22 22 { 23 if($colour == null) 23 $img = (isset($params['clone'])) ? new gdImage($input_img->getData()): $input_img; 24 25 if (!isset($params['color'])) 24 26 throw new sfException('No Color provided to colourize with!'); 25 27 26 if(!is_array($colour)) 27 $colour = gsImageHelper::HTMLHexToBinArray($colour); 28 $color = (!is_array($params['color'])) ? gsImageHelper::HTMLHexToBinArray($color): $params['color']; 28 29 29 if(count($colo ur) != 3)30 if(count($color) != 3) 30 31 throw new sfException('Wrong parameter count for colourization!'); 31 32 32 if($ret_new) 33 { 34 $tmp = new gdImage(); 35 $tmp->setData($img->getData()); 36 imagefilter($tmp->getData(), IMG_FILTER_GRAYSCALE); 37 imagefilter($tmp->getData(), IMG_FILTER_COLORIZE, $colour[0], $colour[1], $colour[2]); 38 return $tmp; 39 } 40 else 41 { 42 imagefilter($img->getData(), IMG_FILTER_GRAYSCALE); 43 imagefilter($img->getData(), IMG_FILTER_COLORIZE, $colour[0], $colour[1], $colour[2]); 33 imagefilter($tmp->getData(), IMG_FILTER_GRAYSCALE); 34 imagefilter($tmp->getData(), IMG_FILTER_COLORIZE, $color[0], $color[1], $color[2]); 44 35 45 return $img; 46 } 36 return $img; 47 37 } 48 38 } plugins/gsImageProcessorPlugin/trunk/lib/gsRotationProcessor.class.php
r13172 r13174 13 13 */ 14 14 15 class gsRotationProcessor 15 class gsRotationProcessor 16 16 { 17 static public function rotate(&$i mg,$degree ,$background = '000000',$ret_new=false)17 static public function rotate(&$input_img, $params = array()) 18 18 { 19 if($degree == 0) 19 $img = isset($params['clone']) ? new gdImage($input_img->getData()): $input_img; 20 21 if(!isset($params['degree'])) 22 throw new sfException('gsRotationProcessor missing required \'degree\' param.'); 23 24 $background = isset($params['background']) ? $params['background']:null; 25 26 if($background) 20 27 { 21 throw new sfException('DEGREE == 0!!!'); 22 if($ret_new) 23 return clone $img; 24 else 25 return $img; 28 $background = !is_array($background) ? gsImageHelper::HTMLHexToBinArray($background): $background; 29 $background = @imagecolorallocate($img->getData(),$background[0],$background[1],$background[2]); 26 30 } 27 31 28 if(!is_array($background)) 29 { 30 $background = gsImageHelper::HTMLHexToBinArray($background); 31 $background = @imagecolorallocate($img->getData(),$background[0],$background[1],$background[2]); 32 } 32 imagerotate($img->getData(), $params['degree'],$background,isset($params['ignore_transparent'])?$params['ignore_transparent']:null); 33 33 34 if($ret_new) 35 { 36 $tmp = clone $img; 37 $tmp->setData(imagerotate($tmp->getData(), $degree,$background)); 38 39 return $tmp; 40 } 41 else 42 { 43 $img->setData(imagerotate($img->getData(), $degree,$background)); 44 45 return $img; 46 } 34 return $img; 47 35 } 48 36 } plugins/gsImageProcessorPlugin/trunk/lib/gsShadowProcessor.class.php
r13172 r13174 2 2 3 3 /** 4 * gsShadowProcessor 5 * 4 * gsShadowProcessor 5 * 6 6 * @package gsImageProcessorPlugin 7 7 * @version $id$ 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 10 10 * @license GPL Version 2 11 11 */ … … 17 17 * applyShadow 18 18 * 19 * Creates a copy of the image and applies a drop shadow to it and then returns the new image. 20 * the background colour can be changed by passing an HTML hex value (with or without the #) 21 * 19 * Creates a copy of the image and applies a drop shadow to it and then returns the new image. 20 * the background colour can be changed by passing an HTML hex value (with or without the #) 21 * 22 22 * @return gdImage a new image with drop shadow applied 23 * @param gdImage $img 24 * @param string $bgcolour 23 * @param gdImage $img 24 * @param string $bgcolour 25 25 * @param string $shadowPath the path to the shadow images defaults to the plugin data/img directory 26 26 * @access public 27 * 27 * 28 28 */ 29 static public function applyShadow( $img=null,$bgcolour = '000000', $shadowPath = null)29 static public function applyShadow(&$img, $params = array() ) //$bgcolour = '000000', $shadowPath = null) 30 30 { 31 31 // make sure we have the image resource … … 33 33 throw new sfException('Cannot apply a shadow to a non-existant image.'); 34 34 35 $shadow_path = ($shadowPath != null && is_file($shadowPath.'ds_left.png')) ? $shadowPath: realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; 35 $bgcolour = isset($params['color'])?$params['color']: '000000'; 36 37 $shadow_path = (isset($params['shadow_path']) && is_file($params['shadow_path'].'ds_left.png')) ? $params['shadow_path']: realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; 36 38 37 39 $shadows = array(); 38 40 // attempt to load the drop shadow array 39 41 if (!isset($shadows['l']) && empty($shadows['l'])) 40 $shadows['l'] = @ImageCreateFromPNG($shadow_path . "ds_left.png");41 if (!isset($shadows['r']) && empty($shadows['r'])) 42 $shadows['r'] = @ImageCreateFromPNG($shadow_path . "ds_right.png");43 if (!isset($shadows['t']) && empty($shadows['t'])) 44 $shadows['t'] = @ImageCreateFromPNG($shadow_path . "ds_top.png");45 if (!isset($shadows['b']) && empty($shadows['b'])) 46 $shadows['b'] = @ImageCreateFromPNG($shadow_path . "ds_bottom.png");47 if (!isset($shadows['tl']) && empty($shadows['tl'])) 48 $shadows['tl'] = @ImageCreateFromPNG($shadow_path . "ds_tlcorner.png");49 if (!isset($shadows['tr']) && empty($shadows['tr'])) 50 $shadows['tr'] = @ImageCreateFromPNG($shadow_path . "ds_trcorner.png");51 if (!isset($shadows['bl']) && empty($shadows['bl'])) 52 $shadows['bl'] = @ImageCreateFromPNG($shadow_path . "ds_blcorner.png");53 if (!isset($shadows['br']) && empty($shadows['br'])) 54 $shadows['br'] = @ImageCreateFromPNG($shadow_path . "ds_brcorner.png");42 $shadows['l'] = @ImageCreateFromPNG($shadow_path . 'ds_left.png'); 43 if (!isset($shadows['r']) && empty($shadows['r'])) 44 $shadows['r'] = @ImageCreateFromPNG($shadow_path . 'ds_right.png'); 45 if (!isset($shadows['t']) && empty($shadows['t'])) 46 $shadows['t'] = @ImageCreateFromPNG($shadow_path . 'ds_top.png'); 47 if (!isset($shadows['b']) && empty($shadows['b'])) 48 $shadows['b'] = @ImageCreateFromPNG($shadow_path . 'ds_bottom.png'); 49 if (!isset($shadows['tl']) && empty($shadows['tl'])) 50 $shadows['tl'] = @ImageCreateFromPNG($shadow_path . 'ds_tlcorner.png'); 51 if (!isset($shadows['tr']) && empty($shadows['tr'])) 52 $shadows['tr'] = @ImageCreateFromPNG($shadow_path . 'ds_trcorner.png'); 53 if (!isset($shadows['bl']) && empty($shadows['bl'])) 54 $shadows['bl'] = @ImageCreateFromPNG($shadow_path . 'ds_blcorner.png'); 55 if (!isset($shadows['br']) && empty($shadows['br'])) 56 $shadows['br'] = @ImageCreateFromPNG($shadow_path . 'ds_brcorner.png'); 55 57 56 58 // verify all is well … … 74 76 75 77 // pre-process the image 76 $background = gsImageHelper::HTMLHexToBinArray($bgcolour);78 $background = (!is_array($bgcolour)) ? gsImageHelper::HTMLHexToBinArray($bgcolour):$bgcolour; 77 79 $back_color = @ImageColorAllocate($tmp, $background[0], $background[1], $background[2]); 78 80 @imageColorTransparent($back_color); … … 124 126 } 125 127 126 128 127 129 } plugins/gsImageProcessorPlugin/trunk/lib/gsTextOverlayProcessor.class.php
r13172 r13174 55 55 */ 56 56 57 static public function overlayText(&$input_img, $params = array() , $text = null)57 static public function overlayText(&$input_img, $params = array()) 58 58 { 59 59 if( !($input_img instanceof gdImage)) … … 66 66 throw new sfException('Cannot create a text overlay without text parameters!'); 67 67 68 if(!isset($params['font']['file']) || !is_file($params['font']['file'])) 69 throw new sfException('Cannot use a non-existent font. '.$params['font']['file']); 68 if(!isset($params['font']['file']) || !is_file(sfConfig::get('sf_root_dir').$params['font']['file'])) 69 throw new sfException('Cannot use a non-existent font. '.sfConfig::get('sf_root_dir').$params['font']['file']); 70 else 71 $fontFile = sfConfig::get('sf_root_dir').$params['font']['file']; 70 72 71 73 $img = (isset($params['modify_original'])) ? $input_img: new gdImage($input_img->getData()); 72 74 73 // what is this for???74 $safeText = str_replace(array(' ', '-'), array('_', '_'), $text);75 75 76 76 $leading = (isset($params['text']['leading']) && !empty($params['text']['leading'])) ? $params['leading'] : false; 77 $position = isset($params['text']['position']) ? $params['text']['position'] : array(3, 3); 78 $textAngle = isset($params['text']['angle']) ? $params['text']['angle']: 0; 77 78 $dims = (!isset($params['font']['dims'])) ? imagettfbbox($params['font']['size'], 0, sfConfig::get('sf_root_dir').$params['font']['file'], $params['text']['content']):$params['font']['dims']; 79 $position = (isset($params['text']['position'])) ? $params['text']['position'] : array(3, 3); 80 81 /* 82 * Now, we find where we need to place the x-coord of the font's baseline. 83 * This is usually the same as the x-coord of the bottom-left corner, 84 * give or take a pixel or two 85 */ 86 $x = (($dims[0] >= -1) ? abs($dims[0] + 1) * -1: abs($dims[0] + 2))+$position[0]; 87 88 /* 89 * This is the important one. For reasons I admittedly don't fully understand, 90 * the y-coord of the top right (or left) corner of the bounding box is 91 * a negative value equal to the inverse of the baseline height of the 92 * font: I.E. if the b-line height = 24px, then the y-coord of the top- 93 * right = -24. So we set the baseline-y position of the text to the 94 * absolute value of the y-coord of the top-right corner, plus one pixel 95 * to account for anti-aliasing. 96 */ 97 $y = abs($dims[5] + 1)+$position[1]; 98 99 // FIX ME This won't work with autosized images because the autosizer doesn't pay attention to angles 100 $textAngle = (isset($params['text']['angle'])) ? $params['text']['angle']: 0; 79 101 80 102 // add shadow 81 103 if (isset($params['dropshadow'])) 82 104 { 83 $shadowRGB = (isset($params['dropshadow']['color']) && is_array($params['dropshadow']['color']) && count($params['dropshadow']['color']) == 3) ? $params['dropshadow']['color'] : array(0,0,0);105 $shadowRGB = (isset($params['dropshadow']['color']) && is_array($params['dropshadow']['color']) && count($params['dropshadow']['color']) == 3) ? $params['dropshadow']['color'] : ((isset($params['dropshadow']['color']) && !is_array($params['dropshadow']['color'])) ? gsImageHelper::HTMLHexToBinArray($params['dropshadow']['color']): array(0,0,0)); 84 106 $shadowColor = @imagecolorallocatealpha($img->getData(), $shadowRGB[0], $shadowRGB[1], $shadowRGB[2], 0); 85 107 $shadowOffset = (isset($params['dropshadow']['offset']) && is_array($params['dropshadow']['offset']) && count($params['dropshadow']['offset']) == 2) ? $params['dropshadow']['offset'] : array(-2,2); 86 108 87 109 if ($leading) //letter by letter 88 self::createTextWithLeading($img->getData(), array($ position[0]+$shadowOffset[0], $position[1]+$shadowOffset[1]), $text, $textAngle, $params['font']['file'], $params['font']['size'], $shadowColor, $leading);110 self::createTextWithLeading($img->getData(), array($x+$shadowOffset[0], $y+$shadowOffset[1]), $params['text']['content'], $textAngle, $fontFile, $params['font']['size'], $shadowColor, $leading); 89 111 else 90 image TTFText($img->getData(), $params['font']['size'], $textAngle, $position[0] + $shadowOffset[0], ( $position[1]+$params['font']['size'] + $shadowOffset[1] ), $shadowColor, $params['font']['file'], $text);112 imagettftext($img->getData(), $params['font']['size'], $textAngle, $x + $shadowOffset[0], ( $y + $shadowOffset[1] ), $shadowColor, $fontFile, $params['text']['content']); 91 113 } 92 114 93 115 // text color 94 $textRGB = (isset($params['text']['color']) && is_array($params['text']['color']) && count($params['text']['color']) == 3) ? $params['text']['color'] : array(0,0,0); 116 $textRGB = (isset($params['text']['color']) && is_array($params['text']['color']) && count($params['text']['color']) == 3) ? $params['text']['color'] : ((isset($params['text']['color']) && !is_array($params['text']['color'])) ? gsImageHelper::HTMLHexToBinArray($params['text']['color']): array(0,0,0)); 117 $textColor = imagecolorallocate($img->getData(),$textRGB[0],$textRGB[1],$textRGB[2]); 95 118 96 119 // add text 97 120 if ($leading) //letter by letter 98 gsTextImageProcessor::createTextWithLeading($img->getData(), $position, $text, $textAngle, $params['font']['file'], $params['font']['size'], $textRGB, $leading);121 gsTextImageProcessor::createTextWithLeading($img->getData(), array($x,$y), $params['text']['content'], $textAngle, $fontFile, $params['font']['size'], $textColor, $leading); 99 122 else 100 ImageTTFText($img->getData(), $params['font']['size'], $textAngle, $position[0], $position[1]+$params['font']['size'], $textRGB, $params['font']['file'], $text);123 imagettftext($img->getData(), $params['font']['size'], $textAngle, $x, $y, $textColor, $fontFile, $params['text']['content']); 101 124 102 125 return $img; 103 126 } 104 127 128 // FIX ME This won't work with autosized images because the autosizer doesn't pay attention to that 105 129 private function createTextWithLeading(&$im, $textPosition, &$text, &$textAngle, &$font, &$fontSize, &$fontColor, &$leading) 106 130 { plugins/gsImageProcessorPlugin/trunk/lib/gsWatermarkProcessor.class.php
r13172 r13174 2 2 3 3 /** 4 * gsWatermarkProcessor 5 * 4 * gsWatermarkProcessor 5 * 6 6 * @package gsImageProcessorPlugin 7 7 * @version $id$ 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 8 * @copyright 2007 Gnat Solutions, Inc 9 * @author Nathanael D. Noblet <nathanael@gnat.ca> 10 10 * @license GPL Version 2 11 11 */ … … 24 24 * string || array position - positioning of the watermark 25 25 * @option array (x, y) 26 * @option string 27 'top' || 'top left' || 'top right' || 26 * @option string 27 'top' || 'top left' || 'top right' || 28 28 'center' (default) 29 29 'bottom' || bottom left' || 'bottom right' … … 34 34 if($img == null || !($img instanceof gdImage)) 35 35 throw new sfException('Cannot apply a watermark to a non-existant image.'); 36 36 37 37 //set and check alpha 38 38 $alpha = (!empty($params['alpha'])) ? $params['alpha'] /= 100 : 1; 39 39 if ($params['alpha'] > 1 || $params['alpha'] < 0) 40 40 throw new sfException('Watermark alpha setting out of range, use a number within the range of 0-100.'); 41 41 42 42 //set and check watermark image file existance 43 43 $watermarkFile = sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.$params['image']; 44 44 if ( !is_file($watermarkFile) ) 45 45 throw new sfException('Cannot apply a non-existent watermark. Path supplied: '.$watermarkFile); 46 46 47 47 // create the watermark image 48 48 $watermarkImage = ImageCreateFromPng($watermarkFile); 49 49 50 50 //calculate image dimensions 51 51 $mainImageWidth = $img->getWidth(); … … 54 54 $watermarkImageHeight = imagesy($watermarkImage ); 55 55 56 57 56 //determine positioning 58 57 if (isset($params['position'])) … … 78 77 $yPosition = 0; 79 78 break; 80 79 81 80 case 'right': //right center 82 81 $xPosition = ceil($mainImageWidth - $watermarkImageWidth); 83 82 $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 84 83 break; 85 84 86 85 case 'bottom': //bottom center 87 86 $xPosition = ceil(($mainImageWidth / 2) - ($watermarkImageWidth / 2)); 88 87 $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 89 88 break; 90 89 91 90 case 'left': //left center 92 91 $xPosition = 0; 93 92 $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 94 93 break; 95 94 96 95 case 'top right': 97 96 $xPosition = 0; 98 97 $yPosition = ceil($mainImageWidth - $watermarkImageWidth); 99 98 break; 100 99 101 100 case 'top left': 102 101 $xPosition = 0; 103 102 $yPosition = 0; 104 103 break; 105 104 106 105 case 'bottom right': 107 106 $xPosition = ceil($mainImageWidth - $watermarkImageWidth); 108 107 $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 109 108 break; 110 109 111 110 case 'bottom left': 112 111 $xPosition = 0; 113 112 $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 114 113 break; 115 114 116 115 //center it 117 116 default: … … 119 118 $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 120 119 break; 121 120 122 121 } 123 122 } … … 128 127 $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 129 128 } 130 129 131 130 if(sfConfig::get('sf_logging_enabled')) 132 131 sfLogger::getInstance()->debug('Watermarking: xpos='.$xPosition.' ypos='.$yPosition); 133 132 134 133 //create new image to hold merged changes 135 134 $watermarkedImage = imagecreatetruecolor($mainImageWidth, $mainImageHeight); … … 141 140 { 142 141 $returnColor = NULL; 143 142 144 143 //determine the correct pixel location within our watermark 145 144 $watermarkX = $x - $xPosition;// - $mainImageMinWidth; 146 145 $watermarkY = $y - $yPosition;// - $mainImageMinHeight; 147 146 148 147 //fetch color information for both of our images 149 148 $mainRGB = imagecolorsforindex($img->getData(), imagecolorat($img->getData(), $x, $y)); 150 149 151 150 //if our watermark has a non-transparent value at this pixel intersection 152 151 //and we're still within the bounds of the watermark image … … 154 153 { 155 154 $watermarkRGB = imagecolorsforindex($watermarkImage, imagecolorat($watermarkImage, $watermarkX, $watermarkY)); 156 155 157 156 //using image alpha, and user specified alpha, calculate average 158 157 $watermarkAlpha = round(((127 - $watermarkRGB['alpha']) / 127), 2); 159 158 $watermarkAlpha = $watermarkAlpha * $alpha; 160 159 161 160 //calculate the color 'average' between the two - taking into account the specified alpha level 162 161 $avgRed = self::getAverageColor($mainRGB['red'], $watermarkRGB['red'], $watermarkAlpha ); 163 162 $avgGreen = self::getAverageColor($mainRGB['green'], $watermarkRGB['green'], $watermarkAlpha ); 164 163 $avgBlue = self::getAverageColor($mainRGB['blue'], $watermarkRGB['blue'], $watermarkAlpha ); 165 164 166 165 //calculate a color index value using the average RGB values we've determined 167 166 $returnColor = self::getImageColor($watermarkedImage, $avgRed, $avgGreen, $avgBlue); 168 } 167 } 169 168 else //if we're not dealing with an average color here, then let's just copy over the main color 170 169 { 171 170 $returnColor = imagecolorat($img->getData(), $x, $y); 172 171 } 173 172 174 173 //draw the appropriate color onto the return image 175 174 imagesetpixel( $watermarkedImage, $x, $y, $returnColor); … … 180 179 return new gdImage($watermarkedImage); 181 180 } 182 183 181 182 184 183 /** 185 184 * getImageColor … … 194 193 { 195 194 return round( ( ( $colorA * ( 1 - $alpha ) ) + ( $colorB * $alpha ) ) ); 196 } 197 195 } 196 198 197 /** 199 198 * getImageColor … … 214 213 return imagecolorclosest($img, $red, $green, $blue); 215 214 } 216 217 215 216 218 217 /** 219 218 * applyWatermark … … 221 220 * Creates a copy of the image, and merges the copy with the defined watermark image 222 221 * The watermark images should to be stored in 'data/watermarks/' 223 * 224 * 222 * 223 * 225 224 * @return gdImage a new image with watermark applied 226 * @param gdImage $img 225 * @param gdImage $img 227 226 * @param string $watermarkFile 228 227 * @access public 229 * 228 * 230 229 */ 231 230 232 231 static public function applyWatermarkImage(&$img = null, $params = array()) //$watermarkFile = null, $watermarkScale=null, $watermarkPosition=null) 233 232 { … … 235 234 if($img == null || !($img instanceof gdImage)) 236 235 throw new sfException('Cannot apply a watermark to a non-existant image.'); 237 236 238 237 if ( empty($params) || !isseT($params['image'])) 239 238 throw new sfException('Cannot apply a non-existent watermark.'); … … 242 241 if ( !is_file($water_full_path) ) 243 242 throw new sfException('Cannot apply a non-existent watermark.'.$water_full_path); 244 243 245 244 // create the watermark image 246 245 $watermark = ImageCreateFromPng($water_full_path); 247 246 248 247 // scale the watermark, if asked for 249 248 if (isset($params['scale'])) … … 251 250 //pass to image processor, which returns a copy of the image???? 252 251 } 253 252 254 253 // grab height and width of the watermark after scaling 255 254 $watermarkWidth = imagesx($watermark); … … 282 281 $yPosition = 0; 283 282 break; 284 283 285 284 case 'right': //right center 286 285 $xPosition = $imageWidth - $watermarkWidth; 287 286 $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 288 287 break; 289 288 290 289 case 'bottom': //bottom center 291 290 $xPosition = ($imageWidth / 2) - ($watermarkWidth / 2); 292 291 $yPosition = $imageHeight - $watermarkHeight; 293 292 break; 294 293 295 294 case 'left': //left center 296 295 $xPosition = 0; 297 296 $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 298 297 break; 299 298 300 299 case 'top right': 301 300 $xPosition = 0; 302 301 $yPosition = $imageWidth - $watermarkWidth; 303 302 break; 304 303 305 304 case 'top left': 306 305 $xPosition = 0; 307 306 $yPosition = 0; 308 307 break; 309 308 310 309 case 'bottom right': 311 310 $xPosition = $imageWidth - $watermarkWidth; 312 311 $yPosition = $imageHeight - $watermarkHeight; 313 312 break; 314 313 315 314 case 'bottom left': 316 315 $xPosition = 0; 317 316 $yPosition = $imageHeight - $watermarkHeight; 318 317 break; 319 318 320 319 //center it 321 320 default: … … 323 322 $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 324 323 break; 325 324 326 325 } 327 326 } 328 327 } 329 328 330 329 if(!is_array($params['transparent'])) 331 330 $params['transparent'] = gsImageHelper::HTMLHexToBinArray($params['transparent']); 332 331 333 332 @ImageColorTransparent($watermark, @imagecolorallocate($watermark,$params['transparent'][0],$params['transparent'][1],$params['transparent'][2])); 334 333 335 334 @ImageCopyMerge($img->getData(), $watermark, $xPosition, $yPosition, 4, 4, $watermarkWidth-8, $watermarkHeight-8, 100); 336 335 return $img;