2017 © Pedro Peláez
 

symfony-bundle photo-bundle

Symfony ZertzPhotoBundle

image

zertz/photo-bundle

Symfony ZertzPhotoBundle

  • Saturday, January 24, 2015
  • by Zertz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ZertzPhotoBundle

This bundle provides an easy to use platform to automatically resize photos on upload and display them back to the user., (*1)

Features

  • Photo management
  • Customizable thumbnails
  • Resizing

Upcoming features

  • Batch resize from the command-line

1) Requirements

  1. Symfony 2.2 Although it wasn't tested, it should also work with 2.1
  2. Doctrine 2

2) Installation

In composer.json, add:, (*2)

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

Run an update to install the bundle:, (*3)

php composer.phar update zertz/photo-bundle

3) Configuration

AppKernel.php

Enable the bundle:, (*4)

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

config.yml

Add the following lines:, (*5)

# 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

Note, (*6)

directory and domain keys are required. For JPEG photos, the quality setting is customizable, but defaults to 70., (*7)

Formats, (*8)

formats are customizable and the size property may contain one of, or both, width and height. The photo will automatically be resized and the aspect ratio is always maintained. For JPEG photos, the optional quality setting overrides the global quality setting., (*9)

If no formats are defined, then the original photo is simply uploaded., (*10)

Extend the Photo class

This bundle provides the basics for persisting a photo object to the database. It is your role however to extend the Photo class and add any fields you deem useful., (*11)

To get started, your entity class should look like this:, (*12)

<?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;
    }
}

Finally, run the following command to update the database schema:, (*13)

php app/console doctrine:schema:update --force

4) Usage

The bundle provides simple helpers to upload and display photos., (*14)

In a form, add the format option and the bundle will automatically take care of the rendering:, (*15)

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

In a Twig template, add the path filter and specify the format to display:, (*16)

<img src="{{ photo|path('medium') }}">

5) FAQ

  • Why doesn't the photo preview appear in my form?

If the photo doesn't appear at all, then the format request in your form doesn't exist. Otherwise, if the format exists, you should regenerate thumbnails manually using the following command:, (*17)

php app/console zertz:photo:generate myformat

or, (*18)

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

The Versions

24/01 2015

dev-master

9999999-dev http://github.com/Zertz/ZertzPhotoBundle

Symfony ZertzPhotoBundle

  Sources   Download

MIT

The Requires

 

photo management