noam148/yii2-image-manager

一个用于上传和裁剪图片的 Yii2 模块/小部件

安装次数: 13,940

依赖者: 0

建议者: 0

安全: 0

星标: 129

关注者: 14

分支: 61

开放问题: 23

类型:yii2-extension

1.2.13 2019-05-16 06:28 UTC

README

!!! 欢迎进一步开发此模块 !!!

Yii2 图像管理器

一个用于上传、管理和裁剪图片的 Yii2 模块/小部件

安装

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

  • 运行
php composer.phar require "noam148/yii2-image-manager" "*" 

或添加

"noam148/yii2-image-manager" : "*"

到您的应用 composer.json 文件的 require 部分。

  • 运行迁移以创建 ImageManager 表
yii migrate --migrationPath=@noam148/imagemanager/migrations
  • 在应用配置文件的 components 部分添加新的组件,例如
'components' => [
    'imagemanager' => [
		'class' => 'noam148\imagemanager\components\ImageManagerGetPath',
		//set media path (outside the web folder is possible)
		'mediaPath' => '/path/where/to/store/images/media/imagemanager',
        //path relative web folder. In case of multiple environments (frontend, backend) add more paths 
        'cachePath' =>  ['assets/images', '../../frontend/web/assets/images'],
		//use filename (seo friendly) for resized images else use a hash
		'useFilename' => true,
		//show full url (for example in case of a API)
		'absoluteUrl' => false,
		'databaseComponent' => 'db' // The used database component by the image manager, this defaults to the Yii::$app->db component
	],
],

并在 modules 部分添加,例如

'modules' => [
	'imagemanager' => [
		'class' => 'noam148\imagemanager\Module',
		//set accces rules ()
		'canUploadImage' => true,
		'canRemoveImage' => function(){
			return true;
		},
		'deleteOriginalAfterEdit' => false, // false: keep original image after edit. true: delete original image after edit
		// Set if blameable behavior is used, if it is, callable function can also be used
		'setBlameableBehavior' => false,
		//add css files (to use in media manage selector iframe)
		'cssFiles' => [
			'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css',
		],
	],
],

用法

要访问 imagemanager 模块,请访问

http://www.example.com/imagemanager

Image manager module Image manager module cropper

要加载图片选择器,请看以下内容(确保您的表中有一个字段,模块可以存储 ImageManager 表的 'id')

echo $form->field($model, 'ImageManager_id_avatar')->widget(\noam148\imagemanager\components\ImageManagerInputWidget::className(), [
	'aspectRatio' => (16/9), //set the aspect ratio
    'cropViewMode' => 1, //crop mode, option info: https://github.com/fengyuanchen/cropper/#viewmode
	'showPreview' => true, //false to hide the preview
	'showDeletePickedImageConfirm' => false, //on true show warning before detach image
]);

Image widget Image widget popup

如果您想使用图片

/*
 * $ImageManager_id (id that is store in the ImageManager table)
 * $width/$height width height of the image
 * $thumbnailMode: "outbound", "inset" or "{horz}:{vert}" where {horz} is one from "left", "center", "right" and {vert} is one from "top", "center", "bottom"
 */
\Yii::$app->imagemanager->getImagePath($ImageManager_id, $width, $height,$thumbnailMode)

支持 CKEditor & TinyMce

要在 CKEditor 中使用文件浏览器,请将 filebrowserImageBrowseUrl 添加到 CKEditor 小部件的 clientOptions 中。我只测试了来自 2amigOS 的 CKEditor,但它在其他 CKEditor 小部件上应该也能工作。

use dosamigos\ckeditor\CKEditor;

 echo $form->field($model, 'text')->widget(CKEditor::className(), [
	'options' => ['rows' => 6],
	'preset' => 'basic',
	'clientOptions' => [
		'filebrowserImageBrowseUrl' => yii\helpers\Url::to(['imagemanager/manager', 'view-mode'=>'iframe', 'select-type'=>'ckeditor']),
	]
]);

要在 TinyMce 中使用文件浏览器,请将 file_browser_callback 添加到 TinyMce 小部件的 clientOptions 中。我只测试了来自 2amigOS 的 TinyMce,但它在其他 TinyMce 小部件上应该也能工作。(别忘了将 'image' 添加到您的 'plugins' 数组中)

use dosamigos\tinymce\TinyMce;

echo $form->field($model, 'text')->widget(TinyMce::className(), [
	'options' => ['rows' => 6],
	'language' => 'nl',
	'clientOptions' => [
		'file_browser_callback' => new yii\web\JsExpression("function(field_name, url, type, win) {
			window.open('".yii\helpers\Url::to(['imagemanager/manager', 'view-mode'=>'iframe', 'select-type'=>'tinymce'])."&tag_name='+field_name,'','width=800,height=540 ,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no');
		}"),
		'plugins' => [
			"advlist autolink lists link charmap print preview anchor",
			"searchreplace visualblocks code fullscreen",
			"insertdatetime media table contextmenu paste image"
		],
		'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
	]
]);