项目作者: artgris

项目描述 :
Easier Symfony Media Management
高级语言: PHP
项目地址: git://github.com/artgris/MediaBundle.git
创建时间: 2017-05-08T13:53:54Z
项目社区:https://github.com/artgris/MediaBundle

开源协议:MIT License

下载


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

demo-gif

Getting Started

  • Download the files:

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

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

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

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

  1. use Artgris\Bundle\MediaBundle\Form\Validator\Constraint as MediaAssert; // optionnal, to force image files
  2. // ...
  3. /**
  4. * @var string
  5. * @ORM\Column(type="string")
  6. * @Assert\NotNull()
  7. */
  8. private $image;
  9. /**
  10. * @var Collection|string[]
  11. * @ORM\Column(type="simple_array", nullable=true)
  12. * @MediaAssert\Image()
  13. */
  14. private $gallery;

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

  1. use Artgris\Bundle\MediaBundle\Form\Type\MediaType;
  2. use Artgris\Bundle\MediaBundle\Form\Type\MediaCollectionType;
  3. // ...
  4. $builder
  5. ->add('image', MediaType::class, [
  6. 'conf' => 'default'
  7. ])
  8. ->add('gallery', MediaCollectionType::class, [
  9. 'conf' => 'default'
  10. ]);

Options:

MediaType:

  • 'conf' => 'yourconf' (required) specifies a configuration defined in the FileManager. For more informations about media configurations, refer to FileManagerBundle’s documentation
  • 'extra' => [] (only with FileManagerBundle Service Configuration) 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
  • 'crop_options' => array if allow_crop is set to true, allows to specify extra crop options. The default options:
  1. 'crop_options' => [
  2. 'display_crop_data' => true, // will display crop box informations (x, y, width, height, and ratio if there is one)
  3. 'allow_flip' => true, // allows to flip the image vertically and horizontally
  4. 'allow_rotation' => true, // allows to rotate the image (90 degrees)
  5. 'ratio' => false // force a crop ratio. E.g 16/9
  6. ],

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:

  1. 'entry_options' => [
  2. 'display_file_manager' => false
  3. ]

Changing cropping path

add config/packages/artgris_media.yaml

  1. artgris_media:
  2. cropped_path: "cropped/" #default value

Demo Application

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