2017 © Pedro Peláez
 

symfony-bundle liip-imagine-serialization-bundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

image

bukashk0zzz/liip-imagine-serialization-bundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  • Friday, December 8, 2017
  • by Bukashk0zzz
  • Repository
  • 3 Watchers
  • 18 Stars
  • 10,028 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 14 Versions
  • 6 % Grown

The README.md

Symfony LiipImagineSerialization Bundle

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version Total Downloads, (*1)

SensioLabsInsight, (*2)

About

Provides integration between LiipImagineBundle and JMSSerializerBundle. Allows to generate full or relative URIs to entity fields mapped with @Bukashk0zzz and @JMS annotations during the serialization. Also bundle supports VichUploaderBundle field type., (*3)

Installation Symfony Flex

composer config extra.symfony.allow-contrib true
composer require bukashk0zzz/liip-imagine-serialization-bundle

Installation without Symfony Flex

composer require bukashk0zzz/liip-imagine-serialization-bundle

Add the bundle to app/AppKernel.php, (*4)

$bundles = array(
    // ... other bundles
    new Bukashk0zzz\LiipImagineSerializationBundle\Bukashk0zzzLiipImagineSerializationBundle(),
);

Configuration

Add this to your config.yml:, (*5)

bukashk0zzz_liip_imagine_serialization:
    # Set true for generating url for vichUploader fields
    vichUploaderSerialize: false
    # Set true for generating url with host for vichUploader fields
    includeHost: false
    # Set true for adding original field value to object
    includeOriginal: false
    # Set true for adding host url to original value for vichUploader fields
    includeHostForOriginal: false
    # You can pass there your UrlNormalizer class that implements UrlNormalizerInterface
    originUrlNormalizer: null
    # You can pass there your UrlNormalizer class that implements UrlNormalizerInterface
    filteredUrlNormalizer: null

Usage

Add the next class to the use section of your entity class., (*6)

use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz;

Bundle provides two annotations which allow the serialization of url or @Vich\UploadableField fields in your entities. At first you have to add @Bukashk0zzz\LiipImagineSerializableClass to the entity class which has image fields. Then you have to add @Bukashk0zzz\LiipImagineSerializableField annotation to the field you want to serialize., (*7)

Annotation @Bukashk0zzz\LiipImagineSerializableClass does not have any option.
Annotation @Bukashk0zzz\LiipImagineSerializableField has one required option filter which value should link to the LiipImagine filter., (*8)

It can be set like this @Bukashk0zzz\LiipImagineSerializableField("photoFile") or @Bukashk0zzz\LiipImagineSerializableField(filter="photoFile"). filter can be array of filters in this case serialized field will be also array. For example if you add annotation @Bukashk0zzz\LiipImagineSerializableField(filter={"big", "small"}) for field image then you get:, (*9)

{
  "image": {
             "big": "/uploads/users/big/5659828fa80a7.jpg",
             "small": "/uploads/users/small/5659828fa80a7.jpg"
           }
}

Also there is another two options: - vichUploaderField - If you use VichUploaderBundle for your uploads you must specify link to the field with @Vich\UploadableField annotation - virtualField - By default serializer will override field value with link to filtered image. If you add virtualField option serializer will add to serialized object new field with name that you provided in this option and url to filtered image, original field in this case will be unattached. This option are required if you're using an array of filters., (*10)

Don't forget that to serialize image fields they also should be marked with @JMS annotations to be serialized., (*11)

The generated URI by default:, (*12)

{
  "photo": "http://example.com/uploads/users/photos/5659828fa80a7.jpg",
  "cover": "http://example.com/uploads/users/covers/456428fa8g4a8.jpg"
}

The generated URI with includeHost set to false:, (*13)

{
  "photo": "/uploads/users/photos/5659828fa80a7.jpg",
  "cover": "/uploads/users/covers/456428fa8g4a8.jpg"
}

If you need to change url before passing it to LiipImagine, for example you need to swap origin name, you can use originUrlNormalizer option in bundle config., (*14)

bukashk0zzz_liip_imagine_serialization:
    originUrlNormalizer: AppBundle\Normalizer\UrlNormalizer

If you need to change url after LiipImagine processing, for example you need to swap origin domain, you can use filteredUrlNormalizer option in bundle config., (*15)

bukashk0zzz_liip_imagine_serialization:
    filteredUrlNormalizer: AppBundle\Normalizer\UrlNormalizer

UrlNormalizer class must implement UrlNormalizerInterface, (*16)

<?php

namespace AppBundle\Normalizer;

use Bukashk0zzz\LiipImagineSerializationBundle\Normalizer\UrlNormalizerInterface;

/**
 * Url normalizer
 */
class UrlNormalizer implements UrlNormalizerInterface
{    
    /**
    * {@inheritdoc} 
    */
    public function normalize($url){
        return str_replace('photo.jpg', 'my_photo.jpg', $url);
    }
}

Events

There are two events: - bukashk0zzz_liip_imagine.event_pre_origin_normalize // Dispatch before origin url normalization - bukashk0zzz_liip_imagine.event_pre_filtered_normalize // Dispatch before filtered url normalization, (*17)

Example subscriber:, (*18)

services:
    app.liip_imagine_serialization_subscriber:
        class: AppBundle\Subscribers\LiipImagineSerializationEventSubscriber
        tags:
            - { name: bukashk0zzz_liip_imagine_subscriber }
<?php

namespace AppBundle\Subscribers;

use Bukashk0zzz\LiipImagineSerializationBundle\Event\UrlNormalizerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * LiipImagineSerializationEventSubscriber
 */
class LiipImagineSerializationEventSubscriber implements EventSubscriberInterface
{
    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            UrlNormalizerEvent::ORIGIN => [
                ['normalizeOrigin', 10],
            ],
            UrlNormalizerEvent::FILTERED => [
                ['normalizeFiltered', 10],
            ],
        ];
    }

    /**
     * @param UrlNormalizerEvent $event
     */
    public function normalizeOrigin(UrlNormalizerEvent $event)
    {
        $event->setUrl(str_replace('photo', 'newPhoto', $event->getUrl()));
    }

    /**
     * @param UrlNormalizerEvent $event
     */
    public function normalizeFiltered(UrlNormalizerEvent $event)
    {
        $event->setUrl(str_replace('example.com', 'img.example.com', $event->getUrl()));
    }
}

Example

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz;
use Symfony\Component\HttpFoundation\File\File;

/**
 * User Entity
 *
 * @ORM\Table(name="users")
 * @ORM\Entity()
 *
 * @Vich\Uploadable
 * @Bukashk0zzz\LiipImagineSerializableClass
 */
class User
{    
    /**
     * @var string $coverUrl Cover url
     *
     * @ORM\Column(type="string", length=255)
     *
     * @JMS\Expose
     * @JMS\SerializedName("cover")
     *
     * @Bukashk0zzz\LiipImagineSerializableField("thumb_filter")
     */
    public $coverUrl; 

    /**
     * @var string $photoName Photo name
     *
     * @ORM\Column(type="string", length=255)
     *
     * @JMS\Expose
     * @JMS\SerializedName("photo")
     *
     * @Bukashk0zzz\LiipImagineSerializableField("thumb_filter", vichUploaderField="photoFile")
     */
    public $photoName;

    /**
     * @var File $photoFile Photo file
     *
     * @JMS\Exclude
     *
     * @Vich\UploadableField(mapping="user_photo_mapping", fileNameProperty="photoName")
     */
    public $photoFile;
}

See LICENSE, (*19)

The Versions

08/12 2017

dev-master

9999999-dev https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

08/12 2017

v2.0.2

2.0.2.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

21/11 2017

v2.0.1

2.0.1.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

13/11 2017

v2.0.0

2.0.0.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

11/09 2017

v1.2.2

1.2.2.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

11/09 2017

v1.2.1

1.2.1.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

03/03 2017

v1.2.0

1.2.0.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

19/08 2016

v1.1.0

1.1.0.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

23/07 2016

v1.0.5

1.0.5.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

09/06 2016

v1.0.4

1.0.4.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

30/05 2016

v1.0.3

1.0.3.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

08/04 2016

v1.0.2

1.0.2.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

14/03 2016

v1.0.1

1.0.1.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle

10/03 2016

v1.0.0

1.0.0.0 https://github.com/bukashk0zzz/LiipImagineSerializationBundle

Provides integration between LiipImagineBundle and JMSSerializerBundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Golubovskiy

bundle serialization symfony integration imagine jmsserializerbundle liip liipimaginebundle