Skip to content

Instantly share code, notes, and snippets.

@tobiassjosten
Created December 26, 2010 12:10
Show Gist options
  • Save tobiassjosten/755376 to your computer and use it in GitHub Desktop.
Save tobiassjosten/755376 to your computer and use it in GitHub Desktop.
<?php
/*
* This file is part of the sfImageTransform package.
* (c) 2007 Stuart Lowes <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* sfImageCropGD class.
*
* Crops image.
*
* This class crops a image to a set size.
*
* @package sfImageTransform
* @subpackage transforms
* @author Stuart Lowes <[email protected]>
* @version SVN: $Id$
*/
class sfImageCropGD extends sfImageTransformAbstract
{
/**
* Left coordinate.
*/
protected $left = 0;
/**
* Top coordinate
*/
protected $top = 0;
/**
* Cropped area width.
*/
protected $width;
/**
* Cropped area height
*/
protected $height;
/**
* Construct an sfImageCrop object.
*
* @param integer
* @param integer
* @param integer
* @param integer
*/
public function __construct($left, $top, $width, $height)
{
$this->setLeft($left);
$this->setTop($top);
$this->setWidth($width);
$this->setHeight($height);
}
/**
* Sets the left coordinate
*
* @param integer or string (procentual representation)
*/
public function setLeft($left)
{
if (is_numeric($left))
{
$this->left = (int)$left;
return true;
}
elseif (preg_match('~[0-9]%~', $left))
{
// Limit percentage to between 0 and 100
if ((int)$left < 1)
{
$this->left = 0;
}
else
{
$this->left = min((int)$left, 100).'%';
}
return true;
}
return false;
}
/**
* returns the left coordinate
*
* @return integer
*/
public function getLeft()
{
return $this->left;
}
/**
* set the top coordinate.
*
* @param integer or string (procentual representation)
*/
public function setTop($top)
{
if (is_numeric($top))
{
$this->top = (int)$top;
return true;
}
elseif (preg_match('~[0-9]%~', $top))
{
// Limit percentage to between 0 and 100
if ((int)$top < 1)
{
$this->top = 0;
}
else
{
$this->top = min((int)$top, 100).'%';
}
return true;
}
return false;
}
/**
* returns the top coordinate
*
* @return integer
*/
public function getTop()
{
return $this->top;
}
/**
* set the width.
*
* @param integer
*/
public function setWidth($width)
{
if (is_numeric($width))
{
$this->width = (int)$width;
return true;
}
return false;
}
/**
* returns the width of the thumbnail
*
* @return integer
*/
public function getWidth()
{
return $this->width;
}
/**
* set the height.
*
* @param integer
*/
public function setHeight($height)
{
if (is_numeric($height))
{
$this->height = (int)$height;
return true;
}
return false;
}
/**
* returns the height of the thumbnail
*
* @return integer
*/
public function getHeight()
{
return $this->height;
}
/**
* Apply the transform to the sfImage object.
*
* @access protected
* @param sfImage
* @return sfImage
*/
protected function transform(sfImage $image)
{
$resource = $image->getAdapter()->getHolder();
$dest_resource = $image->getAdapter()->getTransparentImage($this->width, $this->height);
// Preserving transparency for alpha PNGs
imagealphablending($dest_resource, false);
imagesavealpha($dest_resource, true);
// Calculate left/top if they are procentual of the image's width/height
if (strpos($this->left, '%') !== false)
{
$this->left = round((imagesx($resource) - $this->width) / (100 / $this->left));
}
if (strpos($this->top, '%') !== false)
{
$this->top = round((imagesy($resource) - $this->height) / (100 / $this->top));
}
imagecopy($dest_resource, $resource, 0, 0, $this->left, $this->top, $this->width, $this->height);
// Tidy up
imagedestroy($resource);
// Replace old image with flipped version
$image->getAdapter()->setHolder($dest_resource);
return $image;
}
}
diff --git a/lib/transforms/GD/sfImageCropGD.class.php b/lib/transforms/GD/sfImageCropGD.class.php
index 805331d..18c80b3 100644
--- a/lib/transforms/GD/sfImageCropGD.class.php
+++ b/lib/transforms/GD/sfImageCropGD.class.php
@@ -61,7 +61,7 @@ class sfImageCropGD extends sfImageTransformAbstract
/**
* Sets the left coordinate
*
- * @param integer
+ * @param integer or string (procentual representation)
*/
public function setLeft($left)
{
@@ -71,6 +71,20 @@ class sfImageCropGD extends sfImageTransformAbstract
return true;
}
+ elseif (preg_match('~[0-9]%~', $left))
+ {
+ // Limit percentage to between 0 and 100
+ if ((int)$left < 1)
+ {
+ $this->left = 0;
+ }
+ else
+ {
+ $this->left = min((int)$left, 100).'%';
+ }
+
+ return true;
+ }
return false;
}
@@ -88,7 +102,7 @@ class sfImageCropGD extends sfImageTransformAbstract
/**
* set the top coordinate.
*
- * @param integer
+ * @param integer or string (procentual representation)
*/
public function setTop($top)
{
@@ -98,6 +112,20 @@ class sfImageCropGD extends sfImageTransformAbstract
return true;
}
+ elseif (preg_match('~[0-9]%~', $top))
+ {
+ // Limit percentage to between 0 and 100
+ if ((int)$top < 1)
+ {
+ $this->top = 0;
+ }
+ else
+ {
+ $this->top = min((int)$top, 100).'%';
+ }
+
+ return true;
+ }
return false;
}
@@ -183,6 +211,16 @@ class sfImageCropGD extends sfImageTransformAbstract
imagealphablending($dest_resource, false);
imagesavealpha($dest_resource, true);
+ // Calculate left/top if they are procentual of the image's width/height
+ if (strpos($this->left, '%') !== false)
+ {
+ $this->left = round((imagesx($resource) - $this->width) / (100 / $this->left));
+ }
+ if (strpos($this->top, '%') !== false)
+ {
+ $this->top = round((imagesy($resource) - $this->height) / (100 / $this->top));
+ }
+
imagecopy($dest_resource, $resource, 0, 0, $this->left, $this->top, $this->width, $this->height);
// Tidy up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment