fabiomlferreira/yii2-file-manager

Yii2 文件管理器

1.0.3 2020-05-07 18:44 UTC

This package is auto-updated.

Last update: 2024-09-08 05:17:01 UTC


README

此模块提供接口,以便在同一个地方收集和访问所有媒体文件。此模块具有启用即时缩略图生成的选项。

功能

  • 与 TinyMCE 编辑器集成。
  • 自动为上传的文件创建实际目录,如 "2017/03"。
  • 自动为上传的图片创建缩略图或即时生成。
  • 无限数量的缩略图集
  • 所有媒体文件都存储在数据库中,允许您将对象附加到不链接到图片,而是链接到媒体文件ID。这提供了更大的灵活性,因为将来可以轻松更改缩略图的大小。
  • 如果您更改缩略图大小,可以在设置中调整所有现有缩略图的大小。

屏幕截图

安装

安装此扩展的首选方法是通过 composer

运行以下命令:

php composer.phar require --prefer-dist fabiomlferreira/yii2-file-manager "*"

或将其添加到您的 composer.json 文件的 require 部分中:

"fabiomlferreira/yii2-file-manager": "*"

应用迁移

yii migrate --migrationPath=vendor/fabiomlferreira/yii2-file-manager/migrations

配置

'modules' => [
    'filemanager' => [
        'class' => 'fabiomlferreira\filemanager\Module',
        'rename' => true, //enable upload multiple images with the same name, this will rewrite the images name
        'optimizeOriginalImage' => true, //Optimize the original image
        'maxSideSize' => 1200, //limit the maximum size for the original image only work if 'optimizeOriginalImage' => true
        'originalQuality' => 80, // quality for the original image  only work if 'optimizeOriginalImage' => true
        'thumbnailOnTheFly' => false,  //if is true will generate the thumbnails on the fly, is required that you set the component "thumbnail"
        // Upload routes
        'routes' => [
            // Base absolute path to web directory
            'baseUrl' => '',
            // Base web directory url
            'basePath' => '@frontend/web', //for yii2 advanced template
            // Path for uploaded files in web directory
            'uploadPath' => 'uploads',
        ],
        // Thumbnails info
        'thumbs' => [
            'small' => [
                'name' => 'Small',
                'size' => [100, 100],
            ],
            'medium' => [
                'name' => 'Regular',
                'size' => [300, 200],
            ],
            'large' => [
                'name' => 'Large',
                'size' => [500, 400],
            ],
        ],
        // following line will restrict access to admin controller from frontend application
        'as frontend' => 'fabiomlferreira\filemanager\filters\FrontendFilter',
    ],
],

默认情况下,缩略图以 "outbound" 或 "fill" 模式调整大小。要切换到 "inset" 或 "fit" 模式,请使用 mode 参数并提供。可能的值:outbound\Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND) 或 inset\Imagine\Image\ImageInterface::THUMBNAIL_INSET

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET
    ],
],

如果您需要在不添加白色边框的情况下保持宽高比,可以将模式设置为 INSET 并将 keepAspectRatio 属性设置为 true。这将使用 Box 方法来固定最高边,并按比例调整另一边

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET,
        'keepAspectRatio' => true,
    ],
],

如果您需要保持宽高比并将图像放大到始终选中的大小,即使某些图像部分被裁剪。

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'forceUpscale' => true,
    ],
],

如果您需要更改内嵌调整大小边框的颜色和透明度,请使用此方法。

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET,
        'thumbnailBackgroundColor' => 'FFF',
        'thumbnailBackgroundAlpha' => 0
    ],
],

如果您将 'thumbnailOnTheFly' 设置为 true,则需要配置 Thumbnail 组件。

'components' => [
    'thumbnail' => [
        'class' => 'fabiomlferreira\filemanager\Thumbnail',
        'cachePath' => '@webroot/assets/thumbnails', // path for the folder for temporary thumbnails 
        'basePath' => '@webroot',
        'cacheExpire' => 2592000, // time that the thumbnails keeps in cache
        'options' => [
            'placeholder' => [
                'type' => fabiomlferreira\filemanager\Thumbnail::PLACEHOLDER_TYPE_JS,
                'backgroundColor' => '#f5f5f5',
                'textColor' => '#cdcdcd',
                'text' => 'Ooops',
                'random' => true,
                'cache' => false,
            ],
            'quality' => 75
        ]
    ],
]

请阅读 Thumbnail 组件的文档

使用方法

简单的独立字段

use fabiomlferreira\filemanager\widgets\FileInput;

echo $form->field($model, 'original_thumbnail')->widget(FileInput::className(), [
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    // Default to FileInput::DATA_URL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_URL,
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "some description", description: "123", url: "/uploads/2017/03/vedma-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

echo FileInput::widget([
    'name' => 'mediafile',
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    // Default to FileInput::DATA_IDL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_ID,
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "Ведьма с кошкой", description: "123", url: "/uploads/2014/12/vedma-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

与 TinyMCE 一起使用

use fabiomlferreira\filemanager\widgets\TinyMCE;

<?= $form->field($model, 'content')->widget(TinyMCE::className(), [
    'clientOptions' => [
           'language' => 'ru',
        'menubar' => false,
        'height' => 500,
        'image_dimensions' => false,
        'plugins' => [
            'advlist autolink lists link image charmap print preview anchor searchreplace visualblocks code contextmenu table',
        ],
        'toolbar' => 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code',
    ],
]); ?>

在模型中,您必须设置媒体文件行为,如下例所示

use fabiomlferreira\filemanager\behaviors\MediafileBehavior;

public function behaviors()
{
    return [
        'mediafile' => [
            'class' => MediafileBehavior::className(),
            'name' => 'post',
            'attributes' => [
                'thumbnail',
            ],
        ]
    ];
}

然后,您可以从您的拥有模型中获取媒体文件。请参阅示例

use fabiomlferreira\filemanager\models\Mediafile;

$model = Post::findOne(1);
$mediafile = Mediafile::loadOneByOwner('post', $model->id, 'thumbnail');

// Ok, we have mediafile object! Let's do something with him:
// return url for small thumbnail, for example: '/uploads/2014/12/flying-cats.jpg'
echo $mediafile->getThumbUrl('small');
// return image tag for thumbnail, for example: '<img src="/uploads/2017/03/flying-dogs.jpg" alt="ypload">'
echo $mediafile->getThumbImage('small'); // return url for small thumbnail