Development

Changeset 13174

You must first sign up to be able to contribute.

Changeset 13174

Show
Ignore:
Timestamp:
11/20/08 06:02:45 (4 years ago)
Author:
gnat
Message:

update to the latest version with array style params

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/gsImageProcessorPlugin/trunk/lib/ImageProcessorHelper.class.php

    r13172 r13174  
    3232            throw new sfException('Missing required params dimensions array!'); 
    3333 
    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 
    3535        if(isset($params['save']) && !empty($output_file)) 
    3636        { 
     
    3939 
    4040            // 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']; 
    4343            else 
    4444            { 
     
    5252                $output_type = 'png'; 
    5353 
    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']; 
    6060            else 
    6161                $save = false; 
     
    6969            $cparams     = &$params['create']; 
    7070 
    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; 
    9079            switch($depth) 
    9180            { 
    9281                case '8': 
    9382                    $im = imagecreate($imageWidth, $imageHeight); 
     83                    break; 
    9484                case '32': 
    9585                default: 
     
    9888            } 
    9989 
     90            if(!$im) 
     91                throw new sfException('Unable to create image from scratch'); 
     92 
    10093            // 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; 
    10396 
    10497            if(isset($cparams['bgcolor']) || isset($cparams['bgcolour'])) 
     
    109102                @imagefill($im, 0, 0, $bgcolor); 
    110103            } 
    111  
    112             if(!$im) 
    113                 throw new sfException('Unable to create image from scratch'); 
    114104 
    115105            $img = new gdImage($im); 
     
    123113        } 
    124114 
    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                } 
    149151                else 
    150152                { 
    151                     $img = gsResizeProcessor::resizeToSize($img,0,$params['max_height']); 
    152  
    153                     if($img->getWidth() > $params['max_width']) // still too wide 
    154                         $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); 
    155157                } 
    156158            } 
    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         */ 
    168174        if( isset($params['colorize']) && is_array($params['colorize']) ) 
    169175            $img = gsColorizeProcessor::colorize($img,$params['colorize']); 
    170176 
     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         */ 
    171183        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         */ 
    174192        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         */ 
    180207        if( isset($params['watermark']) && is_array($params['watermark'])) 
    181208            $img = gsWatermarkProcessor::createWatermark($img, $params['watermark']); 
    182209 
     210        /* 
     211         * textoverlay: 
     212         * 
     213         */ 
    183214        if( isset($params['textoverlay']) && is_array($params['textoverlay'])) 
    184215            $img = gsTextOverlayProcessor::overlayText($img, $params['textoverlay']); 
    185216 
    186217        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); 
    188219 
    189220        return $img; 
    190221    } 
    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 of 
    201          * the left corners of the image may be negative, due to anti-aliasing 
    202          * and various other font metrics issues, so we need to use absolute 
    203          * values) 
    204          */ 
    205  
    206         $width = abs($dims[2] - $dims[0]); 
    207  
    208         /* 
    209          * Playing around with dimensions depending on just HOW far the text 
    210          * moves off the left side 
    211          */ 
    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 same 
    218          * logic 
    219          */ 
    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 two 
    231          */ 
    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 is 
    237          * a negative value equal to the inverse of the baseline height of the 
    238          * 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 the 
    240          * absolute value of the y-coord of the top-right corner, plus one pixel 
    241          * to account for anti-aliasing. 
    242          */ 
    243  
    244         $y = abs($dims[5] + 1); 
    245  
    246         return array('width'=>$boxWidth,'height'=>$boxHeight); 
    247     } 
    248222} 
  • plugins/gsImageProcessorPlugin/trunk/lib/gdImage.class.php

    r13172 r13174  
    226226        } 
    227227 
    228         if ($ext == 'png') 
     228        if ($ext == 'jpeg') 
     229            $this->_is_saved= $func($this->_img, $filename, $quality); 
     230        else 
    229231            $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        { 
    234235            $err = error_get_last(); 
    235236          throw new sfException('Unable to save image to filesystem. '.$err['message']); 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsBorderProcessor.class.php

    r13172 r13174  
    33/** 
    44 * gsBorderProcessor 
    5  *  
     5 * 
    66 * @package gsImageProcessorPlugin 
    77 * @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> 
    1010 * @license GPL Version 2 
    1111 */ 
     
    1313class gsBorderProcessor 
    1414{ 
    15     static public function addBorder(&$img, $size = 10, $colour= null) 
     15    static public function addBorder(&$input_img, $params = array()) //$width = 10, $color= null) 
    1616    { 
    17         if($colour == null) 
    18             $colour = array(0,0,0); 
     17        $img = (isset($params['clone'])) ? new gdImage($input_img->getData()): $input_img; 
    1918 
    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)); 
    2222 
    23         $mask = @imagecolorallocate($img->getData(),$colour[0],$colour[1],$colour[2]); 
     23        if(!is_array($color)) 
     24            $color = gsImageHelper::HTMLHexToBinArray($color); 
    2425 
    25         @imagefilledrectangle($img->getData(),0,0,$size,$img->getHeight(),$mask); 
     26        $mask = @imagecolorallocate($img->getData(),$color[0],$color[1],$color[2]); 
    2627 
    27         @imagefilledrectangle($img->getData(),0,0,$img->getWidth(),$size,$mask); 
     28        @imagefilledrectangle($img->getData(),0,0,$width,$img->getHeight(),$mask); 
    2829 
    29         @imagefilledrectangle($img->getData(),($img->getWidth()-$size),0,$img->getWidth(),$img->getHeight(),$mask); 
     30        @imagefilledrectangle($img->getData(),0,0,$img->getWidth(),$width,$mask); 
    3031 
    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); 
    3235 
    3336        return $img; 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsColorizeProcessor.class.php

    r13172 r13174  
    1010 * @license GPL Version 2 
    1111 * @description : colourizes photos. 
    12  * @param $colour - array of RBG values, from -255 to +255 where 0 == no change 
     12 * @param $color - array of RBG values, from -255 to +255 where 0 == no change 
    1313                   to get these numbers, use photoshop, grayscale your image 
    1414                   duplicate onto a new layer & colourize as you wish. Use 
     
    1717 */ 
    1818 
    19 class gsColorizeProcessor  
     19class gsColorizeProcessor 
    2020{ 
    21     static public function colourize(&$img,$colour = null,$ret_new=false) 
     21    static public function colourize(&$input_img,$params = array()) //$color = null,$ret_new=false) 
    2222    { 
    23         if($colour == null) 
     23        $img = (isset($params['clone'])) ? new gdImage($input_img->getData()): $input_img; 
     24 
     25        if (!isset($params['color'])) 
    2426            throw new sfException('No Color provided to colourize with!'); 
    2527 
    26         if(!is_array($colour)) 
    27             $colour = gsImageHelper::HTMLHexToBinArray($colour); 
     28        $color = (!is_array($params['color'])) ? gsImageHelper::HTMLHexToBinArray($color): $params['color']; 
    2829 
    29         if(count($colour) != 3) 
     30        if(count($color) != 3) 
    3031            throw new sfException('Wrong parameter count for colourization!'); 
    3132 
    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]); 
    4435 
    45             return $img; 
    46         } 
     36        return $img; 
    4737    } 
    4838} 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsRotationProcessor.class.php

    r13172 r13174  
    1313 */ 
    1414 
    15 class gsRotationProcessor  
     15class gsRotationProcessor 
    1616{ 
    17     static public function rotate(&$img,$degree ,$background = '000000',$ret_new=false
     17    static public function rotate(&$input_img, $params = array()
    1818    { 
    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) 
    2027        { 
    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]); 
    2630        } 
    2731 
    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); 
    3333 
    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; 
    4735    } 
    4836} 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsShadowProcessor.class.php

    r13172 r13174  
    22 
    33/** 
    4  * gsShadowProcessor  
    5  *  
     4 * gsShadowProcessor 
     5 * 
    66 * @package gsImageProcessorPlugin 
    77 * @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> 
    1010 * @license GPL Version 2 
    1111 */ 
     
    1717     * applyShadow 
    1818     * 
    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     * 
    2222     * @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 
    2525     * @param string $shadowPath the path to the shadow images defaults to the plugin data/img directory 
    2626     * @access public 
    27      *  
     27     * 
    2828     */ 
    29     static public function applyShadow($img=null,$bgcolour = '000000', $shadowPath = null) 
     29    static public function applyShadow(&$img, $params = array() ) //$bgcolour = '000000', $shadowPath = null) 
    3030    { 
    3131        // make sure we have the image resource 
     
    3333            throw new sfException('Cannot apply a shadow to a non-existant image.'); 
    3434 
    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; 
    3638 
    3739        $shadows = array(); 
    3840        // attempt to load the drop shadow array 
    3941        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'); 
    5557 
    5658        // verify all is well 
     
    7476 
    7577        // pre-process the image 
    76         $background = gsImageHelper::HTMLHexToBinArray($bgcolour)
     78        $background = (!is_array($bgcolour)) ? gsImageHelper::HTMLHexToBinArray($bgcolour):$bgcolour
    7779        $back_color = @ImageColorAllocate($tmp, $background[0], $background[1], $background[2]); 
    7880        @imageColorTransparent($back_color); 
     
    124126    } 
    125127 
    126     
     128 
    127129} 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsTextOverlayProcessor.class.php

    r13172 r13174  
    5555     */ 
    5656 
    57   static public function overlayText(&$input_img, $params = array(), $text = null
     57  static public function overlayText(&$input_img, $params = array()
    5858    { 
    5959        if( !($input_img instanceof gdImage)) 
     
    6666            throw new sfException('Cannot create a text overlay without text parameters!'); 
    6767 
    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']; 
    7072 
    7173        $img = (isset($params['modify_original'])) ? $input_img: new gdImage($input_img->getData()); 
    7274 
    73         // what is this for??? 
    74       $safeText         = str_replace(array(' ', '-'), array('_', '_'), $text); 
    7575 
    7676      $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; 
    79101 
    80102      // add shadow 
    81103      if (isset($params['dropshadow'])) 
    82104      { 
    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)); 
    84106            $shadowColor  = @imagecolorallocatealpha($img->getData(), $shadowRGB[0], $shadowRGB[1], $shadowRGB[2], 0); 
    85107            $shadowOffset = (isset($params['dropshadow']['offset']) && is_array($params['dropshadow']['offset']) && count($params['dropshadow']['offset']) == 2) ? $params['dropshadow']['offset'] : array(-2,2); 
    86108 
    87109            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); 
    89111            else 
    90                 imageTTFText($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']); 
    91113      } 
    92114 
    93115    // 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]); 
    95118 
    96119        // add text 
    97120      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); 
    99122      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']); 
    101124 
    102125      return $img; 
    103126    } 
    104127 
     128    // FIX ME This won't work with autosized images because the autosizer doesn't pay attention to that 
    105129    private function createTextWithLeading(&$im, $textPosition, &$text, &$textAngle, &$font, &$fontSize, &$fontColor, &$leading) 
    106130    { 
  • plugins/gsImageProcessorPlugin/trunk/lib/gsWatermarkProcessor.class.php

    r13172 r13174  
    22 
    33/** 
    4  * gsWatermarkProcessor  
    5  *  
     4 * gsWatermarkProcessor 
     5 * 
    66 * @package gsImageProcessorPlugin 
    77 * @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> 
    1010 * @license GPL Version 2 
    1111 */ 
     
    2424     *      string || array position - positioning of the watermark 
    2525     *          @option array (x, y) 
    26      *          @option string  
    27                     'top' || 'top left' || 'top right' ||  
     26     *          @option string 
     27                    'top' || 'top left' || 'top right' || 
    2828                    'center' (default) 
    2929                    'bottom' || bottom left' || 'bottom right' 
     
    3434        if($img == null || !($img instanceof gdImage)) 
    3535            throw new sfException('Cannot apply a watermark to a non-existant image.'); 
    36          
     36 
    3737        //set and check alpha 
    3838        $alpha = (!empty($params['alpha'])) ? $params['alpha'] /= 100 : 1; 
    3939        if ($params['alpha'] > 1 || $params['alpha'] < 0) 
    4040            throw new sfException('Watermark alpha setting out of range, use a number within the range of 0-100.'); 
    41          
     41 
    4242        //set and check watermark image file existance 
    4343        $watermarkFile = sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.$params['image']; 
    4444        if ( !is_file($watermarkFile) ) 
    4545            throw new sfException('Cannot apply a non-existent watermark. Path supplied: '.$watermarkFile); 
    46          
     46 
    4747        // create the watermark image 
    4848        $watermarkImage = ImageCreateFromPng($watermarkFile); 
    49          
     49 
    5050        //calculate image dimensions 
    5151        $mainImageWidth = $img->getWidth(); 
     
    5454        $watermarkImageHeight = imagesy($watermarkImage ); 
    5555 
    56          
    5756        //determine positioning 
    5857        if (isset($params['position'])) 
     
    7877                        $yPosition = 0; 
    7978                    break; 
    80                      
     79 
    8180                    case 'right': //right center 
    8281                        $xPosition = ceil($mainImageWidth - $watermarkImageWidth); 
    8382                        $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 
    8483                    break; 
    85                      
     84 
    8685                    case 'bottom': //bottom center 
    8786                        $xPosition = ceil(($mainImageWidth / 2) - ($watermarkImageWidth / 2)); 
    8887                        $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 
    8988                    break; 
    90                      
     89 
    9190                    case 'left': //left center 
    9291                        $xPosition = 0; 
    9392                        $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 
    9493                    break; 
    95                      
     94 
    9695                    case 'top right': 
    9796                        $xPosition = 0; 
    9897                        $yPosition = ceil($mainImageWidth - $watermarkImageWidth); 
    9998                    break; 
    100                      
     99 
    101100                    case 'top left': 
    102101                        $xPosition = 0; 
    103102                        $yPosition = 0; 
    104103                    break; 
    105                      
     104 
    106105                    case 'bottom right': 
    107106                        $xPosition = ceil($mainImageWidth - $watermarkImageWidth); 
    108107                        $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 
    109108                    break; 
    110                      
     109 
    111110                    case 'bottom left': 
    112111                        $xPosition = 0; 
    113112                        $yPosition = ceil($mainImageHeight - $watermarkImageHeight); 
    114113                    break; 
    115                      
     114 
    116115                    //center it 
    117116                    default: 
     
    119118                        $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 
    120119                    break; 
    121                      
     120 
    122121                } 
    123122            } 
     
    128127            $yPosition = ceil(($mainImageHeight / 2) - ($watermarkImageHeight / 2)); 
    129128        } 
    130          
     129 
    131130        if(sfConfig::get('sf_logging_enabled')) 
    132131            sfLogger::getInstance()->debug('Watermarking: xpos='.$xPosition.' ypos='.$yPosition); 
    133          
     132 
    134133        //create new image to hold merged changes 
    135134        $watermarkedImage = imagecreatetruecolor($mainImageWidth, $mainImageHeight); 
     
    141140            { 
    142141                $returnColor = NULL; 
    143             
     142 
    144143                //determine the correct pixel location within our watermark 
    145144                $watermarkX = $x - $xPosition;// - $mainImageMinWidth; 
    146145                $watermarkY = $y - $yPosition;// - $mainImageMinHeight; 
    147             
     146 
    148147                //fetch color information for both of our images 
    149148                $mainRGB = imagecolorsforindex($img->getData(), imagecolorat($img->getData(), $x, $y)); 
    150             
     149 
    151150                //if our watermark has a non-transparent value at this pixel intersection 
    152151                //and we're still within the bounds of the watermark image 
     
    154153                { 
    155154                    $watermarkRGB = imagecolorsforindex($watermarkImage, imagecolorat($watermarkImage, $watermarkX, $watermarkY)); 
    156                   
     155 
    157156                    //using image alpha, and user specified alpha, calculate average 
    158157                    $watermarkAlpha = round(((127 - $watermarkRGB['alpha']) / 127), 2); 
    159158                    $watermarkAlpha = $watermarkAlpha * $alpha; 
    160             
     159 
    161160                    //calculate the color 'average' between the two - taking into account the specified alpha level 
    162161                    $avgRed = self::getAverageColor($mainRGB['red'], $watermarkRGB['red'], $watermarkAlpha ); 
    163162                    $avgGreen = self::getAverageColor($mainRGB['green'], $watermarkRGB['green'], $watermarkAlpha ); 
    164163                    $avgBlue = self::getAverageColor($mainRGB['blue'], $watermarkRGB['blue'], $watermarkAlpha ); 
    165                   
     164 
    166165                    //calculate a color index value using the average RGB values we've determined 
    167166                    $returnColor = self::getImageColor($watermarkedImage, $avgRed, $avgGreen, $avgBlue); 
    168                 }  
     167                } 
    169168                else //if we're not dealing with an average color here, then let's just copy over the main color 
    170169                { 
    171170                  $returnColor = imagecolorat($img->getData(), $x, $y); 
    172171                } 
    173   
     172 
    174173                //draw the appropriate color onto the return image 
    175174                imagesetpixel( $watermarkedImage, $x, $y, $returnColor); 
     
    180179        return new gdImage($watermarkedImage); 
    181180    } 
    182       
    183      
     181 
     182 
    184183    /** 
    185184     * getImageColor 
     
    194193    { 
    195194        return round( ( ( $colorA * ( 1 - $alpha ) ) + ( $colorB  * $alpha ) ) ); 
    196     }     
    197   
     195    } 
     196 
    198197    /** 
    199198     * getImageColor 
     
    214213        return imagecolorclosest($img, $red, $green, $blue); 
    215214    } 
    216      
    217      
     215 
     216 
    218217    /** 
    219218     * applyWatermark 
     
    221220     * Creates a copy of the image, and merges the copy with the defined watermark image 
    222221     * The watermark images should to be stored in 'data/watermarks/' 
    223      *  
    224      *  
     222     * 
     223     * 
    225224     * @return gdImage a new image with watermark applied 
    226      * @param gdImage $img  
     225     * @param gdImage $img 
    227226     * @param string $watermarkFile 
    228227     * @access public 
    229      *  
     228     * 
    230229     */ 
    231       
     230 
    232231    static public function applyWatermarkImage(&$img = null, $params = array()) //$watermarkFile = null, $watermarkScale=null, $watermarkPosition=null) 
    233232    { 
     
    235234        if($img == null || !($img instanceof gdImage)) 
    236235            throw new sfException('Cannot apply a watermark to a non-existant image.'); 
    237              
     236 
    238237        if ( empty($params) || !isseT($params['image'])) 
    239238            throw new sfException('Cannot apply a non-existent watermark.'); 
     
    242241        if ( !is_file($water_full_path) ) 
    243242            throw new sfException('Cannot apply a non-existent watermark.'.$water_full_path); 
    244          
     243 
    245244        // create the watermark image 
    246245        $watermark = ImageCreateFromPng($water_full_path); 
    247                  
     246 
    248247        // scale the watermark, if asked for 
    249248        if (isset($params['scale'])) 
     
    251250            //pass to image processor, which returns a copy of the image???? 
    252251        } 
    253          
     252 
    254253        // grab height and width of the watermark after scaling 
    255254        $watermarkWidth = imagesx($watermark); 
     
    282281                        $yPosition = 0; 
    283282                    break; 
    284                      
     283 
    285284                    case 'right': //right center 
    286285                        $xPosition = $imageWidth - $watermarkWidth; 
    287286                        $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 
    288287                    break; 
    289                      
     288 
    290289                    case 'bottom': //bottom center 
    291290                        $xPosition = ($imageWidth / 2) - ($watermarkWidth / 2); 
    292291                        $yPosition = $imageHeight - $watermarkHeight; 
    293292                    break; 
    294                      
     293 
    295294                    case 'left': //left center 
    296295                        $xPosition = 0; 
    297296                        $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 
    298297                    break; 
    299                      
     298 
    300299                    case 'top right': 
    301300                        $xPosition = 0; 
    302301                        $yPosition = $imageWidth - $watermarkWidth; 
    303302                    break; 
    304                      
     303 
    305304                    case 'top left': 
    306305                        $xPosition = 0; 
    307306                        $yPosition = 0; 
    308307                    break; 
    309                      
     308 
    310309                    case 'bottom right': 
    311310                        $xPosition = $imageWidth - $watermarkWidth; 
    312311                        $yPosition = $imageHeight - $watermarkHeight; 
    313312                    break; 
    314                      
     313 
    315314                    case 'bottom left': 
    316315                        $xPosition = 0; 
    317316                        $yPosition = $imageHeight - $watermarkHeight; 
    318317                    break; 
    319                      
     318 
    320319                    //center it 
    321320                    default: 
     
    323322                        $yPosition = ($imageHeight / 2) - ($watermarkHeight / 2); 
    324323                    break; 
    325                      
     324 
    326325                } 
    327326            } 
    328327        } 
    329          
     328 
    330329        if(!is_array($params['transparent'])) 
    331330            $params['transparent'] = gsImageHelper::HTMLHexToBinArray($params['transparent']); 
    332331 
    333332        @ImageColorTransparent($watermark, @imagecolorallocate($watermark,$params['transparent'][0],$params['transparent'][1],$params['transparent'][2])); 
    334          
     333 
    335334        @ImageCopyMerge($img->getData(), $watermark, $xPosition, $yPosition, 4, 4, $watermarkWidth-8, $watermarkHeight-8, 100); 
    336335        return $img;