Developed with love by KnpLabs Hire us for your project!
44

MediaBundle

by artgris

Easier Symfony Media Management

artgris/MediaBundle - Easier Symfony Media Management

Prerequisites

demo-gif

Getting Started

  • Download the files:

    composer require artgris/media-bundle
    
  • In AppKernel.php add the bundle:

    new Artgris\Bundle\MediaBundle\ArtgrisMediaBundle()
    
  • Then, run the following command:

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

        {# Bootstrap #}
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
        {# Font Awesome #}
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
        {# Artgris FileManager #}
        <link rel="stylesheet" href="{{ asset('bundles/artgrisfilemanager/libs/blueimp-file-upload/css/jquery.fileupload.css') }}">
        {# Import fengyuanchen/cropper #}
        <link rel="stylesheet" href="{{ asset('bundles/artgrismedia/libs/cropperjs-1.4.1/cropper.min.css') }}">
        {# Then the default bundle's CSS #}
        <link rel="stylesheet" href="{{ asset('bundles/artgrismedia/css/media.css') }}">
    {# jQuery #}
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    {#  Bootstrap #}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
    {#  Jqueri UI #}
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
    {# jquery.collection.js #}
    <script type="text/javascript" src="{{ asset('js/jquery.collection.js') }}"></script>
    {# Import fengyuanchen/cropper #}
    <script src="{{ asset('bundles/artgrismedia/libs/cropperjs-1.4.1/cropper.min.js') }}"></script>
    {# Then the default bundle's JavaScript: #}
    {% include '@ArtgrisMedia/assets/include_js.html.twig' %}
  • In routing.yml, you will need to import the Ajax route: yaml artgris_media: resource: "@ArtgrisMediaBundle/Resources/config/routing.yml" prefix: /admin

Usage

In an entity, add the path attributes as string.
You can also use doctrine's types such as simple_array, array, json for collections.


use Artgris\Bundle\MediaBundle\Form\Validator\Constraint as MediaAssert; // optionnal, to force image files

// ...

/**
 * @var string
 * @ORM\Column(type="string")
 * @Assert\NotNull()
 */
private $image;

/**
 * @var Collection|string[]
 * @ORM\Column(type="simple_array", nullable=true)
 * @MediaAssert\Image()
 */
private $gallery;

Then, use a form builder and assigne the MediaType class for a single file, or the MediaCollectionType for multiple files.

use Artgris\Bundle\MediaBundle\Form\Type\MediaType;
use Artgris\Bundle\MediaBundle\Form\Type\MediaCollectionType;

// ... 

$builder
    ->add('image', MediaType::class, [
        'conf' => 'default'
    ])
    ->add('gallery', MediaCollectionType::class, [
        'conf' => 'default'
    ]);

Options:

MediaType:
- 'conf' => 'yourconf' (**required**) specifies a configuration defined in the FileManager. For more informations about media configurations, refer to FileManagerBundle's documentation
- 'readonly' => false prevents the user from manually changing the path (it only adds a "readonly" attribute to the corresponding HTML input)
- 'allow_crop' => true allows the user to edit the image using fengyuanchen/cropper
- 'crop_options' => array if allow_crop is set to true, allows to specify extra crop options. The default options:

'crop_options' => [
    'display_crop_data' => true,    // will display crop box informations (x, y, width, height, and ratio if there is one)
    'allow_flip' => true,           // allows to flip the image vertically and horizontally
    'allow_rotation' => true,       // allows to rotate the image (90 degrees)
    'ratio' => false                // force a crop ratio. E.g 16/9
],

MediaCollectionType:

Some ninsuo/symfony-collection's options are available directly:
- 'min' => 0
- 'max' => 100
- 'init_with_n_elements' => 1
- 'add_at_the_end' => true

Like regular collections, you can edit entries options, i.e to enable alts:

'entry_options' => [
    'display_file_manager' => false
]

Gregwar Image Bundle Integration

This bundle relies on Gregwar/ImageBundle to crop, mirror and scale images.

If you need to manually crop image in twig (if they are too large for example), instead of using the image and web_image functions, you should gImage, which works the same as image but improves compatibility.

E.g:

{{ gImage(news.image).zoomCrop(100, 100) }}

Changing cropping path

add config/packages/artgris_media.yaml

artgris_media:
    cropped_path: "cropped/" #default value

Demo Application

MediaBundleDemo is a complete Symfony application created to showcase MediaBundle features.

artgris_media:
cropped_path: cropped/
  • Update composer.json
    By web-flow, 2 months ago
  • Update composer.json
    By web-flow, 2 months ago
  • Larger modal
    By web-flow, 5 months ago
  • add MediaBundleDemo link
    By web-flow, 8 months ago
  • add doctrine-bundle ~2.0
    By artgris, 8 months ago
  • fix rezising with a filemanager conf (dir) that doesn't end with /
    By artgris, 8 months ago
  • fix collection on single media bug
    By web-flow, 9 months ago
  • Improved overridability for extra serializable fields
    By Arkounay, 10 months ago
  • Fix duplicate block
    By Arkounay, 10 months ago
  • Improve overridability by adding blocks and removing macro + fix z-index hover croppable
    By Arkounay, 10 months ago
  • Trigger .change() events
    By web-flow, 10 months ago
  • update readme
    By artgris, 1 year ago
  • Merge remote-tracking branch 'origin/master'
    By artgris, 1 year ago
  • fix - add dependencies #38
    By artgris, 1 year ago
  • :pencil2: fix typo on translation (#39)
    By artgris, 1 year ago
  • feat - add dependencie
    By artgris, 1 year ago
  • fix - conf service
    By artgris, 1 year ago
  • feat - Changed cropping path #37
    By artgris, 1 year ago
  • fix - default value in crop_options
    By artgris, 1 year ago
  • fix - data-backdrop="static"
    By artgris, 1 year ago
  • fix - crop url
    By artgris, 1 year ago
  • update - readme
    By artgris, 1 year ago
  • update - readme
    By artgris, 1 year ago
  • update - readme
    By artgris, 1 year ago
  • feat - fa 5
    By artgris, 1 year ago
  • fix - media selector
    By artgris, 1 year ago
  • Update README.md
    By web-flow, 1 year ago
  • dragover file
    By web-flow, 1 year ago
  • Merge pull request #36 from GuillaumeEgloff/inte
    By web-flow, 1 year ago
  • Correction inté bootstrap
    By Guillaume Egloff, 1 year ago