Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created May 6, 2012 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/2624789 to your computer and use it in GitHub Desktop.
Save crisu83/2624789 to your computer and use it in GitHub Desktop.
<?php
/**
* ImageBehavior class file.
* @author Christoffer Niska <ChristofferNiska@gmail.com>
* @copyright Copyright &copy; 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