All Projects → Bukashk0zzz → LiipImagineSerializationBundle

Bukashk0zzz / LiipImagineSerializationBundle

Licence: MIT license
Provides integration between LiipImagineBundle and JMSSerializerBundle

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to LiipImagineSerializationBundle

Sonataintlbundle
Symfony SonataIntlBundle
Stars: ✭ 212 (+783.33%)
Mutual labels:  bundle, symfony-bundle
SlackBundle
SlackBundle for Symfony2 with Guzzle-Integration
Stars: ✭ 39 (+62.5%)
Mutual labels:  bundle, symfony-bundle
SonataDoctrineMongoDBAdminBundle
Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle
Stars: ✭ 64 (+166.67%)
Mutual labels:  bundle, symfony-bundle
Gifexceptionbundle
😛 The GhostBuster of your exception page!
Stars: ✭ 197 (+720.83%)
Mutual labels:  bundle, symfony-bundle
Lexikmaintenancebundle
This Symfony2 bundle allows you to place your website in maintenance mode by calling two commands in your console. A page with status code 503 appears to users, it is possible to authorize certain ips addresses stored in your configuration.
Stars: ✭ 253 (+954.17%)
Mutual labels:  bundle, symfony-bundle
Liiphellobundle
[DEPRECATED] Alternative Hello World Bundle for Symfony2 using several FriendsOfSymfony Bundles
Stars: ✭ 206 (+758.33%)
Mutual labels:  bundle, symfony-bundle
gulp-rev-versions-bundle
A bundle that allows symfony to get the version of assets versioned with gulp-rev
Stars: ✭ 13 (-45.83%)
Mutual labels:  bundle, symfony-bundle
Sonataadminbundle
The missing Symfony Admin Generator
Stars: ✭ 2,039 (+8395.83%)
Mutual labels:  bundle, symfony-bundle
Enqueue Bundle
[READ-ONLY] Message queue bundle for Symfony. RabbitMQ, Amazon SQS, Redis, Service bus, Async events, RPC over MQ and a lot more
Stars: ✭ 233 (+870.83%)
Mutual labels:  bundle, symfony-bundle
Fmelfinderbundle
📁 ElFinderBundle provides ElFinder integration with TinyMCE, CKEditor, Summernote editors
Stars: ✭ 231 (+862.5%)
Mutual labels:  bundle, symfony-bundle
Mercure Bundle
The MercureBundle allows to easily push updates to web browsers and other HTTP clients in the Symfony full-stack framework, using the Mercure protocol.
Stars: ✭ 195 (+712.5%)
Mutual labels:  bundle, symfony-bundle
stampie-bundle
stampie.github.io/
Stars: ✭ 26 (+8.33%)
Mutual labels:  bundle, symfony-bundle
Sonatapagebundle
This bundle provides a Site and Page management through container and block services
Stars: ✭ 181 (+654.17%)
Mutual labels:  bundle, symfony-bundle
SensioBuzzBundle
No description or website provided.
Stars: ✭ 89 (+270.83%)
Mutual labels:  bundle, symfony-bundle
Twig Bundle
The Twig Bundle provides configuration for using Twig in your applications.
Stars: ✭ 2,150 (+8858.33%)
Mutual labels:  bundle, symfony-bundle
SonataTimelineBundle
[Abandoned] Integrates SpyTimelineBundle into Sonata
Stars: ✭ 24 (+0%)
Mutual labels:  bundle, symfony-bundle
Debug Bundle
The DebugBundle allows greater integration of the VarDumper component in the Symfony full-stack framework.
Stars: ✭ 2,033 (+8370.83%)
Mutual labels:  bundle, symfony-bundle
Nelmioapidocbundle
Generates documentation for your REST API from annotations
Stars: ✭ 2,009 (+8270.83%)
Mutual labels:  bundle, symfony-bundle
Victoire
Fullstack Symfony CMS: The perfect mix between a framework and a CMS
Stars: ✭ 227 (+845.83%)
Mutual labels:  bundle, symfony-bundle
SonataDashboardBundle
[Abandoned] Provides a Dashboard management through container and block services
Stars: ✭ 17 (-29.17%)
Mutual labels:  bundle, symfony-bundle

Symfony LiipImagineSerialization Bundle

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version Total Downloads

SensioLabsInsight

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.

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

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

Configuration

Add this to your config.yml:

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.

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.

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

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:

{
  "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.

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

The generated URI by default:

{
  "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:

{
  "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.

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.

bukashk0zzz_liip_imagine_serialization:
    filteredUrlNormalizer: AppBundle\Normalizer\UrlNormalizer

UrlNormalizer class must implement UrlNormalizerInterface

<?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

Example subscriber:

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;
}

Copyright / License

See LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].