Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/artgris/mediabundle

Easier Symfony Media Management
https://github.com/artgris/mediabundle

bundle management media symfony symfony-bundle

Last synced: 3 months ago
JSON representation

Easier Symfony Media Management

Awesome Lists containing this project

README

        

## artgris/MediaBundle - Easier Symfony Media Management

> Repository is no longer maintained. A more modern alternative of this bundle exists: Arkounay ux-media bundle - Symfony UX async document upload type using ArtgrisFileManager : https://github.com/Arkounay/ux-media

### Prerequisites

- symfony >= 4.1
- [artgris/FileManagerBundle](https://github.com/artgris/FileManagerBundle#add-following-configuration-)
- Assets:
- CSS: [bootstrap 4 or 5](http://getbootstrap.com/) and [Font Awesome](http://fontawesome.io/)
- JS: [jQuery](https://jquery.com/), [ninsuo/symfony-collection](https://github.com/ninsuo/symfony-collection) and [jQuery UI](https://jqueryui.com/)

![demo-gif](https://github.com/artgris/MediaBundle/raw/master/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:

- CSS (**requires [bootstrap](http://getbootstrap.com/) and [Font Awesome](http://fontawesome.io/)**):

```twig
{# Bootstrap 4 #}

{# or Bootstrap 5 #}

{# Font Awesome #}

{# Artgris FileManager #}

{# Import fengyuanchen/cropper #}

{# Then the default bundle's CSS #}

```

- JS (**requires [jQuery](https://jquery.com/), [ninsuo/symfony-collection](https://github.com/ninsuo/symfony-collection) and [jQuery UI](https://jqueryui.com/)**):

```twig
{# jQuery #}

{# Bootstrap 4 #}


{# or Bootstrap 5 #}

{# Jqueri UI #}

{# jquery.collection.js #}

{# Import fengyuanchen/cropper #}

{# 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.

```php

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.

```php
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](https://github.com/artgris/FileManagerBundle#add-following-configuration-)
- `'extra' => []` (only with FileManagerBundle Service Configuration) [Extra Url parameters injections](https://github.com/artgris/FileManagerBundle/blob/master/Resources/doc/book/2-service-configuration.md#extra-url-parameters-injections)
- `'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](https://github.com/fengyuanchen/cropper)
- `'crop_options' => array` if `allow_crop` is set to `true`, allows to specify extra crop options. The default options:

```php
'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:**

- `'conf' => 'yourconf'` (**required**) specifies a configuration defined in the FileManager. For more informations about media configurations, [refer to FileManagerBundle's documentation](https://github.com/artgris/FileManagerBundle#add-following-configuration-)

Some [ninsuo/symfony-collection](https://github.com/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:

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

### Changing cropping path

add config/packages/artgris_media.yaml

artgris_media:
cropped_path: "cropped/" #default value


### Demo Application

[MediaBundleDemo](https://github.com/artgris/MediaBundleDemo) is a complete Symfony application created to showcase MediaBundle features.