Author: stunami
Date: 2010-04-05 12:58:01 +0200 (Mon, 05 Apr 2010)
New Revision: 28985

Modified:
   
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageAlphaMaskGD.class.php
   
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageRoundedCornersGD.class.php
Log:
Updating roundedCorners and alphaMask transforms


Modified: 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageAlphaMaskGD.class.php
===================================================================
--- 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageAlphaMaskGD.class.php
 2010-04-05 10:55:05 UTC (rev 28984)
+++ 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageAlphaMaskGD.class.php
 2010-04-05 10:58:01 UTC (rev 28985)
@@ -20,39 +20,82 @@
  */
 class sfImageAlphaMaskGD extends sfImageTransformAbstract
 {
+  /**
+   * sfImage mask object
+   * 
+   * @var sfImage
+   */
+  protected $mask = null;
+
+  /**
+   * Color
+   *
+   * @var mixed
+   */
+  protected $color = false;
+
   public function __construct($mask, $color = false) 
   {
-    $this->mask  = $mask;
-    $this->color = $color;
+    $this->setMask($mask);
+    $this->setColor($color);
   }
+
+  public function setMask(sfImage $mask)
+  {
+    $this->mask = $mask;
+
+    return true;
+  }
+
+  public function getMask()
+  {
+    return $this->mask;
+  }
+
+  public function setColor($color)
+  {
+    if (preg_match('/#[\d\w]{6}/',$color))
+    {
+      $this->color = strtoupper($color);
+
+      return true;
+    }
+
+    return false;
+  }
+
+  public function getColor()
+  {
+    return $this->color;
+  }
   
   protected function transform(sfImage $image) 
   {
-    $this->image    = $image;
-    $this->mimeType = $image->getMIMEType();
-    $resource       = $image->getAdapter()->getHolder();
-    
-    switch ($this->mimeType) 
+
+    switch ($image->getMIMEType())
     {
       case 'image/png':
-        $this->transformAlpha($resource);
+        $this->transformAlpha($image);
         break;
       case 'image/gif':
       case 'image/jpg':
       default:
-        $this->transformDefault($resource);
+        $this->transformDefault($image);
     }
     
     return $image;
   }
-  
-  private function transformAlpha($resource) 
+
+  private function transformAlpha(sfImage $image)
   {
-    $w = imagesx($resource);
-    $h = imagesy($resource);
+    $w = $image->getWidth();
+    $h = $image->getHeight();
+
+    $resource = $image->getAdapter()->getHolder();
     
-    $mask   = $this->mask->getAdapter()->getHolder();
     $canvas = imagecreatetruecolor($w, $h);
+
+    $mask = $this->getMask()->getAdapter()->getHolder();
     
     $color_background = imagecolorallocate($canvas, 0, 0, 0);
     imagefilledrectangle($canvas, 0, 0, $w, $h, $color_background);
@@ -63,60 +106,48 @@
     {
       for ($y = 0;$y < $h;$y++) 
       {
-        $RealPixel = @imagecolorsforindex($resource, @imagecolorat($resource, 
$x, $y));
-        $MaskPixel = @imagecolorsforindex($mask, @imagecolorat($mask, $x, $y));
-        $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - 
($RealPixel['alpha'] / 127)));
+        $real_pixel = imagecolorsforindex($resource, imagecolorat($resource, 
$x, $y));
+        $mask_pixel = imagecolorsforindex($mask, imagecolorat($mask, $x, $y));
+        $mask_alpha = 127 - (floor($mask_pixel['red'] / 2) * (1 - 
($real_pixel['alpha'] / 127)));
         
-        if (false === $this->color) 
+        if (false === $this->getColor())
         {
-          $newcolor = imagecolorallocatealpha($canvas, $RealPixel['red'], 
$RealPixel['green'], $RealPixel['blue'], intval($MaskAlpha));
+          $newcolor = imagecolorallocatealpha($canvas, $real_pixel['red'], 
$real_pixel['green'], $real_pixel['blue'], intval($mask_alpha));
         }
         else
         {
-          $newcolorPixel    = sscanf($this->color, '#%2x%2x%2x');
-          $newcolorPixel[0] = ($newcolorPixel[0] * $MaskAlpha + 
$RealPixel['red'] * (127 - $MaskAlpha)) / 127;
-          $newcolorPixel[1] = ($newcolorPixel[1] * $MaskAlpha + 
$RealPixel['green'] * (127 - $MaskAlpha)) / 127;
-          $newcolorPixel[2] = ($newcolorPixel[2] * $MaskAlpha + 
$RealPixel['blue'] * (127 - $MaskAlpha)) / 127;
+          $newcolorPixel    = sscanf($this->getColor(), '#%2x%2x%2x');
+          $newcolorPixel[0] = ($newcolorPixel[0] * $mask_alpha + 
$real_pixel['red'] * (127 - $mask_alpha)) / 127;
+          $newcolorPixel[1] = ($newcolorPixel[1] * $mask_alpha + 
$real_pixel['green'] * (127 - $mask_alpha)) / 127;
+          $newcolorPixel[2] = ($newcolorPixel[2] * $mask_alpha + 
$real_pixel['blue'] * (127 - $mask_alpha)) / 127;
           $newcolor         = imagecolorallocate($canvas, $newcolorPixel[0], 
$newcolorPixel[1], $newcolorPixel[2]);
         }
+        
         imagesetpixel($canvas, $x, $y, $newcolor);
       }
     }
+
     imagealphablending($resource, false);
     imagesavealpha($resource, true);
     imagecopy($resource, $canvas, 0, 0, 0, 0, $w, $h);
     
-    imagedestroy($mask);
     imagedestroy($canvas);
   }
-
-  /**
-   * Callback function to extend/alter parameters as given in your 
thumbnailing.yml.
-   *
-   * This callback adds the resources path to a mask image
-   *
-   * @param  sfImage $sourceImage The original image
-   * @param  array   $parameters  Configured parameters for this transformation
-   * @return array   $parameters  Extended/altered parameters
-   */
-  public static function prepareParameters($sourceImage, $parameters)
+  
+  protected function transformDefault(sfImage $image)
   {
-    if (!array_key_exists('mask', $parameters))
-    {
-      return $parameters;
-    }
+    $w = $image->getWidth();
+    $h = $image->getHeight();
 
-    $user_resources_dir   = sfConfig::get('sf_data_dir') . '/resources';
-    $plugin_paths = ProjectConfiguration::getActive()->getAllPluginPaths();
-    $plugin_resources_dir = 
$plugin_paths['sfImageTransformExtraPlugin'].'/data/example-resources';
-    if (file_exists($user_resources_dir . '/' . $parameters['mask']))
-    {
-      $parameters['mask'] = new sfImage($user_resources_dir . '/' . 
$parameters['mask']);
-    }
-    else if (file_exists($plugin_resources_dir . '/' . $parameters['mask']))
-    {
-      $parameters['mask'] = new sfImage($plugin_resources_dir . '/' . 
$parameters['mask']);
-    }
-    return $parameters;
+    $resource = $image->getAdapter()->getHolder();
+
+    $mask = $this->getMask()->getAdapter()->getHolder();
+    
+    imagealphablending($resource, true);
+    $resource_transparent = imagecolorallocate($resource, 0, 0, 0);
+    imagecolortransparent($resource, $resource_transparent);
+    
+    // Copy $mask over the top of $resource maintaining the Alpha transparency
+    imagecopymerge($resource, $mask, 0, 0, 0, 0, $w, $h, 100);
   }
 }

Modified: 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageRoundedCornersGD.class.php
===================================================================
--- 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageRoundedCornersGD.class.php
    2010-04-05 10:55:05 UTC (rev 28984)
+++ 
plugins/sfImageTransformPlugin/trunk/lib/transforms/GD/sfImageRoundedCornersGD.class.php
    2010-04-05 10:58:01 UTC (rev 28985)
@@ -18,155 +18,92 @@
  * @subpackage transforms
  * @author     Christian Schaefer <[email protected]>
  */
-class sfImageRoundedCornersGD extends sfImageTransformAbstract
+class sfImageRoundedCornersGD extends sfImageAlphaMaskGD
 {
+
+  protected $radius = 5;
+
   public function __construct($radius, $color = false) 
   {
-    $this->radius = $radius;
-    $this->color  = $color;
+    $this->setRadius($radius);
+    $this->setColor($color);
   }
   
   protected function transform(sfImage $image) 
-  {
-    $this->image    = $image;
-    $this->mimeType = $image->getMIMEType();
-    $resource       = $image->getAdapter()->getHolder();
+  {    
+    $this->setMask($this->createMask($image, $image->getWidth(), 
$image->getHeight()));
     
-    switch ($this->mimeType) 
-    {
-      case 'image/png':
-        $this->transformAlpha($resource);
-        break;
-      case 'image/gif':
-      case 'image/jpg':
-      default:
-        $this->transformDefault($resource);
-    }
-    
-    return $image;
+    return parent::transform($image);
   }
-  
-  private function transformAlpha($resource) 
+
+  public function setRadius($radius)
   {
-    $w = imagesx($resource);
-    $h = imagesy($resource);
-    
-    $mask   = $this->getMask($w, $h);
-    $canvas = imagecreatetruecolor($w, $h);
-    
-    $color_background = imagecolorallocate($canvas, 0, 0, 0);
-    imagefilledrectangle($canvas, 0, 0, $w, $h, $color_background);
-    imagealphablending($canvas, false);
-    imagesavealpha($canvas, true);
-    
-    for ($x = 0;$x < $w;$x++) 
+    if(is_numeric($radius) && $radius > 0)
     {
-      for ($y     = 0;$y < $h;$y++) 
-      {
-        $RealPixel = @imagecolorsforindex($resource, @imagecolorat($resource, 
$x, $y));
-        $MaskPixel = @imagecolorsforindex($mask, @imagecolorat($mask, $x, $y));
-        $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - 
($RealPixel['alpha'] / 127)));
-        
-        if (false === $this->color) 
-        {
-          $newcolor = imagecolorallocatealpha($canvas, $RealPixel['red'], 
$RealPixel['green'], $RealPixel['blue'], intval($MaskAlpha));
-        }
-        else
-        {
-          $newcolorPixel    = sscanf($this->color, '#%2x%2x%2x');
-          $newcolorPixel[0] = ($newcolorPixel[0] * $MaskAlpha + 
$RealPixel['red'] * (127 - $MaskAlpha)) / 127;
-          $newcolorPixel[1] = ($newcolorPixel[1] * $MaskAlpha + 
$RealPixel['green'] * (127 - $MaskAlpha)) / 127;
-          $newcolorPixel[2] = ($newcolorPixel[2] * $MaskAlpha + 
$RealPixel['blue'] * (127 - $MaskAlpha)) / 127;
-          $newcolor         = imagecolorallocate($canvas, $newcolorPixel[0], 
$newcolorPixel[1], $newcolorPixel[2]);
-        }
-        imagesetpixel($canvas, $x, $y, $newcolor);
-      }
+      $this->radius = $radius;
+
+      return true;
     }
-    imagealphablending($resource, false);
-    imagesavealpha($resource, true);
-    imagecopy($resource, $canvas, 0, 0, 0, 0, $w, $h);
-    
-    imagedestroy($mask);
-    imagedestroy($canvas);
+
+    return false;
   }
-  
-  private function transformDefault($resource) 
+
+  public function getRadius()
   {
-    $w = imagesx($resource);
-    $h = imagesy($resource);
-    
-    imagealphablending($resource, true);
-    $resource_transparent = imagecolorallocate($resource, 0, 0, 0);
-    imagecolortransparent($resource, $resource_transparent);
-    //$resource_transparent = $this->getTransparentColor($resource);
-    $mask = $this->getMask($w, $h);
-    // Copy $mask over the top of $resource maintaining the Alpha transparency
-    imagecopymerge($resource, $mask, 0, 0, 0, 0, $w, $h, 100);
+    return $this->radius;
   }
-  
-  private function getMask($w, $h) 
+
+  protected function createMask(sfImage $image, $w, $h)
   {
-    // Create a mask png image of the area you want in the circle/ellipse (a 
‘magicpink’ image with a black shape on it, with black set to the colour of 
alpha transparency) - $mask
-    $mask = imagecreatetruecolor($w, $h);
-    imagealphablending($mask, true);
+    // Create a mask png image of the area you want in the circle/ellipse (a 
'magicpink' image with a black shape on it, with black set to the colour of 
alpha transparency) - $mask
+    $mask = $image->getAdapter()->getTransparentImage($w, $h);
+
     // Set the masking colours
-    if (false === $this->color||'image/png' == $this->mimeType) 
+    if (false === $this->getColor() ||'image/png' == $image->getMIMEType())
     {
       $mask_black = imagecolorallocate($mask, 0, 0, 0);
     }
     else
     {
-      $colorPixel = sscanf($this->color, '#%2x%2x%2x');
-      $mask_black = imagecolorallocate($mask, $colorPixel[0], $colorPixel[1], 
$colorPixel[2]);
+      $mask_black = $image->getAdapter()->getColorByHex($mask, 
$this->getColor());
     }
-    $mask_transparent = imagecolorallocate($mask, 255, 255, 255);
+
+    // Cannot use white as transparent mask if color is set to white
+    if($this->getColor() === '#FFFFFF' || $this->getColor() === false)
+    {
+      $mask_transparent = imagecolorallocate($mask, 255, 0, 0);
+    }
+
+    else
+    {
+      $mask_color = imagecolorsforindex($mask, 
imagecolorat($image->getAdapter()->getHolder(), 0, 0));
+      $mask_transparent = imagecolorallocate($mask, $mask_color['red'], 
$mask_color['green'], $mask_color['blue']);
+    }
+    
     imagecolortransparent($mask, $mask_transparent);
     imagefill($mask, 0, 0, $mask_black);
+
     // Draw the rounded rectangle for the mask
-    $this->imagefillroundedrect($mask, 0, 0, $w, $h, $this->radius, 
$mask_transparent);
-    return $mask;
+    $this->imagefillroundedrect($mask, 0, 0, $w, $h, $this->getRadius(), 
$mask_transparent);
+
+    $mask_image = clone $image;
+    $mask_image->getAdapter()->setHolder($mask);
+
+    return $mask_image;
   }
   
-  private function imagefillroundedrect($im, $x, $y, $cx, $cy, $rad, $col) 
+  protected function imagefillroundedrect($im, $x, $y, $cx, $cy, $rad, $col)
   {
     // Draw the middle cross shape of the rectangle
     imagefilledrectangle($im, $x, $y + $rad, $cx, $cy - $rad, $col);
     imagefilledrectangle($im, $x + $rad, $y, $cx - $rad, $cy, $col);
     
     $dia = $rad * 2;
+    
     // Now fill in the rounded corners
     imagefilledellipse($im, $x + $rad, $y + $rad, $rad * 2, $dia, $col);
     imagefilledellipse($im, $x + $rad, $cy - $rad, $rad * 2, $dia, $col);
     imagefilledellipse($im, $cx - $rad, $cy - $rad, $rad * 2, $dia, $col);
     imagefilledellipse($im, $cx - $rad, $y + $rad, $rad * 2, $dia, $col);
   }
-
-  /**
-   * Callback function to extend/alter parameters as given in your 
thumbnailing.yml.
-   *
-   * This callback adds the resources path to an overlay image
-   *
-   * @param  sfImage $sourceImage The original image
-   * @param  array   $parameters  Configured parameters for this transformation
-   * @return array   $parameters  Extended/altered parameters
-   */
-  public static function prepareParameters($sourceImage, $parameters)
-  {
-    if (!array_key_exists('overlay', $parameters))
-    {
-      return $parameters;
-    }
-
-    $user_resources_dir   = sfConfig::get('sf_data_dir') . '/resources';
-    $plugin_resources_dir = sfConfig::get('sf_plugins_dir') . 
'/sfImageTransformExtraPlugin/data/example-resources';
-    if (file_exists($user_resources_dir . '/' . $parameters['overlay']))
-    {
-      $parameters['overlay'] = new sfImage($user_resources_dir . '/' . 
$parameters['overlay']);
-    }
-    else if (file_exists($plugin_resources_dir . '/' . $parameters['overlay']))
-    {
-      $parameters['overlay'] = new sfImage($plugin_resources_dir . '/' . 
$parameters['overlay']);
-    }
-    return $parameters;
-  }
 }

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to