PHP code example of ivankff / yii2-upim
1. Go to this page and download the library: Download ivankff/yii2-upim 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/ */
ivankff / yii2-upim example snippets
Yii::setAlias('@cache-image', dirname(dirname(dirname(__DIR__))) . '/alias/cache/image');
Yii::setAlias('@image', dirname(dirname(dirname(__DIR__))) . '/alias/image');
'components' => [
'image' => [
'class' => 'yii\image\ImageDriver',
'driver' => 'GD', //GD or Imagick
],
],
[
'images.widen' => 2000,
'images.securityKey' => 'some.security.key',
],
/**
* @property-read ImagesInterface $images
*/
class Product extends ActiveRecord
{
public function behaviors()
{
return [
'images' => [
'class' => 'ivankff\yii2UploadImages\behaviors\ImagesBehavior',
'dir' => '@image/product',
'widen' => (int)ArrayHelper::getValue(Yii::$app->params, 'image.widen', 0),
],
];
}
}
public function actions()
{
return [
'delete-file' => 'ivankff\yii2UploadImages\actions\DeleteFileAction',
];
}
/**
* @property-read string[] $uploadImagesFiles
*/
class ProductForm extends Model {
/** @var UploadedFile */
public $uploadImages;
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'upload' => [
'class' => 'ivankff\yii2UploadImages\behaviors\UploadBehavior',
'formAttribute' => 'uploadImages',
'initFiles' => $this->_ar->images->getAll(),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['uploadImages_keys'], 'string'],
[['uploadImages'], 'image', 'maxFiles' => 20],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'uploadImages' => 'Изображения',
];
}
/**
* @param bool $runValidation
* @return bool
*/
public function save($runValidation = true)
{
if ($runValidation && !$this->validate())
return false;
$this->_ar->images->replace($this->uploadImagesFiles);
return $this->_ar->save($runValidation);
}
}
$uploadImages = [];
foreach ($model->uploadImagesFiles as $i => $image)
$uploadImages[] = Yii::$app->router->productThumbnailDop($model->product, $i);
echo $form->field($model, 'uploadImages[]')->widget('ivankff\yii2UploadImages\FileInputWidget', [
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'initialPreview' => $uploadImages,
'overwriteInitial' => false,
]
]);
class Router extends Component
{
/**
* @var string компонент-менеджер фронтенда
*/
public $urlManagerFrontend = 'urlManagerFrontend';
/** @var UrlManager */
private $_frontendManager;
/**
* @param Product $product
* @param array $params
* @param int $i
* @return null|string
*/
public function productThumbnail(Product $product, int $i, $params = [])
{
$req = $this->_getImageRequest($params, $product->id, $i);
return $this->_frontendManager->createUrl(ArrayHelper::merge(
['/catalog/product/picture', 'name' => Inflector::slug($product->title)],
$req->getRequestParams()
));
}
/**
* @param array $params
* @param int $id
* @param int|null $i
* @return \ivankff\yii2UploadImages\ImageActionRequest
* @throws
*/
private function _getImageRequest($params, $id, $i = null)
{
$params['class'] = 'ivankff\yii2UploadImages\actions\ImageActionRequest';
$params['id'] = $id;
$params['i'] = $i;
return Yii::createObject($params);
}
}
public function actions()
{
return [
'picture' => [
'class' => 'ivankff\yii2UploadImages\actions\ImageAction',
'activeRecordClass' => 'common\entities\Product\Product',
],
];
}
['pattern' => 'picture/<name>__p<id:\d+>m.jpg', 'route' => 'catalog/product/picture', 'suffix' => false, 'defaults' => ['i' => 1]],
['pattern' => 'picture/<name>__p<id:\d+>d<i:\d+>.jpg', 'route' => 'catalog/product/picture', 'suffix' => false],