johnny13/li3_uploadable

此包的最新版本(dev-master)没有可用的许可信息。

此插件仅通过HTTP POST上传文件,因为它依赖于$_FILES来获取源路径。

安装: 73

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

类型:lithium-library

dev-master 2014-09-18 22:26 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:54:27 UTC


README

此插件仅通过HTTP POST上传文件,因为它依赖于$_FILES来获取源路径。

依赖关系

li3_behaviors by @jails

安装

将代码克隆到您的库目录中(或将其作为子模块添加)

cd libraries
git clone git@github.com:housni/li3_uploadable.git

如果您打算使用 Imagine(由@avalanche123)策略,您还需要执行以下操作

git submodule init && git submodule update

/app/config/bootstrap/libraries.php 中包含库

Libraries::add('li3_uploadable');

配置

<?php
/**
 * I usually put this in:
 *     app/config/bootstrap/uploadable.php
 * and then I include this file in libraries.php
 */
use li3_uploadable\extensions\storage\Uploadable;

Uploadable::config([
	/**
	 * Themes
	 *
	 * Uploads a zip file and then, using processors, it extracts the contents
	 * to `{:application}/views/themes/{:filename}` and deletes
	 * the zip file via a filter.
	 */
	'themeArchive' => [
		'adapter' => 'CompressionArchive',
		'strategies' => ['ZipArchive'],
		'processors' => [
			'extract' => ['{:application}/views/themes']
		],
		'save' => '{:application}/views/themes/{:filename}.zip',
		'remove' => '{:application}/views/themes/{:filename}',
		'filters' => [
			function($self, $params, $chain) {
				$extracted = $chain->next($self, $params, $chain);
				if (file_exists($extracted['extract']['source'])) {
					unlink($extracted['extract']['source']);
				}
				return $extracted;
			}
		]
	],

	/**
	 * Uploads a preview image of the theme with two different styles.
	 * Assuming the name of the image uploaded is `preview.png`, this will yield:
	 * 1. `thumb_preview.png` at 250 x 250px
	 * 2. `full_preview.png` at 1024 x 1024px
	 *
	 * The `Imagine` strategy depends on Imagine: https://github.com/avalanche123/Imagine
	 */
	'themePreview' => [
		'adapter' => 'Image',
		'strategies' => [
			'Imagine' => [
				'implementation' => 'Imagick',
				'styles' => [
					'thumb' => '250x250',
					'full' => '1280x1024'
				],
				'quality' => 100
			]
		],
		'save' => '{:application}/webroot/uploads/themes/{:id}/{:style}_{:basename}',
		'remove' => '{:application}/webroot/uploads/themes/{:id}',
		'url' => 'uploads/themes/{:id}/{:style}_{:basename}',
		'default' => 'img/missing-theme.png'
	],
]);
?>

用法

下面的模型假设您正在接受一个 enctype="multipart/form-data" 表单,其中名为 directorypreview 的字段可以接受文件。

<?php
/**
 * app/models/Theme.php
 */
namespace app\models;

use li3_behaviors\data\model\Behaviors;

class Theme extends \lithium\data\Model {

	use Behaviors;

	protected $_actsAs = [
		'Uploadable' => [
			'fields' => [
				'themeArchive' => 'directory',
				'themePreview' => 'preview'
			],
			/**
			 * You can override placeholders
			 */
			'placeholders' => [
				'model' => 'MyModelName'
			]
		]
	];
}
?>

在你的视图中

// This will output the text value stored in themes.directory
<?= $theme->directory; ?>

// This will output the 250 x 250px thumb_preview.png url
<img src="<?= $theme->preview->url('thumb'); ?>" alt="Preview of theme">

验证

查看 app/config/bootstrap/validators.php

更多适配器 => 策略

  • MP3 => ID3
  • 视频 => FFmpeg
  • PDF => wkhtmltopdf

待办事项

  • 使代码符合 li3_quality
  • 添加更多文档
  • 添加测试