aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Image/Gd
diff options
context:
space:
mode:
Diffstat (limited to 'h-source/Library/Image/Gd')
-rw-r--r--h-source/Library/Image/Gd/Captcha.php2
-rw-r--r--h-source/Library/Image/Gd/Thumbnail.php247
2 files changed, 185 insertions, 64 deletions
diff --git a/h-source/Library/Image/Gd/Captcha.php b/h-source/Library/Image/Gd/Captcha.php
index 1e8d706..d4285c2 100644
--- a/h-source/Library/Image/Gd/Captcha.php
+++ b/h-source/Library/Image/Gd/Captcha.php
@@ -2,7 +2,7 @@
// EasyGiant is a PHP framework for creating and managing dynamic content
//
-// Copyright (C) 2009 - 2011 Antonio Gallo
+// Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com)
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
diff --git a/h-source/Library/Image/Gd/Thumbnail.php b/h-source/Library/Image/Gd/Thumbnail.php
index 1fd7796..bb69891 100644
--- a/h-source/Library/Image/Gd/Thumbnail.php
+++ b/h-source/Library/Image/Gd/Thumbnail.php
@@ -2,7 +2,7 @@
// EasyGiant is a PHP framework for creating and managing dynamic content
//
-// Copyright (C) 2009 - 2011 Antonio Gallo
+// Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com)
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
@@ -47,9 +47,10 @@ class Image_Gd_Thumbnail
'resample' => 'yes',
'function' => 'none',
'outputFormat' => 'jpeg',
+ 'backgroundColor' => null, //must be hex color
);
- //set the $this->scaffold->params array
+ //set the $this->params array
if (is_array($params))
{
foreach ($params as $key => $value)
@@ -60,6 +61,46 @@ class Image_Gd_Thumbnail
$this->params = $defaultParams;
}
+ public function getSourceCoordinates($direction, $oldDim, $dim)
+ {
+ if ($direction === "x")
+ {
+ switch ($this->params['horizAlign'])
+ {
+ case 'left':
+ $coordSrc = 0;
+ break;
+ case 'right':
+ $coordSrc = floor(($oldDim-$dim));
+ break;
+ case 'center':
+ $coordSrc = floor(($oldDim-$dim)/2);
+ break;
+ default:
+ $coordSrc = $this->params['horizAlign'];
+ }
+ }
+ else
+ {
+ switch ($this->params['vertAlign'])
+ {
+ case 'top':
+ $coordSrc = 0;
+ break;
+ case 'bottom':
+ $coordSrc = floor(($oldDim-$dim));
+ break;
+ case 'center':
+ $coordSrc = floor(($oldDim-$dim)/2);
+ break;
+ default:
+ $coordSrc = $this->params['vertAlign'];
+ }
+ }
+
+ return $coordSrc;
+ }
+
//create the thumbnail
//$imageName: the name of the file inside $this->basePath
//$outputFile: the name of the output file
@@ -112,92 +153,172 @@ class Image_Gd_Thumbnail
$scale = max($this->params['imgWidth']/$width, $this->params['imgHeight']/$height);
}
- if ($scale < 1) {
-
- $xSrc = 0;
- $ySrc = 0;
-
- if ($this->params['cropImage'] === 'no')
+ $xSrc = 0; //x coordinate of source image
+ $ySrc = 0; //y coordinate of source image
+
+ $xDst = 0; //x coordinate of destination image
+ $yDst = 0; //y coordinate of destination image
+
+ if ($this->params['cropImage'] === 'no')
+ {
+ if ($scale <= 1)
{
- $newWidth = floor($scale*$width);
- $newHeight = floor($scale*$height);
+ $newWidth = $backWidth = floor($scale*$width);
+ $newHeight = $backHeight = floor($scale*$height);
+
+ if ($this->params['backgroundColor'])
+ {
+ $backWidth = $this->params['imgWidth'];
+ $backHeight = $this->params['imgHeight'];
+
+ if ($backWidth > $newWidth)
+ {
+ $xDst = floor(($backWidth-$newWidth)/2);
+ }
+ else if ($backHeight > $newHeight)
+ {
+ $yDst = floor(($backHeight-$newHeight)/2);
+ }
+ }
}
- else if ($this->params['cropImage'] === 'yes')
+ else
{
-
- $newWidth = $this->params['imgWidth'];
- $newHeight = $this->params['imgHeight'];
+ $newWidth = $backWidth = $width;
+ $newHeight = $backHeight = $height;
+
+ if ($this->params['backgroundColor'])
+ {
+ $backWidth = $this->params['imgWidth'];
+ $backHeight = $this->params['imgHeight'];
+
+ $xDst = floor(($backWidth-$newWidth)/2);
+ $yDst = floor(($backHeight-$newHeight)/2);
+ }
+ }
+ }
+ else if ($this->params['cropImage'] === 'yes')
+ {
+ if ($scale < 1)
+ {
+ $newWidth = $backWidth = $this->params['imgWidth'];
+ $newHeight = $backHeight = $this->params['imgHeight'];
$oldWidth = $width;
$oldHeight = $height;
$width = floor($newWidth/$scale);
$height = floor($newHeight/$scale);
- switch ($this->params['horizAlign'])
+ $xSrc = $this->getSourceCoordinates("x",$oldWidth,$width);
+ $ySrc = $this->getSourceCoordinates("y",$oldHeight,$height);
+
+ }
+ else
+ {
+ $oldWidth = $width;
+ $oldHeight = $height;
+
+ if ($width <= $this->params['imgWidth'] and $height <= $this->params['imgHeight'])
{
- case 'left':
- $xSrc = 0;
- break;
- case 'right':
- $xSrc = floor(($oldWidth-$width));
- break;
- case 'center':
- $xSrc = floor(($oldWidth-$width)/2);
- break;
- default:
- $xSrc = $this->params['horizAlign'];
+ $newWidth = $backWidth = $width;
+ $newHeight = $backHeight = $height;
+
+ if ($this->params['backgroundColor'])
+ {
+ $backWidth = $this->params['imgWidth'];
+ $backHeight = $this->params['imgHeight'];
+
+ $xDst = floor(($backWidth-$newWidth)/2);
+ $yDst = floor(($backHeight-$newHeight)/2);
+ }
+
}
-
- switch ($this->params['vertAlign'])
+ else if ($width <= $this->params['imgWidth'])
{
- case 'top':
- $ySrc = 0;
- break;
- case 'bottom':
- $ySrc = floor(($oldHeight-$height));
- break;
- case 'center':
- $ySrc = floor(($oldHeight-$height)/2);
- break;
- default:
- $ySrc = $this->params['vertAlign'];
+ $newWidth = $backWidth = $width;
+ $newHeight = $backHeight = $height = $this->params['imgHeight'];
+
+ $ySrc = $this->getSourceCoordinates("y",$oldHeight,$height);
+
+ if ($this->params['backgroundColor'])
+ {
+ $backWidth = $this->params['imgWidth'];
+ $backHeight = $this->params['imgHeight'];
+
+ $xDst = floor(($backWidth-$newWidth)/2);
+ }
+
+ }
+ else if ($height <= $this->params['imgHeight'])
+ {
+ $newHeight = $backHeight = $height;
+ $newWidth = $backWidth = $width = $this->params['imgWidth'];
+
+ $xSrc = $this->getSourceCoordinates("x",$oldWidth,$width);
+
+ if ($this->params['backgroundColor'])
+ {
+ $backWidth = $this->params['imgWidth'];
+ $backHeight = $this->params['imgHeight'];
+
+ $yDst = floor(($backHeight-$newHeight)/2);
+ }
+
}
-
}
+ }
- //temp image
- $tmpImg = imagecreatetruecolor($newWidth, $newHeight);
-
- if(strcmp($type,'png') === 0 or strcmp($type,'gif') === 0){
- imagealphablending($tmpImg, false);
- imagesavealpha($tmpImg, true);
- }
-
- if ($this->params['resample'] === 'yes')
+ //temp image
+ $tmpImg = imagecreatetruecolor($backWidth, $backHeight);
+
+ //set background color if backgroundColor param is not null (hex value)
+ if ($this->params['backgroundColor'])
+ {
+ if (strcmp($this->params['backgroundColor'],"transparent") !== 0)
{
- //copy and resample
- imagecopyresampled($tmpImg, $img, 0, 0, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
+ $rgbColor = hex2rgb($this->params['backgroundColor']);
+
+ $backgroundC = imagecolorallocate($tmpImg,$rgbColor[0],$rgbColor[1],$rgbColor[2]);
}
else
{
- //copy and resize
- imagecopyresized($tmpImg, $img, 0, 0, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
+ $backgroundC = imagecolortransparent($tmpImg);
}
- imagedestroy($img);
- $img = $tmpImg;
-
- if (!function_exists($this->params['function'])) {
- throw new Exception('Error in <b>'.__METHOD__.'</b>: function <b>'.$this->params['function']. '</b> does not exist');
+
+ imagefill($tmpImg, 0, 0, $backgroundC);
+ }
+
+ if(strcmp($type,'png') === 0 or strcmp($type,'gif') === 0){
+
+ if ($this->params['backgroundColor'])
+ {
+ imagealphablending($tmpImg, true);
+ }
+ else
+ {
+ imagealphablending($tmpImg, false);
}
+
+ imagesavealpha($tmpImg, true);
+ }
- $img = call_user_func($this->params['function'],$img);
+ if ($this->params['resample'] === 'yes')
+ {
+ //copy and resample
+ imagecopyresampled($tmpImg, $img, $xDst, $yDst, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
}
else
{
- if(strcmp($type,'png') === 0 or strcmp($type,'gif') === 0){
- imagealphablending($img, false);
- imagesavealpha($img, true);
- }
+ //copy and resize
+ imagecopyresized($tmpImg, $img, $xDst, $yDst, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
}
+ imagedestroy($img);
+ $img = $tmpImg;
+
+ if (!function_exists($this->params['function']))
+ {
+ throw new Exception('Error in <b>'.__METHOD__.'</b>: function <b>'.$this->params['function']. '</b> does not exist');
+ }
+
+ $img = call_user_func($this->params['function'],$img);
}
if (!$img)