mkurzeja / image-bundle
图片处理包
v1.0.3
2013-08-15 15:37 UTC
Requires
- php: >=5.3.0
- ext-gd: *
This package is auto-updated.
Last update: 2024-09-29 04:08:40 UTC
README
GregwarImageBundle
为 Symfony2 和 Twig 提供简单的图片操作和 API
安装
步骤 1: 下载 GregwarImageBundle
使用 vendors 脚本
将以下行添加到您的 deps
文件中
[GregwarImageBundle]
git=http://github.com/Gregwar/ImageBundle.git
target=/bundles/Gregwar/ImageBundle
现在,运行 vendors 脚本来下载包
$ php bin/vendors install
使用子模块
如果您更喜欢使用 git 子模块,请运行以下命令
$ git submodule add git://github.com/Gregwar/ImageBundle.git vendor/bundles/Gregwar/ImageBundle $ git submodule update --init
使用 Composer
将以下内容添加到您的 composer.json
文件的 "require" 部分
"gregwar/image-bundle": "dev-master"
您也可以选择版本号(标签、提交...)
并更新您的依赖项
php composer.phar update
步骤 2: 配置自动加载器
如果您使用 composer,则可以跳过此步骤。
将其添加到您的 autoload.pp
文件中
<?php ... 'Gregwar' => __DIR__.'/../vendor/bundles',
步骤 3: 启用包
在您的 app/AppKernel.php
文件中注册包
<?php ... public function registerBundles() { $bundles = array( ... new Gregwar\ImageBundle\GregwarImageBundle(), ... ); ...
步骤 4: 配置包并设置目录
将以下配置添加到您的 app/config/config.yml
文件中
gregwar_image: ~
如果您想自定义缓存目录名称,可以指定它
gregwar_image:
cache_dir: my_cache_dir
创建缓存目录并更改权限,以便 web 服务器可以写入它
mkdir web/cache
chmod 777 web/cache
您还可以禁用如果指定的文件不存在时抛出的异常
gregwar_image:
throw_exception: false
使用
基础知识
此包基于 Gregwar's Image 类,并提供了简单但强大的 Twig 扩展。例如,您可以这样使用它
<img src="{{ image('linux.jpg').resize(100,100).negate }}" />
就是这样!如果缓存文件不存在,辅助工具将自动创建它。
可用方法与 Gregwar's Image 相同。
您还可以使用逻辑文件名来使用包资源
<img src="{{ image('@AcmeDemoBundle/Resources/images/linux.jpg').resize(100,100).negate }}" />
使用 Image API
图像实例还提供了一个简单的 API,您可以调用一些方法来获取关于处理图像的信息
Image width: {{ image('linux.jpg').width }}px
在控制器中操作图像
图像处理器可以通过名为 image.handling 的服务访问。因此,您可以在控制器中这样做
<?php ... $this->get('image.handling')->open('linux.jpg') ->grayscale() ->rotate(12) ->save('out.jpg')
要求
GregwarImageBundle
需要 PHP 服务器上安装 GD 和 exif 扩展
许可证
此包采用 MIT 许可证