PHP code example of deanar / yii2-file-processor
1. Go to this page and download the library: Download deanar/yii2-file-processor 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/ */
deanar / yii2-file-processor example snippets
'modules' => [
'fp' => [
'class' => 'deanar\fileProcessor\Module',
//'image_driver' => \deanar\fileProcessor\models\Uploads::IMAGE_DRIVER_GD,
'variations_config' => _dir' => 'uploads',
//'default_quality' => 95,
//'default_resize_mod' => 'outbound',
//'unlink_files' => true,
//'debug' => true, // FileAPI debug. false by default
],
]
public function behaviors()
{
return [
'fileSequence' => [
'class' => \deanar\fileProcessor\behaviours\ConnectFileSequence::className(),
'defaultType' => 'projects',
'registeredTypes' => ['projects', 'files'], // or 'projects, files' as string
]
];
}
use deanar\fileProcessor\components\WatermarkFilter;
return [
'projects' => [
'_original' => false,
'thumb' => [200, 150, 'inset'],
'small' => [300, 200, 'outbound', 75],
'big' => [
'width' => 600,
'height' => 350,
'mode' => 'outbound',
'quality' => 75,
'watermark' => [
'path' => 'watermark.png',
'position' => WatermarkFilter::WM_POSITION_BOTTOM_RIGHT,
'margin' => 10,
]
],
],
'article_header' => [
'_original' => true,
'thumb' => [200, 150, 'inset'],
],
'avatar_picture' => [
'_original' => true,
'preview' => [200, 200, 'outbound'],
// For single file uploads. Automatically will be updated 'avatar' attribute in 'Project' model
// with <id> of currently uploaded file
'_insert' => ['app\models\Project' => 'avatar'],
// variants of access control definitions
'_acl' => '*', // * - all users, like without _acl
'_acl' => '@', // @ - authenticated users only
'_acl' => ['users' => ['admin', 'user1']], // defined list of users
'_acl' => ['app\models\Project' => 'user_id'], // if current user id equals to `user_id` attribute of model `app\models\Project`
'_acl' => function ($type_id, $user_id) { // callable check
return \app\models\Project::findOne($type_id)->user_id == $user_id;
},
],
// Used if no variation with specified name found
'_default' => [ ],
// Mixin for all variations. Used by merging arrays.
'_all' => [ ],
];
<?= \deanar\fileProcessor\widgets\MultiUploadWidget::widget([
'type' => 'projects',
'type_id' => $model->id,
'options' => [
'autoUpload' => true,
'multiple' => true,
'accept' => 'image/*,application/zip',
'duplicate' => false,
'maxSize' => '2M', // you can use 'M', 'K', 'G' or simple size in bytes
'maxFiles' => 3,
'imageSize' => [
'minWidth' => 150,
'maxWidth' => 2000,
'minHeight' => 150,
'maxHeight' => 2000,
],
],
'htmlOptions' => [
'class' => 'additional-class',
'data-attribute' => 'value',
],
])
<?= \deanar\fileProcessor\widgets\SingleUploadWidget::widget([
'type' => 'projects',
'type_id' => $model->id,
'crop' => true,
'preview' => true,
'previewSize' => [200,200],
'options' => [
'accept' => 'image/*',
'maxSize' => '2M', // you can use 'M', 'K', 'G' or simple size in bytes
'imageSize' => [
'minWidth' => 150,
'maxWidth' => 2000,
'minHeight' => 150,
'maxHeight' => 2000,
],
],
'htmlOptions' => [
'class' => 'additional-class',
'data-attribute' => 'value',
],
])
$model = ExampleModel::findOne(1);
$uploads = $model->getFiles();
foreach($uploads as $u){
echo $u->imgTag('thumb2', true,['style'=>'border:1px solid red;']);
//or just url (for files/download links)
echo \yii\helpers\Html::a($u->original, $u->getPublicFileUrl('original', true));
}
$uploads = $model->imagesOnly()->getFiles();
// or
$uploads = $model->filesOnly()->getFiles();
$uploads = $model->getFirstFile();
[
'class' => 'deanar\fileProcessor\components\ImageColumn',
'header' => 'Image', // optional
'empty' => 'No Image', // optional
'type' => 'projects', // optional, default value goes from behavior options
'variation' => '_thumb',
'htmlOptions' => [], // optional
],
'attributes' => [
'id',
'title',
...
[
'attribute'=>'Images',
'value'=>\deanar\fileProcessor\widgets\DisplayWidget::widget(['type'=>'projects','type_id'=>$model->id,'variation'=>'_thumb']),
'format'=>'raw',
],
...
'text',
],
php composer.phar
RemoveHandler .php
AddType text/html .php