PHP code example of oxy-coach / yii2-image-behavior
1. Go to this page and download the library: Download oxy-coach/yii2-image-behavior 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/ */
oxy-coach / yii2-image-behavior example snippets
$this->createTable('images', [
'id' => $this->primaryKey(),
'itemId' => $this->integer(11)->notNull(),
'path' => $this->string(255),
'extension' => $this->string(255)->notNull(),
'sort' => $this->integer()->defaultValue(0),
]);
use oxycoach\imagebehavior\ImageBehavior;
\\ ...
public $file;
public function behaviors()
{
return [
'ImageBehavior' => [
'class' => ImageBehavior::class,
'imageModel' => Images::class,
'imageVariable' => 'file',
'uploadPath' => '@upload',
'webUpload' => '@webupload',
'noImagePath' => '@webupload/no-photo.png',
'multiple' => true,
'sizes' => [
'original' => [
'folder' => 'images/original'
],
'preview' => [
'folder' => 'images/preview',
'width' => 350,
'height' => 0, // "0" means auto-height
],
],
],
];
}
public function rules()
{
[['file'], 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 3],
}
/**
* Images model relation
* @return \yii\db\ActiveQuery
*/
public function getImages()
{
return $this->hasMany(Images::class, ['itemId' => 'id'])
->alias('img')
->orderBy('img.sort ASC');
}
<?= $form->field($model, 'file')->fileInput(['multiple' => false, 'accept' => 'image/*'])
html
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']])
html
foreach ($model->getAllImages('original') as $image) {