1. Go to this page and download the library: Download crisu83/yii-imagemanager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
'imageManager' => array(
.....
'presets' => array(
'product' => array(
'filters' => array(
array('thumbnail', 'width' => 220, 'height' => 220, 'mode' => 'outbound'),
),
),
),
'holders' => array(
'default' => 'placeholder.png', // you need to add an image named placeholder.png in the images/holder directory for this
),
),
/**
* .....
* Methods accessible through the 'ImageBehavior' class:
* @method CUploadedFile getUploadedImage()
* @method Image saveImage($file, $name = null, $path = null, $scenario = 'insert')
* @method string renderImagePreset($name, $alt = '', $htmlOptions = array(), $holder = null)
* @method string createImagePresetUrl($name, $holder = null)
* @method boolean deleteImage()
*/
class Product extends CActiveRecord
{
/**
* @var CUploadedFile the uploaded file (used when uploading a product image).
*/
public $upload;
/**
* @return array the behavior configurations (behavior name=>behavior config).
*/
public function behaviors()
{
return array(
'image' => array(
'class' => 'vendor.crisu83.yii-imagemanager.behaviors.ImageBehavior',
'name' => $this->name,
'path' => 'products',
),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'image' => array(self::BELONGS_TO, 'Image', 'imageId'),
);
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
......
array('upload', 'file'), // configure the validator if necessary
);
}
/**
* @return array customized attribute labels (name=>label).
*/
public function attributeLabels()
{
return array(
'upload' => 'Image',
);
}
.....
}
class ProductController extends Controller
{
.....
/**
* Displays the page for editing the details for a specific product.
*/
public function actionUpdate($id)
{
$model = Product::model()->findByPk($id);
if (isset($_POST['Product']) {
$model->attributes = $_POST['Product'];
if ($model->save()) {
$this->redirect(array('admin'));
}
}
$this->render('update', array('model' => $model);
}
}