[limb-svn] r6322 - in 3.x/trunk/limb/imagekit: . design src src/exception src/gd src/gd/filters tests tests/cases tests/cases/gd tests/cases/gd/filters tests/var
svn at limb-project.com
svn at limb-project.com
Thu Sep 20 19:56:39 MSD 2007
Author: cmz
Date: 2007-09-20 19:56:39 +0400 (Thu, 20 Sep 2007)
New Revision: 6322
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6322
Added:
3.x/trunk/limb/imagekit/design/
3.x/trunk/limb/imagekit/design/image_alt.png
3.x/trunk/limb/imagekit/design/image_alt.zargo
3.x/trunk/limb/imagekit/src/exception/
3.x/trunk/limb/imagekit/src/exception/lmbImageCreateFailedException.class.php
3.x/trunk/limb/imagekit/src/exception/lmbImageSaveFailedException.class.php
3.x/trunk/limb/imagekit/src/exception/lmbImageTypeNotSupportException.class.php
3.x/trunk/limb/imagekit/src/gd/
3.x/trunk/limb/imagekit/src/gd/filters/
3.x/trunk/limb/imagekit/src/gd/filters/lmbGdResizeImageFilter.class.php
3.x/trunk/limb/imagekit/src/gd/filters/lmbGdRotateImageFilter.class.php
3.x/trunk/limb/imagekit/src/gd/filters/lmbGdWaterMarkImageFilter.class.php
3.x/trunk/limb/imagekit/src/gd/lmbGdImageContainer.class.php
3.x/trunk/limb/imagekit/src/gd/lmbGdImageConvertor.class.php
3.x/trunk/limb/imagekit/src/lmbAbstractImageContainer.class.php
3.x/trunk/limb/imagekit/src/lmbAbstractImageConvertor.class.php
3.x/trunk/limb/imagekit/src/lmbAbstractImageFilter.class.php
3.x/trunk/limb/imagekit/src/lmbImageKit.class.php
3.x/trunk/limb/imagekit/tests/cases/gd/filters/
3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdResizeImageFilterTest.class.php
3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdRotateImageFilterTest.class.php
3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdWaterMarkImageFilterTest.class.php
3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageContainerTest.class.php
3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageConvertorTest.class.php
3.x/trunk/limb/imagekit/tests/cases/lmbImageKitTest.class.php
3.x/trunk/limb/imagekit/tests/var/
3.x/trunk/limb/imagekit/tests/var/input.jpg
3.x/trunk/limb/imagekit/tests/var/water_mark.gif
Log:
-- New Image tools with Fluent interface
Added: 3.x/trunk/limb/imagekit/design/image_alt.png
===================================================================
(Binary files differ)
Property changes on: 3.x/trunk/limb/imagekit/design/image_alt.png
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: 3.x/trunk/limb/imagekit/design/image_alt.zargo
===================================================================
(Binary files differ)
Property changes on: 3.x/trunk/limb/imagekit/design/image_alt.zargo
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: 3.x/trunk/limb/imagekit/src/exception/lmbImageCreateFailedException.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/exception/lmbImageCreateFailedException.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/exception/lmbImageCreateFailedException.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,12 @@
+<?php
+
+class lmbImageCreateFailedException extends lmbException
+{
+
+ function __construct($file_name)
+ {
+ parent::__construct('Image create is failed', array('file' => $file_name));
+ }
+
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/exception/lmbImageSaveFailedException.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/exception/lmbImageSaveFailedException.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/exception/lmbImageSaveFailedException.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,12 @@
+<?php
+
+class lmbImageSaveFailedException extends lmbException
+{
+
+ function __construct($file_name)
+ {
+ parent::__construct('Image save is failed', array('file' => $file_name));
+ }
+
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/exception/lmbImageTypeNotSupportException.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/exception/lmbImageTypeNotSupportException.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/exception/lmbImageTypeNotSupportException.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,12 @@
+<?php
+
+class lmbImageTypeNotSupportException extends lmbException
+{
+
+ function __construct($type = '')
+ {
+ parent::__construct('Image type is not support', $type ? array('type' => $type) : array());
+ }
+
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdResizeImageFilter.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdResizeImageFilter.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdResizeImageFilter.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,64 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/../../lmbAbstractImageFilter.class.php');
+
+/**
+ * Resize image filter
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbGdResizeImageFilter extends lmbAbstractImageFilter
+{
+
+ function run(lmbAbstractImageContainer $container)
+ {
+ $src_w = $container->getWidth();
+ $src_h = $container->getHeight();
+ list($dst_w, $dst_h) = $this->calcNewSize($src_w, $src_h);
+ $im = imagecreatetruecolor($dst_w, $dst_h);
+ imagecopyresampled($im, $container->getResource(), 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
+ $container->replaceResource($im);
+ }
+
+ protected function calcNewSize($src_w, $src_h)
+ {
+ $dst_w = $this->getWidth();
+ if(!$dst_w) $dst_w = $src_w;
+ $dst_h = $this->getheight();
+ if(!$dst_h) $dst_h = $src_h;
+ //return array($dst_w, $dst_h);
+ return $this->calcSize($src_w, $src_h, $dst_w, $dst_h, $this->getPreserveAspectRatio(), $this->getSaveMinSize());
+ }
+
+ function getWidth()
+ {
+ return $this->getParam('width', 0);
+ }
+
+ function getHeight()
+ {
+ return $this->getParam('height', 1);
+ }
+
+ function getPreserveAspectRatio()
+ {
+ return $this->getParam('preserve_aspect_ratio', 2, true);
+ }
+
+ function getSaveMinSize()
+ {
+ return $this->getParam('save_min_size', 3, false);
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdRotateImageFilter.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdRotateImageFilter.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdRotateImageFilter.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,46 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/../../lmbAbstractImageFilter.class.php');
+
+/**
+ * Rotate image filter
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbGdRotateImageFilter extends lmbAbstractImageFilter
+{
+
+ function run(lmbAbstractImageContainer $container)
+ {
+ $angle = $this->getAngle();
+ if(!$angle) return;
+ $bgcolor = $this->getBgColor();
+ $cur_im = $container->getResource();
+ $bg = imagecolorallocate($cur_im, $bgcolor['red'], $bgcolor['green'], $bgcolor['blue']);
+ $im = imagerotate($cur_im, $angle, $bg);
+ $container->replaceResource($im);
+ }
+
+ function getAngle()
+ {
+ return $this->getParam('angle', 0);
+ }
+
+ function getBgColor()
+ {
+ $bgcolor = $this->getParam('bgcolor', 1, 'FFFFFF');
+ return $this->parseHexColor($bgcolor);
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdWaterMarkImageFilter.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdWaterMarkImageFilter.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/gd/filters/lmbGdWaterMarkImageFilter.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,61 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/../../lmbAbstractImageFilter.class.php');
+
+/**
+ * Resize image filter
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbGdWaterMarkImageFilter extends lmbAbstractImageFilter
+{
+
+ function run(lmbAbstractImageContainer $container)
+ {
+ $width = $container->getWidth();
+ $height = $container->getHeight();
+ $wm_cont = new lmbGdImageContainer($this->getWaterMark());
+ list($x, $y) = $this->calcPosition($this->getX(), $this->getY(), $width, $height);
+ imagecopymerge($container->getResource(), $wm_cont->getResource(), $x, $y, 0, 0, $wm_cont->getWidth(), $wm_cont->getHeight(), 100 - $this->getOpacity());
+ }
+
+ function calcPosition($x, $y, $width, $height)
+ {
+ if($x >= 0 && $y >= 0) return array($x, $y);
+ if($x < 0) $x += $width;
+ if($y < 0) $y += $height;
+ return array($x, $y);
+ }
+
+ function getWaterMark()
+ {
+ return $this->getParam('water_mark', 0);
+ }
+
+ function getX()
+ {
+ return $this->getParam('x', 1, 0);
+ }
+
+ function getY()
+ {
+ return $this->getParam('y', 2, 0);
+ }
+
+ function getOpacity()
+ {
+ return $this->getParam('opacity', 3, 0);
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/gd/lmbGdImageContainer.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/gd/lmbGdImageContainer.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/gd/lmbGdImageContainer.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,139 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/../lmbAbstractImageContainer.class.php');
+lmb_require(dirname(__FILE__).'/../exception/lmbImageTypeNotSupportException.class.php');
+lmb_require(dirname(__FILE__).'/../exception/lmbImageCreateFailedException.class.php');
+lmb_require(dirname(__FILE__).'/../exception/lmbImageSaveFailedException.class.php');
+lmb_require('limb/fs/src/exception/lmbFileNotFoundException.class.php');
+
+/**
+ * GD image container
+ *
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbGdImageContainer extends lmbAbstractImageContainer
+{
+ protected static $gd_types = array(
+ 'gif' => IMG_GIF,
+ //'jpg' => IMG_JPG,
+ 'jpeg' => IMG_JPG,
+ 'png' => IMG_PNG,
+ 'wbmp' => IMG_WBMP
+ );
+ protected static $lookup_types = array(
+ IMAGETYPE_GIF => 'gif',
+ IMAGETYPE_JPEG => 'jpeg',
+ IMAGETYPE_PNG => 'png',
+ IMAGETYPE_WBMP => 'wbmp'
+ );
+
+ protected $img;
+ protected $img_type;
+ protected $pallete;
+
+ function load($file_name, $type = '')
+ {
+ $imginfo = @getimagesize($file_name);
+ if(!$imginfo) throw new lmbFileNotFoundException($file_name);
+ if(!$type) $type = self::convertImageType($imginfo[2]);
+ if(!self::supportLoadType($type)) throw new lmbImageTypeNotSupportException($type);
+ $createfunc = 'imagecreatefrom'.$type;
+ if(!($this->img = @$createfunc($file_name)))
+ throw new lmbImageCreateFailedException($file_name);
+ $this->img_type = $type;
+ }
+
+ function save($file_name = null, $type = '')
+ {
+ if(!$type) $type = $this->img_type;
+ if(!self::supportSaveType($type)) throw new lmbImageTypeNotSupportException($type);
+ $imagefunc = 'image'.$type;
+ if(!@$imagefunc($this->img, $file_name))
+ throw new lmbImageSaveFailedException($file_name);
+ $this->destroyImage();
+ }
+
+ function getResource()
+ {
+ return $this->img;
+ }
+
+ function replaceResource($img)
+ {
+ imagedestroy($this->img);
+ $this->img = $img;
+ }
+
+ function isPallete()
+ {
+ return !imageistruecolor($this->img);
+ }
+
+ function getWidth()
+ {
+ return imagesx($this->img);
+ }
+
+ function getHeight()
+ {
+ return imagesy($this->img);
+ }
+
+ function destroyImage()
+ {
+ if(!$this->img) return;
+ imagedestroy($this->img);
+ $this->img = null;
+ }
+
+ static function supportLoadType($type)
+ {
+ return self::supportType($type);
+ }
+
+ static function supportSaveType($type)
+ {
+ return self::supportType($type);
+ }
+
+ static function supportType($type)
+ {
+ if(!function_exists('imagetypes')) return false;
+ $gdtype = self::getGdType($type);
+ if($gdtype === false) return false;
+ return (boolean)(imagetypes() & $gdtype);
+ }
+
+ static function getGdType($type)
+ {
+ return isset(self::$gd_types[$type]) ? self::$gd_types[$type] : false;
+ }
+
+ static function convertImageType($imagetype)
+ {
+ if(!isset(self::$lookup_types[$imagetype]))
+ {
+ $type = function_exists('image_type_to_extension') ? image_type_to_extension($imagetype) : '';
+ throw new lmbImageTypeNotSupportException($type);
+ }
+ return self::$lookup_types[$imagetype];
+ }
+
+ function __destruct()
+ {
+ $this->destroyImage();
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/gd/lmbGdImageConvertor.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/gd/lmbGdImageConvertor.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/gd/lmbGdImageConvertor.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,51 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/../lmbAbstractImageConvertor.class.php');
+lmb_require(dirname(__FILE__).'/lmbGdImageContainer.class.php');
+lmb_require('limb/fs/src/exception/lmbFileNotFoundException.class.php');
+
+/**
+ * GD image convertor
+ *
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbGdImageConvertor extends lmbAbstractImageConvertor
+{
+
+ protected function createFilter($name, $params)
+ {
+ $class = $this->loadFilter(dirname(__FILE__).'/filters', $name, 'Gd');
+ return new $class($params);
+ }
+
+ protected function createImageContainer($file_name, $type = '')
+ {
+ return new lmbGdImageContainer($file_name, $type);
+ }
+
+ function isSupportConversion($file, $src_type = '', $dest_type = '')
+ {
+ if(!$src_type)
+ {
+ $imginfo = @getimagesize($file);
+ if(!$imginfo) throw new lmbFileNotFoundException($file);
+ $src_type = lmbGdImageContainer::convertImageType($imginfo[2]);
+ }
+ if(!$dest_type) $dest_type = $src_type;
+ return lmbGdImageContainer::supportLoadType($src_type) &&
+ lmbGdImageContainer::supportSaveType($dest_type);
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/lmbAbstractImageContainer.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/lmbAbstractImageContainer.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/lmbAbstractImageContainer.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,29 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Abstract image container
+ *
+ * @package imagekit
+ * @version $Id$
+ */
+abstract class lmbAbstractImageContainer
+{
+
+ function __construct($file_name, $type = '')
+ {
+ $this->load($file_name, $type);
+ }
+
+ abstract function load($file_name, $type = '');
+
+ abstract function save($file_name = null, $type = '');
+
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/lmbAbstractImageConvertor.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/lmbAbstractImageConvertor.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/lmbAbstractImageConvertor.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,73 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Abstract image convertor
+ *
+ * @package imagekit
+ * @version $Id$
+ */
+abstract class lmbAbstractImageConvertor
+{
+ protected $filters = array();
+ protected $container = null;
+
+ function __construct()
+ {
+
+ }
+
+ function addFilter($name, $params)
+ {
+ $this->filters[] = $this->createFilter($name, $params);
+ }
+
+ function run($src, $dest, $src_type = '', $dest_type = '')
+ {
+ $container = $this->createImageContainer($src, $src_type);
+ foreach($this->filters as $filter) $filter->run($container);
+ $container->save($dest, $dest_type);
+ }
+
+ function load($file_name, $type = '')
+ {
+ $this->container = $this->createImageContainer($file_name, $type);
+ return $this;
+ }
+
+ function apply($name)
+ {
+ $args = func_get_args();
+ $params = array_slice($args, 1);
+ $filter = $this->createFilter($name, $params);
+ $filter->run($this->container);
+ return $this;
+ }
+
+ function save($file_name = null, $type = '')
+ {
+ $this->container->save($file_name, $type);
+ $this->container = null;
+ }
+
+ protected function loadFilter($dir, $name, $prefix)
+ {
+ $class = 'lmb'.$prefix.ucfirst($name).'ImageFilter';
+ $full_path = $dir.'/'.$class.'.class.php';
+ lmb_require($full_path);
+ return $class;
+ }
+
+ abstract protected function createFilter($name, $params);
+
+ abstract protected function createImageContainer($file_name, $type = '');
+
+ abstract function isSupportConversion($file, $src_type = '', $dest_type = '');
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/lmbAbstractImageFilter.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/lmbAbstractImageFilter.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/lmbAbstractImageFilter.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,83 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+lmb_require(dirname(__FILE__).'/lmbAbstractImageContainer.class.php');
+
+/**
+ * Abstract image filter
+ *
+ * @package imagekit
+ * @version $Id$
+ */
+abstract class lmbAbstractImageFilter
+{
+ protected $params;
+
+ function __construct($params)
+ {
+ $this->params = $params;
+ }
+
+ function parseHexColor($hex)
+ {
+ $length = strlen($hex);
+ $color['red'] = hexdec(substr($hex, $length - 6, 2));
+ $color['green'] = hexdec(substr($hex, $length - 4, 2));
+ $color['blue'] = hexdec(substr($hex, $length - 2, 2));
+ return $color;
+ }
+
+ function calcSize($src_w, $src_h, $dst_w, $dst_h, $preserve_aspect_ratio = true, $save_min_size = false)
+ {
+ $w = $dst_w;
+ $h = $dst_h;
+ if($preserve_aspect_ratio)
+ {
+ $scale = (float)1;
+ $scale_w = (float)$dst_w / (float)$src_w;
+ $scale_h = (float)$dst_h / (float)$src_h;
+ if($scale_w > 1 && $scale_h > 1)
+ {
+ if($save_min_size) $scale = 1;
+ elseif($scale_w > $scale_h) $scale = $scale_h;
+ else $scale = $scale_w;
+ }
+ elseif($scale_w < 1 && $scale_h < 1)
+ {
+ if($scale_w > $scale_h) $scale = $scale_h;
+ else $scale = $scale_w;
+ }
+ elseif($scale_w < 1) $scale = $scale_w;
+ else $scale = $scale_h;
+ $w = intval($src_w * $scale);
+ $h = intval($src_h * $scale);
+ }
+ elseif($save_min_size)
+ {
+ if($dst_w > $src_w) $w = $src_w;
+ if($dst_h > $src_h) $h = $src_h;
+ }
+ return array($w, $h);
+ }
+
+ function getParam($name, $num, $default = null)
+ {
+ $param = $default;
+ if(isset($this->params[$name])) $param = $this->params[$name];
+ elseif(isset($this->params[$num])) $param = $this->params[$num];
+ return $param;
+ }
+
+ abstract function run(lmbAbstractImageContainer $container);
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/src/lmbImageKit.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/src/lmbImageKit.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/src/lmbImageKit.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,42 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @package imagekit
+ * @version $Id$
+ */
+class lmbImageKit
+{
+
+ static function create($library = 'gd', $dir = '')
+ {
+ if(defined('LIMB_IMAGE_LIBRARY'))
+ $library = LIMB_IMAGE_LIBRARY;
+
+ $image_class_name = 'lmb' . ucfirst($library) . 'ImageConvertor';
+
+ $class_path = dirname(__FILE__) . '/'. $library . '/' . $image_class_name . '.class.php';
+
+ if(!file_exists($class_path))
+ throw new lmbFileNotFoundException($class_path, 'image library not found');
+
+ lmb_require($class_path);
+
+ return new $image_class_name();
+ }
+
+ static function load($file_name, $type = '', $library = 'gd', $dir = '')
+ {
+ $convertor = self::create($library, $dir);
+ $convertor->load($file_name, $type);
+ return $convertor;
+ }
+
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdResizeImageFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdResizeImageFilterTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdResizeImageFilterTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,103 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../../../src/gd/lmbGdImageContainer.class.php');
+lmb_require(dirname(__FILE__).'/../../../../src/gd/filters/lmbGdResizeImageFilter.class.php');
+
+class lmbGdResizeImageFilterTest extends UnitTestCase
+{
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../../../var/input.jpg';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../../../var/output.jpg';
+ }
+
+ function _getContainer()
+ {
+ return new lmbGdImageContainer($this->_getInputImage());
+ }
+
+ function testSimpleResize()
+ {
+ $cont = $this->_getContainer();
+ $filter = new lmbGdResizeImageFilter(array('width' => 50, 'height' => 70, 'preserve_aspect_ratio' => false));
+
+ $filter->run($cont);
+ $cont->save($this->_getOutputImage());
+ list($width, $height, $type) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, 50);
+ $this->assertEqual($height, 70);
+ }
+
+ function testPreserveAspectRatio()
+ {
+ $filter = new lmbGdResizeImageFilter(array());
+
+ list($w, $h) = $filter->calcSize(100, 60, 20, 30, true);
+ $this->assertEqual($w, 20);
+ $this->assertEqual($h, 12);
+
+ list($w, $h) = $filter->calcSize(60, 100, 20, 30, true);
+ $this->assertEqual($w, 18);
+ $this->assertEqual($h, 30);
+ }
+
+ function testSaveMinSize()
+ {
+ $filter = new lmbGdResizeImageFilter(array());
+
+ list($w, $h) = $filter->calcSize(100, 60, 20, 30, true, true);
+ $this->assertEqual($w, 20);
+ $this->assertEqual($h, 12);
+
+ list($w, $h) = $filter->calcSize(60, 100, 20, 30, true, true);
+ $this->assertEqual($w, 18);
+ $this->assertEqual($h, 30);
+
+ list($w, $h) = $filter->calcSize(10, 20, 20, 30, true, true);
+ $this->assertEqual($w, 10);
+ $this->assertEqual($h, 20);
+
+ list($w, $h) = $filter->calcSize(10, 20, 20, 30, true);
+ $this->assertEqual($w, 15);
+ $this->assertEqual($h, 30);
+
+ list($w, $h) = $filter->calcSize(10, 20, 20, 30, false, true);
+ $this->assertEqual($w, 10);
+ $this->assertEqual($h, 20);
+ }
+
+ function testParams()
+ {
+ $filter = new lmbGdResizeImageFilter(array('width' => 90, 'height' => 100, 'preserve_aspect_ratio' => false, 'save_min_size' => true));
+
+ $this->assertEqual($filter->getWidth(), 90);
+ $this->assertEqual($filter->getHeight(), 100);
+ $this->assertFalse($filter->getPreserveAspectRatio());
+ $this->assertTrue($filter->getSaveMinSize());
+
+ $filter = new lmbGdResizeImageFilter(array(10, 50, true, false));
+
+ $this->assertEqual($filter->getWidth(), 10);
+ $this->assertEqual($filter->getHeight(), 50);
+ $this->assertTrue($filter->getPreserveAspectRatio());
+ $this->assertFalse($filter->getSaveMinSize());
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdRotateImageFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdRotateImageFilterTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdRotateImageFilterTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,68 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../../../src/gd/lmbGdImageContainer.class.php');
+lmb_require(dirname(__FILE__).'/../../../../src/gd/filters/lmbGdRotateImageFilter.class.php');
+
+class lmbGdRotateImageFilterTest extends UnitTestCase {
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../../../var/input.jpg';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../../../var/output.jpg';
+ }
+
+ function _getContainer()
+ {
+ return new lmbGdImageContainer($this->_getInputImage());
+ }
+
+ function testRotate()
+ {
+ $cont = $this->_getContainer();
+ $filter = new lmbGdRotateImageFilter(array('angle' => 90));
+
+ $filter->run($cont);
+ $cont->save($this->_getOutputImage());
+ list($width, $height, $type) = getimagesize($this->_getInputImage());
+ list($width2, $height2, $type2) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, $height2);
+ $this->assertEqual($height, $width2);
+ $this->assertEqual($type, $type2);
+ }
+
+ function testParams()
+ {
+ $filter = new lmbGdRotateImageFilter(array('angle' => 90, 'bgcolor' => 'FF0000'));
+
+ $this->assertEqual($filter->getAngle(), 90);
+ $bgcolor = $filter->getBgColor();
+ $this->assertEqual($bgcolor['red'], 255);
+ $this->assertEqual($bgcolor['green'], 0);
+ $this->assertEqual($bgcolor['blue'], 0);
+
+ $filter = new lmbGdRotateImageFilter(array(100, 'bgcolor' => '00FF00'));
+
+ $this->assertEqual($filter->getAngle(), 100);
+ $bgcolor = $filter->getBgColor();
+ $this->assertEqual($bgcolor['red'], 0);
+ $this->assertEqual($bgcolor['green'], 255);
+ $this->assertEqual($bgcolor['blue'], 0);
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdWaterMarkImageFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdWaterMarkImageFilterTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/gd/filters/lmbGdWaterMarkImageFilterTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,93 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../../../src/gd/lmbGdImageContainer.class.php');
+lmb_require(dirname(__FILE__).'/../../../../src/gd/filters/lmbGdWaterMarkImageFilter.class.php');
+
+class lmbGdWaterMarkImageFilterTest extends UnitTestCase
+{
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../../../var/input.jpg';
+ }
+
+ function _getWaterMarkImage()
+ {
+ return dirname(__FILE__).'/../../../var/water_mark.gif';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../../../var/output.jpg';
+ }
+
+ function _getContainer()
+ {
+ return new lmbGdImageContainer($this->_getInputImage());
+ }
+
+ function testWaterMark()
+ {
+ $cont = $this->_getContainer();
+ $filter = new lmbGdWaterMarkImageFilter(array('water_mark' => $this->_getWaterMarkImage(), 'x' => 5, 'y' => 6));
+
+ $filter->run($cont);
+ $cont->save($this->_getOutputImage());
+ list($width, $height, $type) = getimagesize($this->_getInputImage());
+ list($width2, $height2, $type2) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, $width2);
+ $this->assertEqual($height, $height2);
+ $this->assertEqual($type, $type2);
+ }
+
+ function testParams()
+ {
+ $filter = new lmbGdWaterMarkImageFilter(array('water_mark' => 'input.jpg', 'x' => 90, 'y' => 100, 'opacity' => 20));
+
+ $this->assertEqual($filter->getWaterMark(), 'input.jpg');
+ $this->assertEqual($filter->getX(), 90);
+ $this->assertEqual($filter->getY(), 100);
+ $this->assertEqual($filter->getOpacity(), 20);
+
+ $filter = new lmbGdWaterMarkImageFilter(array('input.jpg', 10, 50, 30));
+
+ $this->assertEqual($filter->getWaterMark(), 'input.jpg');
+ $this->assertEqual($filter->getX(), 10);
+ $this->assertEqual($filter->getY(), 50);
+ $this->assertEqual($filter->getOpacity(), 30);
+ }
+
+ function testCalcPosition()
+ {
+ $filter = new lmbGdWaterMarkImageFilter(array());
+
+ $result = $filter->calcPosition(10, 100, 150, 250);
+ $this->assertEqual($result[0], 10);
+ $this->assertEqual($result[1], 100);
+
+ $result = $filter->calcPosition(-10, 100, 150, 250);
+ $this->assertEqual($result[0], 140);
+ $this->assertEqual($result[1], 100);
+
+ $result = $filter->calcPosition(10, -100, 150, 250);
+ $this->assertEqual($result[0], 10);
+ $this->assertEqual($result[1], 150);
+
+ $result = $filter->calcPosition(-10, -100, 150, 250);
+ $this->assertEqual($result[0], 140);
+ $this->assertEqual($result[1], 150);
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageContainerTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageContainerTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageContainerTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,76 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../../src/gd/lmbGdImageContainer.class.php');
+
+class lmbGdImageContainerTest extends UnitTestCase {
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../../var/input.jpg';
+ }
+
+ function _getPalleteImage()
+ {
+ return dirname(__FILE__).'/../../var/water_mark.gif';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../../var/output.jpg';
+ }
+
+ function testConstructSave()
+ {
+ $cont = new lmbGdImageContainer($this->_getInputImage());
+ $cont->save($this->_getOutputImage());
+
+ list($width, $height, $type) = getimagesize($this->_getInputImage());
+ list($width2, $height2, $type2) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, $width2);
+ $this->assertEqual($height, $height2);
+ $this->assertEqual($type, $type2);
+ }
+
+ function testChangeType()
+ {
+ $cont = new lmbGdImageContainer($this->_getInputImage(), 'jpeg');
+ $cont->save($this->_getOutputImage(), 'gif');
+ list($width, $height, $type) = getimagesize($this->_getInputImage());
+ list($width2, $height2, $type2) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, $width2);
+ $this->assertEqual($height, $height2);
+ $this->assertNotEqual($type, $type2);
+ $this->assertEqual($type, IMAGETYPE_JPEG);
+ $this->assertEqual($type2, IMAGETYPE_GIF);
+ }
+
+ function testGetSize()
+ {
+ $cont = new lmbGdImageContainer($this->_getInputImage());
+
+ $this->assertEqual($cont->getWidth(), 100);
+ $this->assertEqual($cont->getHeight(), 137);
+ }
+
+ function testIsPallete()
+ {
+ $cont = new lmbGdImageContainer($this->_getInputImage());
+ $this->assertFalse($cont->isPallete());
+
+ $cont = new lmbGdImageContainer($this->_getPalleteImage());
+ $this->assertTrue($cont->isPallete());
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageConvertorTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageConvertorTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/gd/lmbGdImageConvertorTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,54 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../../src/gd/lmbGdImageConvertor.class.php');
+
+class lmbGdImageConvertorTest extends UnitTestCase {
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../../var/input.jpg';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../../var/output.jpg';
+ }
+
+ function _getConvertor()
+ {
+ return new lmbGdImageConvertor();
+ }
+
+ function testSimpleResize()
+ {
+ $conv = $this->_getConvertor();
+ $conv->addFilter('resize', array('width' => 50, 'height' => 70, 'preserve_aspect_ratio' => false));
+
+ $conv->run($this->_getInputImage(), $this->_getOutputImage());
+ list($width, $height, $type) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, 50);
+ $this->assertEqual($height, 70);
+ }
+
+ function testCheckSupportConv()
+ {
+ $conv = $this->_getConvertor();
+
+ $this->assertTrue($conv->isSupportConversion('', 'jpeg', 'gif'));
+ $this->assertTrue($conv->isSupportConversion($this->_getInputImage()));
+ $this->assertFalse($conv->isSupportConversion($this->_getInputImage(), '', 'zxzx'));
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/cases/lmbImageKitTest.class.php
===================================================================
--- 3.x/trunk/limb/imagekit/tests/cases/lmbImageKitTest.class.php (rev 0)
+++ 3.x/trunk/limb/imagekit/tests/cases/lmbImageKitTest.class.php 2007-09-20 15:56:39 UTC (rev 6322)
@@ -0,0 +1,45 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require(dirname(__FILE__).'/../../src/lmbImageKit.class.php');
+
+class lmbImageKitTest extends UnitTestCase {
+
+ function _getInputImage()
+ {
+ return dirname(__FILE__).'/../var/input.jpg';
+ }
+
+ function _getOutputImage()
+ {
+ return dirname(__FILE__).'/../var/output.jpg';
+ }
+
+ function testCreateGdConvertor()
+ {
+ $conv = lmbImageKit::create('gd');
+
+ $this->assertIsA($conv, 'lmbGdImageConvertor');
+ }
+
+ function testTraversing()
+ {
+ lmbImageKit::load($this->_getInputImage())->apply('resize', 50, 60, false)->apply('rotate', 90)->save($this->_getOutputImage());
+
+ list($width, $height, $type) = getimagesize($this->_getOutputImage());
+ $this->assertEqual($width, 60);
+ $this->assertEqual($height, 50);
+ }
+
+ function tearDown()
+ {
+ @unlink($this->_getOutputImage());
+ }
+}
+?>
\ No newline at end of file
Added: 3.x/trunk/limb/imagekit/tests/var/input.jpg
===================================================================
(Binary files differ)
Property changes on: 3.x/trunk/limb/imagekit/tests/var/input.jpg
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: 3.x/trunk/limb/imagekit/tests/var/water_mark.gif
===================================================================
(Binary files differ)
Property changes on: 3.x/trunk/limb/imagekit/tests/var/water_mark.gif
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
More information about the limb-svn
mailing list