pagewiser / idn-nette
IDN服务器Nette辅助函数和宏
v0.2
2015-09-15 12:04 UTC
Requires
- php: >=5.3.0
- nette/nette: >=2.2.0
- pagewiser/idn-client: v0.2
Replaces
This package is not auto-updated.
Last update: 2024-09-14 16:55:27 UTC
README
IDN扩展插件用于Nette。支持上传和图片处理,为latte添加了图片处理宏。
安装
要将此扩展注册到Nette中,请将latte宏扩展添加到您的配置文件中。
Nette 2.3.x
extensions:
idn: Pagewiser\Idn\Nette\Bridges\Nette\Nette23Extension
nette:
latte:
macros:
- Pagewiser\Idn\Nette\IdnMacros::install
idn:
apiHost: 'http://idnapi.pwlab.tk/'
apiKey: 'your-api-key'
apiSecret: 'your-api-secret'
imageHost: '//storage.pwlab.tk/'
lazy: TRUE
client: 'client-name'
profiler: TRUE
dirAliases:
profilePhoto: 'user/photo'
Nette 2.2.x
services:
nette.latteFactory:
setup:
- Pagewiser\Idn\Nette\IdnMacros::install(::$service->getCompiler())
然后您需要配置您的IDN账户和API类。
idn:
class: Marten\NetteIdn\Api('username', 'password')
基本用法
图片上传
展示者
/**
* @var \Pagewiser\Idn\Nette\Api $idnApi
* @inject
*/
public $idnApi
public function formSuccess(Form $form)
{
$values = $form->getValues();
try
{
$uploadedFile = $this->idnApi->upload('photos/'.$values['id'].'.jpg', $values['file']->getTemporaryFile());
if ($uploadedFile['status'] !== 'success')
{
// Rollback
}
// Commit
}
catch (\Api\Exception\ApiException $e)
{
$form->addError($e->getMessage());
}
}
如果您使用相同的名称重新上传图片,IDN服务器将自动清除该图片的缓存。仍然建议使用生成的文件名,因为它将处理浏览器中的本地缓存。
图片显示
Latte
<img src="{idn 'photos', $job['logo'], '80x80'}">
Latte宏有4个参数。
- 存储图片的目录
- 文件名
- 所需大小
- 转换选项:** f - 使用图片填充大小。图片可以大于提供的大小。 ** e - 图片的精确大小。图片将被拉伸。 ** c - 图片的精确大小。图片将被裁剪。 ** b - 图片的精确大小。图片将被调整大小以适应大小,背景将被添加。 ** a - 面部检测。图片将被调整到所需大小,并将放大以在图片上聚焦面部。
删除图片
try
{
try
{
$this->idnApi->delete('photos/', $photoName);
}
catch (\Pagewiser\Idn\Client\FileNotFoundException $ex)
{
// File not found, was already deleted?
}
}
catch (\Pagewiser\Idn\Client\OperationException $ex)
{
$this->flashMessage($ex->getMessage(), 'warning');
}