zertz/photo-bundle

Symfony ZertzPhotoBundle

安装: 14

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2015-01-24 02:26 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:51:58 UTC


README

此包提供了一个易于使用的平台,用于在上传时自动调整照片大小,并将其显示回用户。

特性

  • 照片管理
  • 可定制缩略图
  • 调整大小

即将推出的功能

  • 从命令行批量调整大小
  1. 要求
  1. Symfony 2.2 尽管未经测试,它应该也能与2.1兼容
  2. Doctrine 2
  1. 安装

在 composer.json 中添加

"require": {
    "zertz/photo-bundle": "dev-master"
}

运行更新以安装包

php composer.phar update zertz/photo-bundle
  1. 配置

AppKernel.php

启用包

public function registerBundles()
{
    $bundles = array(
        new Zertz\PhotoBundle\ZertzPhotoBundle(),
    );
}

config.yml

添加以下行

# Twig Configuration
twig:
    form:
        resources:
            - ZertzPhotoBundle:Form:fields.html.twig

# ZertzPhotoBundle Configuration
zertz_photo:
    directory: /path/to/save/to
    domain: http://img.yourdomain.com
    quality: 70
    formats:
        small:
            size: { width: 140 , height: 87 }
        medium:
            size: { height: 175 }
        large:
            size: { width: 635 }
            quality: 90

注意

directorydomain 键是 必需的。对于 JPEG 照片,quality 设置是可定制的,但默认为 70。

格式

formats 是可定制的,而 size 属性可以包含 widthheight 之一或两者。照片将自动调整大小,并且总是保持宽高比。对于 JPEG 照片,可选的 quality 设置会覆盖全局 quality 设置。

如果没有定义 formats,则仅上传原始照片。

扩展 Photo 类

此包为将照片对象持久化到数据库提供基本功能。然而,您需要扩展 Photo 类并添加您认为有用的任何字段。

要开始,您的实体类应如下所示

<?php
// src/Acme/PhotoBundle/Entity/Photo.php

namespace Acme\PhotoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zertz\PhotoBundle\Entity\Photo as BasePhoto;

/**
 * @ORM\Entity
 * @ORM\Table(name="zertz_photo")
 */
class Photo extends BasePhoto
{
    /**
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    public function __construct() {
        parent::__construct();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

最后,运行以下命令以更新数据库模式

php app/console doctrine:schema:update --force
  1. 用法

此包提供简单的助手来上传和显示照片。

在表单中添加 format 选项,包将自动处理渲染

->add('file', 'file', array(
    'format' => 'small',
))

在 Twig 模板中添加 path 过滤器并指定要显示的格式

<img src="{{ photo|path('medium') }}">
  1. 常见问题解答 (FAQ)
  • 为什么我的表单中不显示照片预览?

如果照片根本不显示,那么您表单中的格式请求不存在。如果格式存在,则您应该使用以下命令手动重新生成缩略图

php app/console zertz:photo:generate myformat

php app/console zertz:photo:generate --all