prestaconcept / PrestaImageBundle

Allow to crop local and remote image before uploading them through a classic form.
MIT License
23 stars 19 forks source link

PrestaImageBundle

tests quality codecov Latest Stable Version Total Downloads

Overview

PrestaImageBundle is a Symfony bundle providing tools to resize uploaded and remote images before sending them through a classic form. It uses the Cropper.js library.

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require presta/image-bundle

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:

$ composer require presta/image-bundle

Step 2: Enable the Bundle

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

// config/bundles.php

return [
    // ...
    Presta\ImageBundle\PrestaImageBundle::class => ['all' => true],
];

Configuration

You must configure the form_layout.html.twig form theme into config/packages/twig.yaml.

twig:
    form_themes:
        - "@PrestaImage/form/form_layout.html.twig"

Note: you can also create your own form theme instead.

You must include the routing into config/routes.yaml:

presta_image:
    resource: "@PrestaImageBundle/config/routing.yaml"

See VichUploader documentation to configure the bundle.

Assets

See Cropper.js documentation to install assets.

Don't forget to include the following assets in your page:

How to: implementation examples

Usage

Initialize cropper

document.querySelectorAll('.presta-image').forEach(element => {
    new Cropper(element)
})

Use the form type

<?php

use Presta\ImageBundle\Form\Type\ImageType;

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('image', ImageType::class)
    ;
}

Available options for the ImageType:

Notes

You can find Cropper.js options here.

The max_width and max_height options are used to define maximum size the cropped uploaded image will be. Bigger images (after cropping) are scaled down.

Security Note: NEVER rely on this size constraints and the format settings as they can easily be manipulated client side. ALWAYS validate the image-data, image-size, image-format server side!

Contributing

Pull requests are welcome.

Thanks to everyone who has contributed already.


This project is supported by PrestaConcept

Lead Developer : @J-Ben87

Released under the MIT License