arkounay/image-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (2.1.1) of this package.

Easier image upload form bundle with Doctrine

Installs: 393

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 1

Open Issues: 0

Type:symfony-bundle

2.1.1 2017-01-30 07:49 UTC

This package is auto-updated.

Last update: 2022-02-12 16:14:18 UTC


README

Arkounay Image Bundle - Symfony Easier Doctrine image management

alt tag

Getting Started

  • Download the files:

      composer require arkounay/image-bundle
    
  • In AppKernel.php add the bundle:

      new Arkounay\ImageBundle\ArkounayImageBundle()
    
  • Then, run the following command:

      php bin/console assets:install 
    
  • In your twig template, you will then need to import the required assets:

    • CSS:

        <link rel="stylesheet" href="{{ asset('bundles/arkounayimage/arkounay_image_bundle.css') }}">
      
    • JS (requires jQuery, ninsuo/symfony-collection and optionally bootstrap, Font Awesome and jQuery UI):

        {# Import jQuery: #}
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
           
        {# Then the default bundle's JavaScript: #}
        {% include '@ArkounayImage/assets/include_js.html.twig' %}
      
  • In routing.yml, you will need to import the Ajax route:

       arkounay_image:
           resource: "@ArkounayImageBundle/Resources/config/routing.yml"
    
  • In config.yml, add the following Doctrine types:

      doctrine:
          dbal:
              types:
                  json_image: Arkounay\ImageBundle\Type\JsonImageType
                  json_images: Arkounay\ImageBundle\Type\JsonImagesType
    

Usage

In an entity, you can now add the new json_image types:

/**
 * @var Image
 * @ORM\Column(type="json_image")
 */
private $image;

/**
 * @var ArrayCollection|Image[]
 * @ORM\Column(type="json_images")
 */
private $imageCollection;

public function __construct()
{
    $this->imageCollection = new ArrayCollection();
}

You can bound these fields to a form using its corresponding type:

use Arkounay\ImageBundle\Form\JsonImagesType;
use Arkounay\ImageBundle\Form\JsonImageType;

// ... 

$builder
    ->add('image', JsonImageType::class)
    ->add('imageCollection', JsonImagesType::class);

Options:

JsonImageType:

  • 'allow_alt' => true allows the user to specify an alt
  • 'path_readonly' => false prevents the user from manually changing the path (it only adds a "readonly" attribute to the corresponding HTML input)

JsonsImageType:

Some ninsuo/symfony-collection's options are available directly:

  • 'min' => 0
  • 'max' => 100
  • 'init_with_n_elements' => 1
  • 'add_at_the_end' => true

About the form HTML theme

Include bootstrap's theme

{% form_theme form ':admin/includes:bootstrap_3_layout.html.twig' %}

To override the widget theme, check Resources/views/forms/fields.html.twig.

Other

  • By default, only ROLE_ADMIN can upload images. You can specify roles in config.yml, ie:

      arkounay_image:
          roles: ['ROLE_SUPER_ADMIN']
    
  • This bundle works with EasyAdminBundle, you can for example add multiple images by specifying the proper type:

      { property: 'imageCollection', type: 'Arkounay\ImageBundle\Form\JsonImagesType', type_options: {allow_add: true} }