2017 © Pedro PelĂĄez
 

symfony-bundle image-bundle

A bundle providing fields for image upload with jquery upload and image cropping with jcrop for symfony2

image

comur/image-bundle

A bundle providing fields for image upload with jquery upload and image cropping with jcrop for symfony2

  • Thursday, September 14, 2017
  • by comur
  • Repository
  • 15 Watchers
  • 103 Stars
  • 39,060 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 81 Forks
  • 53 Open issues
  • 25 Versions
  • 6 % Grown

The README.md

ComurImageBundle

Introduction

Image upload / crop bundle for Symfony2, (*1)

This bundle helps you easily create an image upload / crop field in your forms. You don't need to use any type of generator or there is no other requirements. It uses bootstrap to make it look well but you can use any other css to customize it., (*2)

It uses beautiful Jquery File Upload to upload files (original UploadHandler has been modified to add namespace and a new config parameter to generate random filenames) and JCrop to let you crop uploaded images., (*3)

Works perfectly with ComurContentAdminBundle. If you don't know what it is, take a look and you will be surprised ! This bundle helps you create beautiful admins with inline editing capabilities with ease !!, (*4)

If this bundle helps you reduce time to develop, you can pay me a cup of coffee ;), (*5)

coffee, (*6)

coffee, (*7)

Which version use

v SF v Bundle
2.x 1.x
3.x 2.x
4.x 2.x

:warning: Use 1.X releases for compatibility with bootstrap 2.x., (*8)

:warning: Compatibility with bootstrap 2.X is no more maintained, (*9)

:warning: I won't maintain Symfony 2 anymore, I will only merge PR on 1.2 branch for compatibility with SF2. If you need something you can always create a PR and I will merge it., (*10)

:warning: Animated gif cropping is only available with imagick, (*11)

Changelog

Please see on CHANGELOG.md, (*12)

Screen shots

Here are some screen shots since i didn't have time to put a demo yet:, (*13)

Simple Image widget

alt tag, (*14)

alt tag, (*15)

Upload image screen

alt tag, (*16)

Select image from library screen

alt tag, (*17)

Crop image screen

alt tag, (*18)

alt tag, (*19)

Dependencies

Bundle uses following packages in assets (see in bower.json) :, (*20)

Package version optional
blueimp-file-upload ^9.31.0 false
Jcrop jcrop#0.9.15 false
jquery ^3.4.1 true
holderjs ^2.9.6 true
bootstrap ^4.3.1 true
Font-Awesome FortAwesome/Font-Awesome#^5.8.2 true

You can disable auto including of these assets by using modal template inclusion parameters (see step4), (*21)

Installation

Applications that use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute:, (*22)

$ composer require comur/content-admin-bundle

Step 2: Enable other bundles

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:, (*23)

// config/bundles.php

return [
            // ...
            Comur\ImageBundle\ComurImageBundle::class => ['all' => true],
            FOS\JsRoutingBundle\FOSJsRoutingBundle::class => ['all' => true],
            JMS\TranslationBundle\JMSTranslationBundle::class => ['all' => true],
        ];

Step 3: Create routing files

Two of the bundles provides routes, which must be enabled by creating these files:, (*24)

# config/routes/fos_js_routing.yaml
fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing-sf4.xml"

```yaml, (*25)

config/routes/comur_image.yaml

comur_image: resource: "@ComurImageBundle/Resources/config/routing.yml" prefix: /, (*26)



Applications that don't use Symfony Flex ---------------------------------------- ### Step 1: Download the Bundle Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle: ```console $ composer require comur/content-admin-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*27)

Step 2: Enable the Bundle and dependency bundles

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:, (*28)

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Comur\ImageBundle\ComurImageBundle(),
            new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
            new JMS\TranslationBundle\JMSTranslationBundle(),
        ];

        // ...
    }

    // ...
}

Step 3: Add this route to your routing.yml:

    # app/config/routing.yml
    fos_js_routing:
      resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

    comur_image:
        resource: "@ComurImageBundle/Resources/config/routing.yml"
        prefix:   /

Step 4: Add Modal template after body tag of your layout:

  <body>
  {% include "ComurImage/Form/croppable_image_modal.html.twig"%}
  

  </body>

Note: This template includes many script and styles including jquery and bootstrap. You can use following parameters to avoid jquery and/or bootstrap being included:, (*29)

{% include "ComurImage/Form/croppable_image_modal.html.twig" with {'include_fontawesome': false, 'include_jquery': false, 'include_bootstrap': false, 'bootstrap_version': 4} %}

:warning: bootstrap_version is used for bootstrap 4 compatibility issues. If you include bootstrap using bundle, it will use bootstrap 4 and set it to 4 automatically. If you need to use bootstrap 3, remove bootstrap_version parameter or put 3., (*30)

:">Step 5: Do not forget to put FOSJSRoutingBundle script in your :

  <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
  <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>

:boom: That's it !, (*31)

Configuration

All parameters are optional:, (*32)

  comur_image:
    config:
      cropped_image_dir: 'cropped'
      thumbs_dir: 'thumbnails'
      media_lib_thumb_size: 150
      public_dir: '%kernel.project_dir%/public'
      translation_domain: 'ComurImageBundle'
      gallery_thumb_size: 150
      gallery_dir: 'gallery'

cropped_image_dir

It's used to determine relative directory name to put cropped images (see above)., (*33)

Default value: 'cropped', (*34)

thumbs_dir

It's used to determine relative directory name to put thumbnails (see above)., (*35)

Default value: 'thumbnails', (*36)

media_lib_thumb_size

It's used to determine thumbnails size in pixels (squares) used in media library., (*37)

Default value: 150, (*38)

public_dir

Path to your public directory. It's used to check thumb existence in thumb twig helper and your upload dir will be relative to this dir (since security fix #80), (*39)

Default value: '%kernel.project_dir%/public', (*40)

translation_domain

Domain name for translations. For instance two languages are provided (en & fr). To override the domain name, change this parameter to whatever you want., (*41)

Default value: 'ComurImageBundle', (*42)

That's the image size in pixels that you want to show in gallery widget. Gallery widget will automaticaly create square thums having this size and show them in the gallery widget., (*43)

Default value: 150, (*44)

That's the gallery directory name. The widget will store all gallery images in a specific directory inside the root directory that you will give when you will add the widget to your forms., (*45)

For eg. if you put 'uploads/images' as webDir when you add the widget, gallery images will be stored in 'uploads/images/[gallery_dir]'. This is added to make gallery use easier so you don't have to add new functions to your entities to get gallery dirs., (*46)

Usage

There are two widgets provided with this bundle. They both have exacly same config parameters., (*47)

Image widget

Use widget in your forms (works with SonataAdmin too) to create a simple image field :, (*48)

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      // get your entity related with your form type
      $myEntity = $builder->getForm()->getData();
      ...
      ->add('image', CroppableImageType::class, array(
          'uploadConfig' => array(
              'uploadRoute' => 'comur_api_upload',     //optional
              'uploadDir' => $myEntity->getUploadDir(), // required - see explanation below (you can also put just a dir name relative to your public dir)
              // 'uploadUrl' => $myEntity->getUploadRootDir(),       // DEPRECATED due to security issue !!! Please use uploadDir. required - see explanation below (you can also put just a dir path)
              'webDir' => $myEntity->getUploadDir(),        // required - see explanation below (you can also put just a dir path)
              'fileExt' => '*.jpg;*.gif;*.png;*.jpeg',   //optional
              'maxFileSize' => 50, //optional
              'libraryDir' => null,             //optional
              'libraryRoute' => 'comur_api_image_library', //optional
              'showLibrary' => true,             //optional
              'saveOriginal' => 'originalImage',      //optional
              'generateFilename' => true      //optional
          ),
          'cropConfig' => array(
              'disable' => false,      //optional
              'minWidth' => 588,
              'minHeight' => 300,
              'aspectRatio' => true,         //optional
              'cropRoute' => 'comur_api_crop',   //optional
              'forceResize' => false,       //optional
              'thumbs' => array(           //optional
                array(
                  'maxWidth' => 180,
                  'maxHeight' => 400,
                  'useAsFieldImage' => true  //optional
                )
              )
          )
      ))

You need to create a field (named image in this example but you can choose whatever you want):, (*49)

  // YourBundle\Entity\YourEntity.php

  ...

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $image;

    ...

And create your functions in your entity to have directory paths, for ex :, (*50)

  /* DEPRECATED, Use upload dir
  public function getUploadRootDir()
  {
      // absolute path to your directory where images must be saved
      return __DIR__.'/../../../../../web/'.$this->getUploadDir();
  }*/

  /* Directory relative to your public dir (see public_dir in configuration)
  public function getUploadDir()
  {
      return 'uploads/myentity';
  }

  public function getWebPath()
  {
      return null === $this->image ? null : '/'.$this->getUploadDir().'/'.$this->image;
  }

That's all ! This will add an image preview with an edit button in your form and will let you upload / select from library and crop images without reloading the page., (*51)

To save original image path, you have to create another field and name it same as saveOriginal parameter:, (*52)

  // YourBundle\Entity\YourEntity.php

  


  /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $originalImage;

    


I will put a demo soon
, (*53)

Use widget in your forms (works with SonataAdmin too) to create a sortable list of images (so a gallery :)) stored in an array typed field :, (*54)

  ->add('gallery', CroppableGalleryType::class, array(
    //same parameters as comur_image
  ))

And create your array typed field for storing images in it. Doctrine (or other ORM) will serialize this field to store it as string in the DB., (*55)

  // YourBundle\Entity\YourEntity.php

  ...

    /**
     * @ORM\Column(type="array", nullable=true)
     */
    protected $gallery;

    ...

And create your functions in your entity to have directory paths, for ex :, (*56)

    /* DEPRECATED, Use upload dir
    public function getUploadRootDir()
    {
        // absolute path to your directory where images must be saved
        return __DIR__.'/../../../../../web/'.$this->getUploadDir();
    }*/

    public function getUploadDir()
    {
        return 'uploads/myentity';
    }

    public function getWebPath()
    {
        return null === $this->image ? null : '/'.$this->getUploadDir().'/'.$this->image;
    }

That's all ! This will create a widget like on the following image when you will use it in your forms. You can also reorder them since php serialized arrays are ordered:, (*57)

Gallery images will be stored in uploadDir / gallery_dir (default is gallery). Cropped images will be stored in uploadDir / gallery_dir / cropped_dir (same as image widget) and thumbs in uploadDir / gallery_dir / cropped_dir / thumb_dir with specified width. So if you pu, (*58)

uploadConfig

uploadRoute (optional)

Route called to send uploaded file. It's recommended to not change this parameter except if you know exactly what you do., (*59)

Default value: comur_api_upload, (*60)

uploadDir (required)

Since 2.0.3, (*61)

Replaces uploadUrl removed because a security issue (see #80), (*62)

Public dir relative path to directory where put uploaded image. I recommend you to create a function in your entity and call it like it's shown in the example:, (*63)

    /* DEPRECATED, Use upload dir
    public function getUploadRootDir()
    {
        // absolute path to your directory where images must be saved
        return __DIR__.'/../../../../../web/'.$this->getUploadDir();
    }*/

    public function getUploadDir()
    {
        return 'uploads/myentity';
    }

    public function getWebPath()
    {
        return null === $this->image ? null : '/'.$this->getUploadDir().'/'.$this->image;
    }

webDir (required)

Url used to show your image in templates, must be relative url. If you created related functions as explained in uploadDir section, then you can user getWebPath() function for webDir parameter., (*64)

fileExt (optional)

Permitted image extensions., (*65)

Default value: '.jpg;.gif;.png;.jpeg', (*66)

maxFileSize (optional)

Permitted maximum image weight in MB., (*67)

Default value: 50 (50Mb), (*68)

libraryDir (optional)

Directory to look into for images to show in image library., (*69)

Default value: uploadDir, (*70)

libraryRoute (optional)

Route called to get images to show in library. I recommend you to not change this parameter if you don't know exactly what it does., (*71)

Default value: comur_api_image_library, (*72)

showLibrary (optional)

Set this to false if you don't want the user see existing images in libraryDir., (*73)

Default value: true, (*74)

saveOriginal (optional)

Use this parameter if you want to save original file's path (for eg. to show big image in a lightbox). You have to put property name of your entity and the bundle will use it to save original file path in it., (*75)

Attention: This parameter is disabled for gallery for instance. It will be implemented soon., (*76)

Default value: false, (*77)

generateFilename (optional)

This parameter is used to generate an uniq file name. Setted to false, it will keep the original filename., (*78)

Default value: true, (*79)

cropConfig

disable (optional)

Since 2.0.4, (*80)

Disables cropping feature and saves directly original image, (*81)

Default value: false, (*82)

minWidth (optional)

Minimum with of desired image., (*83)

Default value: 1, (*84)

minHeight (optional)

Minimum height of desired image., (*85)

Default value: 1, (*86)

aspectRatio (optional)

True to aspect ratio of crop screen., (*87)

Default value: true, (*88)

cropRoute (optional)

Route to crop action. I recommend you to not change this parameter if you don't know exactly what it does., (*89)

Default value: comur_api_crop, (*90)

forceResize (optional)

If true, system will resize image to fit minWidth and minHeight. Default value: false, (*91)

thumbs (optional)

Array of thums to create automaticly. System will resize images to fit maxWidth and maxHeight. It will put them in "uploadDir/cropped_images_dir/thumbs_dir/widthxheight-originalfilename.extension" so you can use included Thumb Twig extension to get them, ex:, (*92)

{# your_themplate.html.twig #}
<img src="{{ entity.imagePath|thumb(45, 56) }}"

New in 0.2.2: You can use 'useAsFieldImage' option to use this thumb as image field's preview (in your form you will see this thumb instead of original cropped image). Usefull when you have big cropped images., (*93)

SECURITY

Please read following documentation about upload best practices: https://github.com/blueimp/jQuery-File-Upload/blob/master/SECURITY.md, (*94)

TODO LIST

  • Create tests
  • Add more comments in the code
  • Think about removed image deletion (for now images are not deleted, you have to care about it by yourself
)
  • Update existing images list dynamicly after an image upload
  • Add javascript events (in progress)

The Versions

19/08 2015