Commits

Christoffer Niska committed 574e837

Added support for naming images and improved the image behavior.

Comments (0)

Files changed (7)

  * @author Christoffer Niska <ChristofferNiska@gmail.com>
  * @copyright Copyright &copy; Christoffer Niska 2011-
  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @version 1.1.0
+ * @version 1.2.0
  */
 class ImageModule extends CWebModule
 {
 	 * @property boolean whether the installer is enabled.
 	 */
 	public $install=false;
-	/**
-	 * @property boolean whether the debug mode is enabled.
-	 */
-	public $debug=false;
 
 	/**
 	 * Initializes the module.
 				)
 			));
 
+			$this->registerScripts();
+
 			$this->defaultController='install';
 		}
-
-		$this->registerScripts();
 	}
 
 	/**
 		$assetsPath=Yii::getPathOfAlias('image.assets');
 
 		// Republish the assets if debug mode is enabled.
-		if($this->debug===true)
+		if(YII_DEBUG)
 			return Yii::app()->assetManager->publish($assetsPath,false,-1,true);
 		else
 			return Yii::app()->assetManager->publish($assetsPath);
 	 */
 	public function getVersion()
 	{
-		return '1.1.0';
+		return '1.2.0';
 	}
 }

components/ImgInstaller.php

 		// Replace the placeholders in the template file.
 		$htaccess=strtr($htaccess,array(
 			'{baseUrl}'=>Yii::app()->getRequest()->getBaseUrl().'/',
-			'{sourceUrl}'=>'^versions/([a-zA-Z0-9]+)/([0-9]+)\.(gif|jpg|png)$',
+			'{sourceUrl}'=>'^versions/([\w\d]+)/[\w\d]+\-([\d]+)\.(gif|jpg|png)$',
 			'{targetUrl}'=>$targetUrl,
 		));
 

components/ImgManager.php

 	/**
 	 * @property string the base path.
 	 */
-	private $_basePath;
+	protected $_basePath;
 	/**
 	 * @property string the image version path.
 	 */
-	private $_versionBasePath;
+	protected $_versionBasePath;
 
 	private static $_thumbOptions=array(); // needed for the static factory-method
 	private static $_imagePath;
 	 * Returns the URL for a specific image.
 	 * @param string $id the image id.
 	 * @param string $version the name of the image version.
-	 * @param boolean $absolute whether to get an absolute URL.
+	 * @param boolean $absolute whether or not to get an absolute URL.
 	 * @return string the URL.
 	 * @throws CException if the version is not defined.
 	 */
 		if(isset($this->versions[$version]))
 		{
 			$image = $this->loadModel($id);
-			$options=ImgOptions::create($this->versions[$version]);
 			$filename=$this->resolveFileName($image);
 			$path=$this->getVersionPath($version);
 			return Yii::app()->request->getBaseUrl($absolute).'/'.$path.$filename;
 
 	/**
 	 * Saves a new image.
+	 * @param string $name the image name. Available since 1.2.0
 	 * @param CUploadedFile $file the uploaded image.
 	 * @return Image the image record.
 	 * @throws ImageException if saving the image record or file fails.
 	 */
-	public function save($file)
+	public function save($name,$file)
 	{
 		$trx=Yii::app()->db->beginTransaction();
 
 		try
 		{
 			$image=new Image();
+			$image->name=$this->normalizeString($name);
+			$image->extension=strtolower($file->getExtensionName());
 			$image->filename=$file->getName();
-			$image->extension=strtolower($file->getExtensionName());
 			$image->byteSize=$file->getSize();
 			$image->mimeType=$file->getType();
 			$image->created=new CDbExpression('NOW()');
 	 */
 	public function delete($id)
 	{
+		/** @var Image $image */
 		$image = Image::model()->findByPk($id);
 
 		if($image instanceof Image)
 		{
-			$filename=$this->resolveFileName($image);
-			$filepath=$this->getImagePath(true).$filename;
+			$fileName=$this->resolveFileName($image);
+			$filePath=$this->getImagePath(true).$fileName;
 
 			if($image->delete()===false)
 				throw new ImgException(Img::t('error', 'Failed to delete image! Record could not be deleted.'));
 
-			if(file_exists($filepath)!==false && unlink($filepath)===false)
+			if(file_exists($filePath)!==false && unlink($filePath)===false)
 				throw new ImgException(Img::t('error', 'Failed to delete image! File could not be deleted.'));
 
 			foreach($this->versions as $version=>$config)
 	 * @return boolean whether the image was deleted.
 	 * @throws ImgException if the image cannot be deleted.
 	 */
-	private function deleteVersion($image,$version)
+	protected function deleteVersion($image,$version)
 	{
 		if(isset($this->versions[$version]))
 		{
-			$filepath=$this->resolveImageVersionPath($image,$version);
-
-			if(file_exists($filepath)!==false && unlink($filepath)===false)
+			$filePath=$this->resolveImageVersionPath($image,$version);
+			if(file_exists($filePath)!==false && unlink($filePath)===false)
 				throw new ImgException(Img::t('error', 'Failed to delete the image version! File could not be deleted.'));
 		}
 		else
 	{
 		if(isset($this->versions[$version]))
 		{
-			$image = $this->loadModel($id);
+			$image=$this->loadModel($id);
 
 			if($image!=null)
 			{
 	}
 
 	/**
+	 * Returns the images path.
+	 * @param boolean $absolute whether or not the path should be absolute.
+	 * @return string the path.
+	 */
+	public function getImagePath($absolute=false)
+	{
+		$path='';
+
+		if($absolute===true)
+			$path.=$this->getBasePath();
+
+		return $path.$this->imagePath;
+	}
+
+	/**
 	 * Returns the version specific path.
 	 * @param string $version the name of the image version.
-	 * @param boolean $absolute whether the path should be absolute.
+	 * @param boolean $absolute whether or not the path should be absolute.
 	 * @return string the path.
 	 */
-	private function getVersionPath($version,$absolute=false)
+	protected function getVersionPath($version,$absolute=false)
 	{
 		$path=$this->getVersionBasePath($absolute).$version.'/';
 
 	 * @param Image $image the image model.
 	 * @return string the file name.
 	 */
-	private function resolveFileName($image)
+	protected function resolveFileName($image)
 	{
-		return $image instanceof Image ? $image->id.'.'.$image->extension : null;
+		if($image instanceof Image)
+		{
+			if(!empty($image->name))
+				return $image->name.'-'.$image->id.'.'.$image->extension; // since 1.2.0
+			else
+				return $image->id.'.'.$image->extension; // backwards compatibility
+		}
+		else
+			return null;
 	}
 
 	/**
 	 * @param string $version the image version.
 	 * @return string the path.
 	 */
-	private function resolveImageVersionPath($image,$version)
+	protected function resolveImageVersionPath($image,$version)
 	{
-		$filename=$this->resolveFileName($image);
-		return $image instanceof Image ? $this->getVersionPath($version,true).$filename : null;
+		if($image instanceof Image)
+			return $this->getVersionPath($version,true).$this->resolveFileName($image);
+		else
+			return null;
 	}
 
 	/**
 	 * Returns the base path.
 	 * @return string the path.
 	 */
-	private function getBasePath()
+	protected function getBasePath()
 	{
 		if($this->_basePath!==null)
 			return $this->_basePath;
 		else
-			return $this->_basePath=realpath( Yii::app()->basePath.'/../' ).'/';
-	}
-
-	/**
-	 * Returns the images path.
-	 * @param boolean $absolute whether the path should be absolute.
-	 * @return string the path.
-	 */
-	public function getImagePath($absolute=false)
-	{
-		$path='';
-
-		if($absolute===true)
-			$path.=$this->getBasePath();
-
-		return $path.$this->imagePath;
+			return $this->_basePath=realpath(Yii::app()->basePath.'/../').'/';
 	}
 
 	/**
 	 * Returns the image version path.
-	 * @param boolean $absolute whether the path should be absolute.
+	 * @param boolean $absolute whether or not the path should be absolute.
 	 * @return string the path.
 	 */
-	private function getVersionBasePath($absolute=false)
+	protected function getVersionBasePath($absolute=false)
 	{
 		$path='';
 
 	}
 
 	/**
+	 * Normalizes the given string by replacing special characters. �=>a, �=>e, �=>o, etc.
+	 * @param string the string to normalize.
+	 * @return string the normalized string.
+	 * @since 1.2.0
+	 */
+	protected function normalizeString($string)
+	{
+        return preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i','$1',htmlentities($string,ENT_QUOTES,'UTF-8'));
+	}
+
+	/**
 	 * Creates a new image.
 	 * @param string $fileName the file name.
 	 * @return ImgThumb
 	 */
-	private static function thumbFactory($fileName)
+	protected static function thumbFactory($fileName)
 	{
 		$phpThumb=PhpThumbFactory::create(self::$_imagePath.$fileName,self::$_thumbOptions);
 		return new ImgThumb($phpThumb);

components/ImgRecordBehavior.php

 class ImgRecordBehavior extends CBehavior
 {
 	/**
+	 * @property string the image id attribute. Defaults to "imageId".
+	 * @since 1.2.0
+	 */
+	public $attribute='imageId';
+
+	/**
 	 * Saves the image for the owner of this behavior.
+	 * @param string $name the image name.
 	 * @param CUploadedFile $file the uploaded file.
 	 */
-	public function saveImage($file)
+	public function saveImage($name,$file)
 	{
-		$image = Yii::app()->image->save($file);
+		$image=Yii::app()->image->save($name,$file);
 
-		if ($image !== null && $this->owner->hasAttribute('imageId'))
-			$this->owner->imageId = $image->id;
+		if($image!==null && $this->owner->hasAttribute($this->attribute))
+			$this->owner->{$this->attribute}=$image->id;
 	}
 
 	/**
 	 * Renders the image for the owner of this behavior.
 	 * @param string $version the name of the image version.
-	 * @param string $alt the image alternative text
-	 * @param array $htmlOptions the additional HTML options
+	 * @param string $alt the image alternative text.
+	 * @param array $htmlOptions the additional HTML options.
 	 */
 	public function renderImage($version,$alt='',$htmlOptions=array())
 	{
-		if ($this->owner->hasAttribute('imageId'))
+		if($this->owner->hasAttribute($this->attribute))
 		{
-			$image = Yii::app()->image->loadModel($this->owner->imageId);
+			$image=Yii::app()->image->loadModel($this->owner->{$this->attribute});
 
-			if ($image !== null)
+			if ($image!==null)
 				$image->render($version,$alt,$htmlOptions);
 		}
 	}
 
 CREATE TABLE `Image` (
 	`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-	`parentId` int(10) unsigned NOT NULL,
-	`parent` varchar(255) NOT NULL,
+	`name` varchar(255) NOT NULL,
+	`extension` varchar(255) NOT NULL,
 	`filename` varchar(255) NOT NULL,
-	`extension` varchar(255) NOT NULL,
 	`byteSize` int(10) unsigned NOT NULL,
 	`mimeType` varchar(255) NOT NULL,
 	`created` timestamp NULL DEFAULT NULL,

messages/templates/install.php

 return array (
   'An error occurred while installing the Image module.' => '',
   'Congratulations!' => '',
-  'Continue &raquo;' => '',
+  'Continue' => '',
   'Do you wish to continue?' => '',
   'Error' => '',
   'Install' => '',
  *
  * The followings are the available columns in table 'Image':
  * @property integer $id
+ * @property string $name
+ * @property string $extension
  * @property string $filename
- * @property string $extension
  * @property integer $byteSize
  * @property string $mimeType
  * @property string $created
 {
 	/**
 	 * Returns the static model of the specified AR class.
+	 * @param string $className the class name.
 	 * @return Image the static model class
 	 */
 	public static function model($className=__CLASS__)
 	public function rules()
 	{
 		return array(
-			array('filename, extension, byteSize, mimeType', 'required'),
-			array('byteSize', 'numerical', 'integerOnly'=>true),
-			array('filename, extension, mimeType, created', 'length', 'max'=>255),
-			array('id, filename, extension, byteSize, mimeType, created', 'safe', 'on'=>'search'),
+			array('name, extension, filename, byteSize, mimeType','required'),
+			array('byteSize','numerical','integerOnly'=>true),
+			array('name, extension, filename, mimeType, created','length','max'=>255),
+			array('id, name, extension, filename, byteSize, mimeType, created','safe','on'=>'search'),
 		);
 	}
 
 	{
 		return array(
 			'id' => Img::t('core','Id'),
+			'name' => Img::t('core','Name'),
+			'extension' => Img::t('core','Extension'),
 			'filename' => Img::t('core','Filename'),
-			'extension' => Img::t('core','Extension'),
 			'byteSize' => Img::t('core','Byte Size'),
 			'mimeType' => Img::t('core','Mime Type'),
 			'created' => Img::t('core','Created'),
 		$criteria=new CDbCriteria;
 
 		$criteria->compare('id',$this->id);
+		$criteria->compare('name',$this->name,true);
+		$criteria->compare('extension',$this->extension,true);
 		$criteria->compare('filename',$this->filename,true);
-		$criteria->compare('extension',$this->extension,true);
 		$criteria->compare('byteSize',$this->byteSize);
 		$criteria->compare('mimeType',$this->mimeType,true);
 		$criteria->compare('created',$this->created,true);
 	 */
 	public function render($version,$alt='',$htmlOptions=array())
 	{
-		$src = Yii::app()->image->getURL($this->id, $version);
+		$src = Yii::app()->image->getURL($this->id,$version);
 		echo CHtml::image($src,$alt,$htmlOptions);
 	}
 }