Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* CImageHandler: Image reloading from the memory instead of the disk

* CImageHandler: Transparance saving for non-processed image
* CImageHandler: Fixed incorrect resizing in resizeCanvas method
* CImageHandler: Added greyscale method
* CImageHandler: Added testing page
  • Loading branch information...
commit f0b39b2b564c2ccc6f9227d3396d535877ce52d1 1 parent 69119bd
tokolist@gmail.com authored
View
BIN  images/image_handler/paypal_512.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/image_handler/wm361812tt.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/image_handler/wm370124tt.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2  index.php
@@ -1,7 +1,7 @@
<?php
// change the following paths if necessary
-$yii=dirname(__FILE__).'/../framework/yii.php';
+$yii=dirname(__FILE__).'/../../yiiframework/yii-1.1.10.r3566/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
View
5 nbproject/project.properties
@@ -1,4 +1,7 @@
-include.path=${php.global.include.path}
+file.reference.yii-1.1.10.r3566-framework=../../httpd/home/yiiframework/yii-1.1.10.r3566/framework
+include.path=\
+ ${php.global.include.path}:\
+ ${file.reference.yii-1.1.10.r3566-framework}
php.version=PHP_5
source.encoding=UTF-8
src.dir=.
View
6 protected/config/main.php
@@ -32,6 +32,10 @@
// application components
'components'=>array(
+ 'ih'=>array(
+ 'class'=>'ext.yiicomp.components.ImageHandler.CImageHandler',
+ ),
+
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
@@ -62,7 +66,7 @@
*/
'errorHandler'=>array(
// use 'site/error' action to display errors
- 'errorAction'=>'site/error',
+ //'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
View
48 protected/controllers/ImageHandlerController.php
@@ -0,0 +1,48 @@
+<?php
+
+class ImageHandlerController extends Controller
+{
+ private $basePath;
+
+ public function init() {
+ $this->basePath = Yii::app()->basePath . '/../images/image_handler/';
+ parent::init();
+ }
+
+ public function actionResize1()
+ {
+ Yii::app()->ih->load($this->basePath . 'wm361812tt.jpg')
+ ->resizeCanvas(100,100,array(0, 255, 0))
+ ->show();
+ }
+
+ public function actionResize2()
+ {
+ Yii::app()->ih->load($this->basePath . 'wm370124tt.jpg')
+ ->resizeCanvas(100,100,array(0, 255, 0))
+ ->show();
+ }
+
+ public function actionGrayscale()
+ {
+ Yii::app()->ih->load($this->basePath . 'wm370124tt.jpg')
+ ->grayscale()
+ ->show();
+ }
+
+ public function actionPngAlpha()
+ {
+ Yii::app()->ih->load($this->basePath . 'paypal_512.png')
+ ->thumb(100,100)
+ ->show();
+ }
+
+ public function actionFillBg()
+ {
+ Yii::app()->ih->load($this->basePath . 'paypal_512.png')
+ ->resizeCanvas(Yii::app()->ih->width,Yii::app()->ih->height,array(0, 255, 0))
+ ->show();
+ }
+
+
+}
View
25 protected/controllers/SiteController.php
@@ -31,6 +31,31 @@ public function actionIndex()
// using the default layout 'protected/views/layouts/main.php'
$this->render('index');
}
+
+ public function actionImageHandler()
+ {
+ $basePath = Yii::app()->basePath . '/../images/image_handler/';
+ $reloadSavePath = $basePath . 'reload_test/';
+
+ Yii::app()->ih->load($basePath . 'wm361812tt.jpg')
+ ->thumb(50,50)
+ ->save($reloadSavePath . 'img_thumb50x50.jpg', CImageHandler::IMG_JPEG)
+
+ ->reload()
+ ->rotate(180)
+ ->thumb(100,100)
+ ->save($reloadSavePath . 'img_thumb100x100_rotate.jpg', CImageHandler::IMG_JPEG)
+
+ ->reload()
+ ->resizeCanvas(100,100,array(255,0,0))
+ ->save($reloadSavePath . 'img_thumb100x100_bg.jpg', CImageHandler::IMG_JPEG)
+
+ ->reload()
+ ->thumb(100,100)
+ ->save($reloadSavePath . 'img_thumb100x100.jpg', CImageHandler::IMG_JPEG);
+
+ $this->render('imageHandler');
+ }
/**
* This is the action to handle external exceptions.
View
784 protected/extensions/yiicomp/components/ImageHandler/CImageHandler.php
@@ -3,13 +3,13 @@
* Image handler
* @author Pelesh Yaroslav aka Tokolist http://tokolist.com
* @link http://code.google.com/p/yii-components/
- * @version 0.9 beta
+ * @version 1.0
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class CImageHandler extends CApplicationComponent
{
-
+ private $originalImage = null;
private $image = null;
private $format = 0;
@@ -42,139 +42,161 @@ class CImageHandler extends CApplicationComponent
public function getImage()
{
- return $this->image;
+ return $this->image;
}
public function getFormat()
{
- return $this->format;
+ return $this->format;
}
public function getWidth()
{
- return $this->width;
+ return $this->width;
}
public function getHeight()
{
- return $this->height;
+ return $this->height;
}
public function getMimeType()
{
- return $this->mimeType;
+ return $this->mimeType;
}
public function __destruct()
{
- $this->freeImage();
+ $this->freeImage();
}
private function freeImage()
{
- if (is_resource($this->image))
+ if(is_resource($this->image))
{
imagedestroy($this->image);
}
+
+ if($this->originalImage !== null)
+ {
+ if(is_resource($this->originalImage['image']))
+ {
+ imagedestroy($this->originalImage['image']);
+ }
+ $this->originalImage = null;
+ }
}
private function checkLoaded()
{
- if (!is_resource($this->image))
- {
- throw new Exception('Load image first');
- }
+ if (!is_resource($this->image))
+ {
+ throw new Exception('Load image first');
+ }
}
private function loadImage($file)
{
- $result = array();
-
-
-
- if ($imageInfo = @getimagesize($file))
- {
- $result['width'] = $imageInfo[0];
- $result['height'] = $imageInfo[1];
-
- $result['mimeType'] = $imageInfo['mime'];
-
- switch ($result['format'] = $imageInfo[2])
- {
- case self::IMG_GIF:
- if ($result['image'] = imagecreatefromgif($file))
- {
- return $result;
- }
- else
- {
- throw new Exception('Invalid image gif format');
- }
- break;
- case self::IMG_JPEG:
- if ($result['image'] = imagecreatefromjpeg($file))
- {
- return $result;
- }
- else
- {
- throw new Exception('Invalid image jpeg format');
- }
- break;
- case self::IMG_PNG:
- if ($result['image'] = imagecreatefrompng($file))
- {
- return $result;
- }
- else
- {
- throw new Exception('Invalid image png format');
- }
- break;
- default:
- throw new Exception('Not supported image format');
- }
- }
- else
- {
- throw new Exception('Invalid image file');
- }
+ $result = array();
- }
- public function load($file)
- {
- $this->freeImage();
+ if ($imageInfo = @getimagesize($file))
+ {
+ $result['width'] = $imageInfo[0];
+ $result['height'] = $imageInfo[1];
- if ($img = $this->loadImage($file))
- {
- $this->width = $img['width'];
- $this->height = $img['height'];
- $this->mimeType = $img['mimeType'];
- $this->format = $img['format'];
- $this->image = $img['image'];
+ $result['mimeType'] = $imageInfo['mime'];
- $this->fileName = $file;
+ switch ($result['format'] = $imageInfo[2])
+ {
+ case self::IMG_GIF:
+ if ($result['image'] = imagecreatefromgif($file))
+ {
+ return $result;
+ }
+ else
+ {
+ throw new Exception('Invalid image gif format');
+ }
+ break;
+ case self::IMG_JPEG:
+ if ($result['image'] = imagecreatefromjpeg($file))
+ {
+ return $result;
+ }
+ else
+ {
+ throw new Exception('Invalid image jpeg format');
+ }
+ break;
+ case self::IMG_PNG:
+ if ($result['image'] = imagecreatefrompng($file))
+ {
+ return $result;
+ }
+ else
+ {
+ throw new Exception('Invalid image png format');
+ }
+ break;
+ default:
+ throw new Exception('Not supported image format');
+ }
+ }
+ else
+ {
+ throw new Exception('Invalid image file');
+ }
- return $this;
- }
- else
- {
- return false;
- }
+ }
+
+ protected function initImage($image = false)
+ {
+ if($image === false)
+ {
+ $image = $this->originalImage;
+ }
+
+ $this->width = $image['width'];
+ $this->height = $image['height'];
+ $this->mimeType = $image['mimeType'];
+ $this->format = $image['format'];
+
+ //Image
+ if(is_resource($this->image))
+ imagedestroy($this->image);
+
+ $this->image = imagecreatetruecolor($this->width, $this->height);
+ $this->preserveTransparency($this->image);
+ imagecopy($this->image, $image['image'], 0, 0, 0, 0, $this->width, $this->height);
}
- public function reload()
+ public function load($file)
{
- $this->checkLoaded();
+ $this->freeImage();
+
+ if(($this->originalImage = $this->loadImage($file)))
+ {
+ $this->initImage();
+ $this->fileName = $file;
+
- if ($this->fileName != '')
+ return $this;
+ }
+ else
{
- return $this->load($this->fileName);
+ return false;
}
+ }
+ public function reload()
+ {
+ $this->checkLoaded();
+ $this->initImage();
+
+ return $this;
}
private function preserveTransparency($newImage)
@@ -182,43 +204,43 @@ private function preserveTransparency($newImage)
switch($this->format)
{
case self::IMG_GIF:
- $color = imagecolorallocate(
- $newImage,
- $this->transparencyColor[0],
- $this->transparencyColor[1],
- $this->transparencyColor[2]
- );
-
- imagecolortransparent($newImage, $color);
- imagetruecolortopalette($newImage, false, 256);
+ $color = imagecolorallocate(
+ $newImage,
+ $this->transparencyColor[0],
+ $this->transparencyColor[1],
+ $this->transparencyColor[2]
+ );
+
+ imagecolortransparent($newImage, $color);
+ imagetruecolortopalette($newImage, false, 256);
break;
- case self::IMG_PNG:
+ case self::IMG_PNG:
imagealphablending($newImage, false);
- $color = imagecolorallocatealpha (
- $newImage,
- $this->transparencyColor[0],
- $this->transparencyColor[1],
- $this->transparencyColor[2],
- 0
+ $color = imagecolorallocatealpha (
+ $newImage,
+ $this->transparencyColor[0],
+ $this->transparencyColor[1],
+ $this->transparencyColor[2],
+ 0
);
-
- imagefill($newImage, 0, 0, $color);
+
+ imagefill($newImage, 0, 0, $color);
imagesavealpha($newImage, true);
- break;
- }
+ break;
+ }
}
- public function resize($toWidth, $toHeight, $proportional = true)
- {
- $this->checkLoaded();
+ public function resize($toWidth, $toHeight, $proportional = true)
+ {
+ $this->checkLoaded();
$toWidth = $toWidth !== false ? $toWidth : $this->width;
$toHeight = $toHeight !== false ? $toHeight : $this->height;
- if($proportional)
- {
- $newHeight = $toHeight;
+ if($proportional)
+ {
+ $newHeight = $toHeight;
$newWidth = round($newHeight / $this->height * $this->width);
@@ -227,34 +249,34 @@ public function resize($toWidth, $toHeight, $proportional = true)
$newWidth = $toWidth;
$newHeight = round($newWidth / $this->width * $this->height);
}
- }
+ }
else
{
$newWidth = $toWidth;
$newHeight = $toHeight;
}
- $newImage = imagecreatetruecolor($newWidth, $newHeight);
+ $newImage = imagecreatetruecolor($newWidth, $newHeight);
- $this->preserveTransparency($newImage);
+ $this->preserveTransparency($newImage);
- imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
+ imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
- imagedestroy($this->image);
+ imagedestroy($this->image);
- $this->image = $newImage;
- $this->width = $newWidth;
- $this->height = $newHeight;
+ $this->image = $newImage;
+ $this->width = $newWidth;
+ $this->height = $newHeight;
- return $this;
+ return $this;
- }
+ }
- public function thumb($toWidth, $toHeight, $proportional = true)
- {
- $this->checkLoaded();
+ public function thumb($toWidth, $toHeight, $proportional = true)
+ {
+ $this->checkLoaded();
if($toWidth !== false)
$toWidth = min($toWidth, $this->width);
@@ -265,132 +287,132 @@ public function thumb($toWidth, $toHeight, $proportional = true)
$this->resize($toWidth, $toHeight, $proportional);
- return $this;
-
- }
-
- public function watermark($watermarkFile, $offsetX, $offsetY, $corner = self::CORNER_RIGHT_BOTTOM)
- {
-
- $this->checkLoaded();
-
- if ($wImg = $this->loadImage($watermarkFile))
- {
-
- $posX = 0;
- $posY = 0;
-
- switch ($corner)
- {
- case self::CORNER_LEFT_TOP:
- $posX = $offsetX;
- $posY = $offsetY;
- break;
- case self::CORNER_RIGHT_TOP:
- $posX = $this->width - $wImg['width'] - $offsetX;
- $posY = $offsetY;
- break;
- case self::CORNER_LEFT_BOTTOM:
- $posX = $offsetX;
- $posY = $this->height - $wImg['height'] - $offsetY;
- break;
- case self::CORNER_RIGHT_BOTTOM:
- $posX = $this->width - $wImg['width'] - $offsetX;
- $posY = $this->height - $wImg['height'] - $offsetY;
- break;
- case self::CORNER_CENTER:
- $posX = floor(($this->width - $wImg['width']) / 2);
- $posY = floor(($this->height - $wImg['height']) / 2);
- break;
- default:
- throw new Exception('Invalid $corner value');
- }
-
- imagecopy($this->image, $wImg['image'], $posX, $posY, 0, 0, $wImg['width'], $wImg['height']);
-
-
- imagedestroy($wImg['image']);
-
- return $this;
- }
- else
- {
- return false;
- }
- }
-
-
- public function flip($mode)
- {
- $this->checkLoaded();
-
- $srcX = 0;
- $srcY = 0;
- $srcWidth = $this->width;
- $srcHeight = $this->height;
-
- switch ($mode)
- {
- case self::FLIP_HORIZONTAL:
- $srcX = $this->width - 1;
- $srcWidth = -$this->width;
- break;
- case self::FLIP_VERTICAL:
- $srcY = $this->height - 1;
- $srcHeight = -$this->height;
- break;
- case self::FLIP_BOTH:
- $srcX = $this->width - 1;
- $srcY = $this->height - 1;
- $srcWidth = -$this->width;
- $srcHeight = -$this->height;
- break;
- default:
- throw new Exception('Invalid $mode value');
- }
-
- $newImage = imagecreatetruecolor($this->width, $this->height);
- $this->preserveTransparency($newImage);
-
- imagecopyresampled($newImage, $this->image, 0, 0, $srcX, $srcY, $this->width, $this->height, $srcWidth, $srcHeight);
-
- imagedestroy($this->image);
-
- $this->image = $newImage;
- //dimensions not changed
+ return $this;
+
+ }
+
+ public function watermark($watermarkFile, $offsetX, $offsetY, $corner = self::CORNER_RIGHT_BOTTOM)
+ {
+
+ $this->checkLoaded();
+
+ if ($wImg = $this->loadImage($watermarkFile))
+ {
+
+ $posX = 0;
+ $posY = 0;
+
+ switch ($corner)
+ {
+ case self::CORNER_LEFT_TOP:
+ $posX = $offsetX;
+ $posY = $offsetY;
+ break;
+ case self::CORNER_RIGHT_TOP:
+ $posX = $this->width - $wImg['width'] - $offsetX;
+ $posY = $offsetY;
+ break;
+ case self::CORNER_LEFT_BOTTOM:
+ $posX = $offsetX;
+ $posY = $this->height - $wImg['height'] - $offsetY;
+ break;
+ case self::CORNER_RIGHT_BOTTOM:
+ $posX = $this->width - $wImg['width'] - $offsetX;
+ $posY = $this->height - $wImg['height'] - $offsetY;
+ break;
+ case self::CORNER_CENTER:
+ $posX = floor(($this->width - $wImg['width']) / 2);
+ $posY = floor(($this->height - $wImg['height']) / 2);
+ break;
+ default:
+ throw new Exception('Invalid $corner value');
+ }
+
+ imagecopy($this->image, $wImg['image'], $posX, $posY, 0, 0, $wImg['width'], $wImg['height']);
+
+
+ imagedestroy($wImg['image']);
+
+ return $this;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+
+ public function flip($mode)
+ {
+ $this->checkLoaded();
+
+ $srcX = 0;
+ $srcY = 0;
+ $srcWidth = $this->width;
+ $srcHeight = $this->height;
+
+ switch ($mode)
+ {
+ case self::FLIP_HORIZONTAL:
+ $srcX = $this->width - 1;
+ $srcWidth = -$this->width;
+ break;
+ case self::FLIP_VERTICAL:
+ $srcY = $this->height - 1;
+ $srcHeight = -$this->height;
+ break;
+ case self::FLIP_BOTH:
+ $srcX = $this->width - 1;
+ $srcY = $this->height - 1;
+ $srcWidth = -$this->width;
+ $srcHeight = -$this->height;
+ break;
+ default:
+ throw new Exception('Invalid $mode value');
+ }
+
+ $newImage = imagecreatetruecolor($this->width, $this->height);
+ $this->preserveTransparency($newImage);
+
+ imagecopyresampled($newImage, $this->image, 0, 0, $srcX, $srcY, $this->width, $this->height, $srcWidth, $srcHeight);
+
+ imagedestroy($this->image);
+
+ $this->image = $newImage;
+ //dimensions not changed
return $this;
- }
+ }
- public function rotate($degrees)
- {
- $this->checkLoaded();
+ public function rotate($degrees)
+ {
+ $this->checkLoaded();
- $degrees = (int) $degrees;
- $this->image = imagerotate($this->image, $degrees, 0);
+ $degrees = (int) $degrees;
+ $this->image = imagerotate($this->image, $degrees, 0);
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
+ $this->width = imagesx($this->image);
+ $this->height = imagesy($this->image);
- return $this;
- }
+ return $this;
+ }
- public function crop($width, $height, $startX = false, $startY = false)
- {
- $this->checkLoaded();
+ public function crop($width, $height, $startX = false, $startY = false)
+ {
+ $this->checkLoaded();
- $width = (int) $width;
- $height = (int) $height;
+ $width = (int) $width;
+ $height = (int) $height;
//Centered crop
- $startX = $startX === false ? floor(($this->width - $width) / 2) : intval($startX);
- $startY = $startY === false ? floor(($this->height - $height) / 2) : intval($startY);
+ $startX = $startX === false ? floor(($this->width - $width) / 2) : intval($startX);
+ $startY = $startY === false ? floor(($this->height - $height) / 2) : intval($startY);
- //Check dimensions
- $startX = max(0, min($this->width, $startX));
- $startY = max(0, min($this->height, $startY));
- $width = min($width, $this->width - $startX);
- $height = min($height, $this->height - $startY);
+ //Check dimensions
+ $startX = max(0, min($this->width, $startX));
+ $startY = max(0, min($this->height, $startY));
+ $width = min($width, $this->width - $startX);
+ $height = min($height, $this->height - $startY);
$newImage = imagecreatetruecolor($width, $height);
@@ -399,64 +421,64 @@ public function crop($width, $height, $startX = false, $startY = false)
imagecopyresampled($newImage, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height);
- imagedestroy($this->image);
+ imagedestroy($this->image);
- $this->image = $newImage;
- $this->width = $width;
- $this->height = $height;
+ $this->image = $newImage;
+ $this->width = $width;
+ $this->height = $height;
return $this;
- }
+ }
- public function text($text, $fontFile, $size=12, $color=array(0, 0, 0),
- $corner=self::CORNER_LEFT_TOP, $offsetX=0, $offsetY=0, $angle=0)
- {
- $this->checkLoaded();
+ public function text($text, $fontFile, $size=12, $color=array(0, 0, 0),
+ $corner=self::CORNER_LEFT_TOP, $offsetX=0, $offsetY=0, $angle=0)
+ {
+ $this->checkLoaded();
- $bBox = imagettfbbox($size, $angle, $fontFile, $text);
- $textHeight = $bBox[1] - $bBox[7];
- $textWidth = $bBox[2] - $bBox[0];
+ $bBox = imagettfbbox($size, $angle, $fontFile, $text);
+ $textHeight = $bBox[1] - $bBox[7];
+ $textWidth = $bBox[2] - $bBox[0];
- $color = imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
+ $color = imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
switch ($corner)
{
- case self::CORNER_LEFT_TOP:
- $posX = $offsetX;
- $posY = $offsetY;
- break;
- case self::CORNER_RIGHT_TOP:
- $posX = $this->width - $textWidth - $offsetX;
- $posY = $offsetY;
- break;
- case self::CORNER_LEFT_BOTTOM:
- $posX = $offsetX;
- $posY = $this->height - $textHeight - $offsetY;
- break;
- case self::CORNER_RIGHT_BOTTOM:
- $posX = $this->width - $textWidth - $offsetX;
- $posY = $this->height - $textHeight - $offsetY;
- break;
- case self::CORNER_CENTER:
- $posX = floor(($this->width - $textWidth) / 2);
- $posY = floor(($this->height - $textHeight) / 2);
- break;
- default:
- throw new Exception('Invalid $corner value');
+ case self::CORNER_LEFT_TOP:
+ $posX = $offsetX;
+ $posY = $offsetY;
+ break;
+ case self::CORNER_RIGHT_TOP:
+ $posX = $this->width - $textWidth - $offsetX;
+ $posY = $offsetY;
+ break;
+ case self::CORNER_LEFT_BOTTOM:
+ $posX = $offsetX;
+ $posY = $this->height - $textHeight - $offsetY;
+ break;
+ case self::CORNER_RIGHT_BOTTOM:
+ $posX = $this->width - $textWidth - $offsetX;
+ $posY = $this->height - $textHeight - $offsetY;
+ break;
+ case self::CORNER_CENTER:
+ $posX = floor(($this->width - $textWidth) / 2);
+ $posY = floor(($this->height - $textHeight) / 2);
+ break;
+ default:
+ throw new Exception('Invalid $corner value');
}
- imagettftext($this->image, $size, $angle, $posX, $posY + $textHeight, $color, $fontFile, $text);
+ imagettftext($this->image, $size, $angle, $posX, $posY + $textHeight, $color, $fontFile, $text);
- return $this;
- }
+ return $this;
+ }
public function adaptiveThumb($width, $height)
{
- $this->checkLoaded();
+ $this->checkLoaded();
$width = intval($width);
$height = intval($height);
@@ -484,114 +506,132 @@ public function adaptiveThumb($width, $height)
public function resizeCanvas($toWidth, $toHeight, $backgroundColor = array(255, 255, 255))
{
- $this->checkLoaded();
+ $this->checkLoaded();
$newWidth = min($toWidth, $this->width);
- $newHeight = min($toHeight, $this->height);
+ $newHeight = min($toHeight, $this->height);
- if ($this->width > $this->height)
- {
- $newHeight = round($newWidth / $this->width * $this->height);
- }
- else
- {
- $newWidth = round($newHeight / $this->height * $this->width);
- }
+ $widthProportion = $newWidth / $this->width;
+ $heightProportion = $newHeight / $this->height;
- $posX = floor(($toWidth - $newWidth) / 2);
- $posY = floor(($toHeight - $newHeight) / 2);
+ if($widthProportion < $heightProportion)
+ {
+ $newHeight = round($widthProportion * $this->height);
+ }
+ else
+ {
+ $newWidth = round($heightProportion * $this->width);
+ }
+
+ $posX = floor(($toWidth - $newWidth) / 2);
+ $posY = floor(($toHeight - $newHeight) / 2);
$newImage = imagecreatetruecolor($toWidth, $toHeight);
- $backgroundColor = imagecolorallocate($newImage, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]);
- imagefill($newImage, 0, 0, $backgroundColor);
+ $backgroundColor = imagecolorallocate($newImage, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]);
+ imagefill($newImage, 0, 0, $backgroundColor);
imagecopyresampled($newImage, $this->image, $posX, $posY, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
- imagedestroy($this->image);
+ imagedestroy($this->image);
- $this->image = $newImage;
- $this->width = $toWidth;
- $this->height = $toHeight;
+ $this->image = $newImage;
+ $this->width = $toWidth;
+ $this->height = $toHeight;
return $this;
}
+
+ public function grayscale()
+ {
+ //$newImage=$this->createImage($this->width, $this->height, $this->trueColor);
+ $newImage = imagecreatetruecolor($this->width, $this->height);
+
+ imagecopy($newImage, $this->image, 0, 0, 0, 0, $this->width, $this->height);
+ imagecopymergegray($newImage, $newImage, 0, 0, 0, 0, $this->width, $this->height, 0);
- public function show($inFormat = false, $jpegQuality = 75)
- {
- $this->checkLoaded();
-
- if (!$inFormat)
- {
- $inFormat = $this->format;
- }
-
- switch ($inFormat)
- {
- case self::IMG_GIF:
- header('Content-type: image/gif');
- imagegif($this->image);
- break;
- case self::IMG_JPEG:
- header('Content-type: image/jpeg');
- imagejpeg($this->image, null, $jpegQuality);
- break;
- case self::IMG_PNG:
- header('Content-type: image/png');
- imagepng($this->image);
- break;
- default:
- throw new Exception('Invalid image format for putput');
- }
-
- return $this;
- }
-
- public function save($file = false, $toFormat = false, $jpegQuality = 75, $touch = false)
- {
- if (empty($file))
- {
- $file = $this->fileName;
- }
-
- $this->checkLoaded();
-
- if (!$toFormat)
- {
- $toFormat = $this->format;
- }
-
- switch ($toFormat)
- {
- case self::IMG_GIF:
- if (!imagegif($this->image, $file))
- {
- throw new Exception('Can\'t save gif file');
- }
- break;
- case self::IMG_JPEG:
- if (!imagejpeg($this->image, $file, $jpegQuality))
- {
- throw new Exception('Can\'t save jpeg file');
- }
- break;
- case self::IMG_PNG:
- if (!imagepng($this->image, $file))
- {
- throw new Exception('Can\'t save png file');
- }
- break;
- default:
- throw new Exception('Invalid image format for save');
- }
-
- if ($touch && $file != $this->fileName)
- {
- touch($file, filemtime($this->fileName));
- }
-
- return $this;
- }
+ imagedestroy($this->image);
+
+ $this->image = $newImage;
+
+ return $this;
+ }
+
+ public function show($inFormat = false, $jpegQuality = 75)
+ {
+ $this->checkLoaded();
+
+ if (!$inFormat)
+ {
+ $inFormat = $this->format;
+ }
+
+ switch ($inFormat)
+ {
+ case self::IMG_GIF:
+ header('Content-type: image/gif');
+ imagegif($this->image);
+ break;
+ case self::IMG_JPEG:
+ header('Content-type: image/jpeg');
+ imagejpeg($this->image, null, $jpegQuality);
+ break;
+ case self::IMG_PNG:
+ header('Content-type: image/png');
+ imagepng($this->image);
+ break;
+ default:
+ throw new Exception('Invalid image format for putput');
+ }
+
+ return $this;
+ }
+
+ public function save($file = false, $toFormat = false, $jpegQuality = 75, $touch = false)
+ {
+ if (empty($file))
+ {
+ $file = $this->fileName;
+ }
+
+ $this->checkLoaded();
+
+ if (!$toFormat)
+ {
+ $toFormat = $this->format;
+ }
+
+ switch ($toFormat)
+ {
+ case self::IMG_GIF:
+ if (!imagegif($this->image, $file))
+ {
+ throw new Exception('Can\'t save gif file');
+ }
+ break;
+ case self::IMG_JPEG:
+ if (!imagejpeg($this->image, $file, $jpegQuality))
+ {
+ throw new Exception('Can\'t save jpeg file');
+ }
+ break;
+ case self::IMG_PNG:
+ if (!imagepng($this->image, $file))
+ {
+ throw new Exception('Can\'t save png file');
+ }
+ break;
+ default:
+ throw new Exception('Invalid image format for save');
+ }
+
+ if ($touch && $file != $this->fileName)
+ {
+ touch($file, filemtime($this->fileName));
+ }
+
+ return $this;
+ }
}
View
1  protected/views/layouts/main.php
@@ -29,6 +29,7 @@
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
+ array('label'=>'ImageHandler', 'url'=>array('/site/imageHandler')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
View
45 protected/views/site/imageHandler.php
@@ -0,0 +1,45 @@
+<h1>ImageHandler</h1>
+<h2>resizeCanvas</h2>
+
+<table>
+ <tr>
+ <td><?php echo CHtml::image(Yii::app()->createUrl('imageHandler/resize1')); ?></td>
+ <td><?php echo CHtml::image(Yii::app()->createUrl('imageHandler/resize2')); ?></td>
+ </tr>
+</table>
+
+
+<h2>grayscale</h2>
+
+<table>
+ <tr>
+ <td><?php echo CHtml::image(Yii::app()->createUrl('imageHandler/grayscale')); ?></td>
+ </tr>
+</table>
+
+<h2>reload</h2>
+
+<table>
+ <tr>
+ <td><?php echo CHtml::image('/images/image_handler/reload_test/img_thumb50x50.jpg'); ?></td>
+ <td><?php echo CHtml::image('/images/image_handler/reload_test/img_thumb100x100_rotate.jpg'); ?></td>
+ <td><?php echo CHtml::image('/images/image_handler/reload_test/img_thumb100x100_bg.jpg'); ?></td>
+ <td><?php echo CHtml::image('/images/image_handler/reload_test/img_thumb100x100.jpg'); ?></td>
+ </tr>
+</table>
+
+<h2>PNG with alpha chanel</h2>
+
+<table>
+ <tr>
+ <td><?php echo CHtml::image(Yii::app()->createUrl('imageHandler/pngAlpha')); ?></td>
+ </tr>
+</table>
+
+<h2>Fill background</h2>
+
+<table>
+ <tr>
+ <td><?php echo CHtml::image(Yii::app()->createUrl('imageHandler/fillBg')); ?></td>
+ </tr>
+</table>
Please sign in to comment.
Something went wrong with that request. Please try again.