Skip to content
onadrog edited this page Oct 12, 2021 · 3 revisions

Usage

Table of contents:

Requirements:

Installation:

composer require onadrog/imageconverterbundle
php bin/console asset:install

Mapping Attribute to an existing entity

example:

// src/Entity/Foo.php
namespace App\Entity;

// ...
use Onadrog\ImageConverterBundle\Mapping\Attribute as Onadrog;

/**
 * @ORM\Entity(repositoryClass=FooRepository::class)
 */
#[Onadrog\ImageUpload]
class Foo
{
    // ...

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $fileName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $fileSlug;

    /**
     * @ORM\Column(type="json")
     */
    private $fileDimension = [];

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $fileAlt;

    /**
     * @ORM\Column(type="json")
     */
    private $mimeTypes = [];

    #[Onadrog\ImageUploadProperties(name: 'fileName', slug: 'fileSlug', alt: 'fileAlt', dimension: 'fileDimension', mimeTypes: 'mimeTypes')]
    private $file;
}

Relational entity here we map attribute to Media:

// src/Entity/Product.php

namespace App\Entity;

// ...

class Product
{
    //...

    /**
     * @ORM\ManyToOne(targetEntity=Media::class, inversedBy="products", cascade={"persist"})
     */
    private $media;
}



// src/Entity/Media.php

namespace App\Entity;

use Onadrog\ImageConverterBundle\Mapping\Attribute as Onadrog;

#[Onadrog\ImageUpload]
class Media
{
    // ...

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $slug;

    /**
     * @ORM\Column(type="json")
     */
    private $dimension = [];

    /**
     * @ORM\OneToMany(targetEntity=Product::class, mappedBy="media")
     */
    private $products;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $alt;

    /**
     * @ORM\Column(type="json")
     */
    private $mimeTypes = [];

    #[Onadrog\ImageUploadProperties(name: 'name', slug: 'slug', dimension: 'dimension', alt: 'alt', mimeTypes: 'mimeTypes')]
    private $file;
}

FormBuilder:

Once the entity mapped you can use ImageConverterType as follow.

Add the propery who had the ImageUploadProperties attribute.

For entities with relation mapping use inversed property (e.g. for ProductType, media is used here). The bundle will read the Media entity and find the attribute mapping.

// src/Form/FooType.php
    namespace App\Foo\Form;

    // ...
    use Onadrog\ImageConverterBundle\Form\Type\ImageConverterType;

    class FooType extends AbstractType
    {
        //..
        public function builForm(FormBuilder $builder, array $options)
        {
            // ...

            $builder->add('file', ImageConverterType::class);
        }
}



        // ------------- Relational Entity mapping ------------- \\

// src/Form/ProductType.php

namespace App\Foo\Form;

// ...
use Onadrog\ImageConverterBundle\Form\Type\ImageConverterType;

class ProductType extends AbstractType
{
    //...
    public function builForm(FormBuilder $builder, array $options)
    {
        // ...

        $builder->add('media', ImageConverterType::class);
    }
}

Attributes:

ImageUpload:

Type: Attribute::TARGET_CLASS

This attribute is needed to tell the bundle wich entity will keep the image information into the database.

ImageUploadProperties:

Type: Attribute::TARGET_PROPERTY

This is where the magic happen this attribute require 4 properties:

name: the name of the image

alt: the image alt name (if no value are provided in the form the name will be used)

slug: the relative path to the image based on the public_path config

dimension: the dimension of the image (used in the twig extension for the <img> width and height attr)

mimeTypes: The file mimeType used to retrieve the convert file and if the configuration varaible is set to true retrieve also the original file, to put it inj a <picture> tag.

Note:

Consoles commands are also available to generate a new Entity class see command wiki

Clone this wiki locally