Author: caefer
Date: 2010-03-25 11:51:43 +0100 (Thu, 25 Mar 2010)
New Revision: 28782
Modified:
plugins/sfImageTransformExtraPlugin/trunk/TODO
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageAlphaMaskGD.class.php
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageRoundedCornersGD.class.php
plugins/sfImageTransformExtraPlugin/trunk/package.xml.tmpl
Log:
refactored transformations.
sfImageRoundedCornersGD now extends sfImageAlphaMaskGD as it is only a
specialisation of it and now shares a lot of its code rather than copying it.
TODO and package.xml.tmpl are updated for this as well
Modified: plugins/sfImageTransformExtraPlugin/trunk/TODO
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/TODO 2010-03-25 10:22:21 UTC
(rev 28781)
+++ plugins/sfImageTransformExtraPlugin/trunk/TODO 2010-03-25 10:51:43 UTC
(rev 28782)
@@ -1,5 +1,4 @@
* Create abstract base class for sources to reduce copy and paste code
- * Make sfImageRoundedCornersGD use sfImageAlphaMaskGD to reduce copy and
paste code
* HTTP cache
* remove sfImageTransformExtraPluginConfiguration::setCacheKey()
* write more tests to produce image urls
Modified:
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageAlphaMaskGD.class.php
===================================================================
---
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageAlphaMaskGD.class.php
2010-03-25 10:22:21 UTC (rev 28781)
+++
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageAlphaMaskGD.class.php
2010-03-25 10:51:43 UTC (rev 28782)
@@ -22,16 +22,15 @@
{
public function __construct($mask, $color = false)
{
- $this->mask = $mask;
+ $this->mask = $mask->getAdapter()->getHolder();
$this->color = $color;
}
protected function transform(sfImage $image)
{
- $this->image = $image;
+ $resource = $image->getAdapter()->getHolder();
$this->mimeType = $image->getMIMEType();
- $resource = $image->getAdapter()->getHolder();
-
+
switch ($this->mimeType)
{
case 'image/png':
@@ -40,18 +39,17 @@
case 'image/gif':
case 'image/jpg':
default:
- //$this->transformDefault($resource); not yet implemented
+ $this->transformDefault($resource);
}
return $image;
}
-
+
private function transformAlpha($resource)
{
$w = imagesx($resource);
$h = imagesy($resource);
- $mask = $this->mask->getAdapter()->getHolder();
$canvas = imagecreatetruecolor($w, $h);
$color_background = imagecolorallocate($canvas, 0, 0, 0);
@@ -64,7 +62,7 @@
for ($y = 0;$y < $h;$y++)
{
$RealPixel = @imagecolorsforindex($resource, @imagecolorat($resource,
$x, $y));
- $MaskPixel = @imagecolorsforindex($mask, @imagecolorat($mask, $x, $y));
+ $MaskPixel = @imagecolorsforindex($this->mask,
@imagecolorat($this->mask, $x, $y));
$MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 -
($RealPixel['alpha'] / 127)));
if (false === $this->color)
@@ -86,9 +84,22 @@
imagesavealpha($resource, true);
imagecopy($resource, $canvas, 0, 0, 0, 0, $w, $h);
- imagedestroy($mask);
+ imagedestroy($this->mask);
imagedestroy($canvas);
}
+
+ private function transformDefault($resource)
+ {
+ $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);
+ // Copy $mask over the top of $resource maintaining the Alpha transparency
+ imagecopymerge($resource, $this->mask, 0, 0, 0, 0, $w, $h, 100);
+ }
/**
* Callback function to extend/alter parameters as given in your
thumbnailing.yml.
Modified:
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageRoundedCornersGD.class.php
===================================================================
---
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageRoundedCornersGD.class.php
2010-03-25 10:22:21 UTC (rev 28781)
+++
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageRoundedCornersGD.class.php
2010-03-25 10:51:43 UTC (rev 28782)
@@ -18,7 +18,7 @@
* @subpackage transforms
* @author Christian Schaefer <[email protected]>
*/
-class sfImageRoundedCornersGD extends sfImageTransformAbstract
+class sfImageRoundedCornersGD extends sfImageAlphaMaskGD
{
public function __construct($radius, $color = false)
{
@@ -28,82 +28,15 @@
protected function transform(sfImage $image)
{
- $this->image = $image;
- $this->mimeType = $image->getMIMEType();
$resource = $image->getAdapter()->getHolder();
-
- switch ($this->mimeType)
- {
- case 'image/png':
- $this->transformAlpha($resource);
- break;
- case 'image/gif':
- case 'image/jpg':
- default:
- $this->transformDefault($resource);
- }
-
- return $image;
- }
-
- private function transformAlpha($resource)
- {
+
$w = imagesx($resource);
$h = imagesy($resource);
+ $this->mask = $this->getMask($w, $h);
- $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++)
- {
- 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);
- }
- }
- imagealphablending($resource, false);
- imagesavealpha($resource, true);
- imagecopy($resource, $canvas, 0, 0, 0, 0, $w, $h);
-
- imagedestroy($mask);
- imagedestroy($canvas);
+ return parent::transform($image);
}
- private function transformDefault($resource)
- {
- $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);
- }
-
private function getMask($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
Modified: plugins/sfImageTransformExtraPlugin/trunk/package.xml.tmpl
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/package.xml.tmpl 2010-03-25
10:22:21 UTC (rev 28781)
+++ plugins/sfImageTransformExtraPlugin/trunk/package.xml.tmpl 2010-03-25
10:51:43 UTC (rev 28782)
@@ -63,6 +63,7 @@
<notes>
* caefer: sf_cache_namespace_callable now set only for
sfImageTranformator module and moved from settings.yml to
sfImageTransformExtraPluginConfiguration
* caefer: moved setCacheKey callback from
sfImageTransformExtraPluginConfiguration to sfRawFileCache
+ * caefer: refactored sfImageRoundedCornersGD transformation to extend
sfImageAlphaMaskGD transformation as it is only a specialisation of it.
</notes>
</release>
<release>
--
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.