aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Image/Gd/Thumbnail.php
diff options
context:
space:
mode:
Diffstat (limited to 'h-source/Library/Image/Gd/Thumbnail.php')
-rw-r--r--h-source/Library/Image/Gd/Thumbnail.php246
1 files changed, 56 insertions, 190 deletions
diff --git a/h-source/Library/Image/Gd/Thumbnail.php b/h-source/Library/Image/Gd/Thumbnail.php
index bb69891..22e501e 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 - 2014 Antonio Gallo (info@laboratoriolibero.com)
+// Copyright (C) 2009 - 2011 Antonio Gallo
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
@@ -47,10 +47,9 @@ class Image_Gd_Thumbnail
'resample' => 'yes',
'function' => 'none',
'outputFormat' => 'jpeg',
- 'backgroundColor' => null, //must be hex color
);
- //set the $this->params array
+ //set the $this->scaffold->params array
if (is_array($params))
{
foreach ($params as $key => $value)
@@ -61,46 +60,6 @@ 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
@@ -116,8 +75,7 @@ class Image_Gd_Thumbnail
if (file_exists($imagePath))
{
- $extArray = explode('.', $imagePath);
- $ext = strtolower(end($extArray));
+ $ext = strtolower(end(explode('.', $imagePath)));
if (strcmp($ext,'jpg') === 0 or strcmp($ext,'jpeg') === 0) {
$img = @imagecreatefromjpeg($imagePath);
@@ -153,172 +111,81 @@ class Image_Gd_Thumbnail
$scale = max($this->params['imgWidth']/$width, $this->params['imgHeight']/$height);
}
- $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)
+ if ($scale < 1) {
+
+ $xSrc = 0;
+ $ySrc = 0;
+
+ if ($this->params['cropImage'] === 'no')
{
- $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
- {
- $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);
- }
+ $newWidth = floor($scale*$width);
+ $newHeight = floor($scale*$height);
}
- }
- else if ($this->params['cropImage'] === 'yes')
- {
- if ($scale < 1)
+ else if ($this->params['cropImage'] === 'yes')
{
- $newWidth = $backWidth = $this->params['imgWidth'];
- $newHeight = $backHeight = $this->params['imgHeight'];
+
+ $newWidth = $this->params['imgWidth'];
+ $newHeight = $this->params['imgHeight'];
$oldWidth = $width;
$oldHeight = $height;
$width = floor($newWidth/$scale);
$height = floor($newHeight/$scale);
- $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'])
+ switch ($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);
- }
-
- }
- else if ($width <= $this->params['imgWidth'])
- {
- $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);
- }
-
+ 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'];
}
- else if ($height <= $this->params['imgHeight'])
+
+ switch ($this->params['vertAlign'])
{
- $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);
- }
-
+ 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'];
}
- }
- }
- //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)
- {
- $rgbColor = hex2rgb($this->params['backgroundColor']);
-
- $backgroundC = imagecolorallocate($tmpImg,$rgbColor[0],$rgbColor[1],$rgbColor[2]);
- }
- else
- {
- $backgroundC = imagecolortransparent($tmpImg);
}
-
- imagefill($tmpImg, 0, 0, $backgroundC);
- }
-
- if(strcmp($type,'png') === 0 or strcmp($type,'gif') === 0){
-
- if ($this->params['backgroundColor'])
+
+ //temp image
+ $tmpImg = imagecreatetruecolor($newWidth, $newHeight);
+
+ if ($this->params['resample'] === 'yes')
{
- imagealphablending($tmpImg, true);
+ //copy and resample
+ imagecopyresampled($tmpImg, $img, 0, 0, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
}
else
{
- imagealphablending($tmpImg, false);
+ //copy and resize
+ imagecopyresized($tmpImg, $img, 0, 0, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
}
-
- imagesavealpha($tmpImg, true);
- }
+ imagedestroy($img);
+ $img = $tmpImg;
- if ($this->params['resample'] === 'yes')
- {
- //copy and resample
- imagecopyresampled($tmpImg, $img, $xDst, $yDst, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
- }
- else
- {
- //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');
+ }
- 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);
}
-
- $img = call_user_func($this->params['function'],$img);
+
}
if (!$img)
@@ -328,7 +195,6 @@ class Image_Gd_Thumbnail
$img = imagecreate($imgWidth, $imgHeight);
imagecolorallocate($img,200,200,200);
-
}
//print the image