Created
May 6, 2012 22:09
-
-
Save crisu83/2624789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ImageBehavior class file. | |
* @author Christoffer Niska <ChristofferNiska@gmail.com> | |
* @copyright Copyright © Christoffer Niska 2011- | |
* @license http://www.opensource.org/licenses/bsd-license New BSD License | |
* @since 1.1.0 | |
*/ | |
class ImageBehavior 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 string $path the path for saving the image. | |
* @param CUploadedFile $file the uploaded file. | |
*/ | |
public function saveImage($file, $name = null, $path = null) | |
{ | |
$image = Yii::app()->image->save($file, $name, $path); | |
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. | |
*/ | |
public function renderImage($version, $alt = '', $htmlOptions = array()) | |
{ | |
if ($this->owner->hasAttribute($this->attribute)) | |
{ | |
$image = Yii::app()->image->loadModel($this->owner->{$this->attribute}); | |
if ($image !== null) | |
$image->render($version, $alt, $htmlOptions); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment