https://github.com/bukashk0zzz/liipimagineserializationbundle
Provides integration between LiipImagineBundle and JMSSerializerBundle
https://github.com/bukashk0zzz/liipimagineserializationbundle
bundle jmsserializerbundle liipimaginebundle php symfony symfony-bundle symfony3-bundle vichuploaderbundle
Last synced: 9 months ago
JSON representation
Provides integration between LiipImagineBundle and JMSSerializerBundle
- Host: GitHub
- URL: https://github.com/bukashk0zzz/liipimagineserializationbundle
- Owner: Bukashk0zzz
- License: mit
- Created: 2016-03-07T15:06:47.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-01-26T06:49:43.000Z (about 4 years ago)
- Last Synced: 2025-06-20T13:56:00.647Z (9 months ago)
- Topics: bundle, jmsserializerbundle, liipimaginebundle, php, symfony, symfony-bundle, symfony3-bundle, vichuploaderbundle
- Language: PHP
- Size: 98.6 KB
- Stars: 25
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Symfony LiipImagineSerialization Bundle
[](https://travis-ci.org/Bukashk0zzz/LiipImagineSerializationBundle)
[](https://codecov.io/github/Bukashk0zzz/LiipImagineSerializationBundle)
[](https://scrutinizer-ci.com/g/Bukashk0zzz/LiipImagineSerializationBundle/?branch=master)
[](https://packagist.org/packages/Bukashk0zzz/liip-imagine-serialization-bundle)
[](https://packagist.org/packages/Bukashk0zzz/liip-imagine-serialization-bundle)
[](https://packagist.org/packages/Bukashk0zzz/liip-imagine-serialization-bundle)
[](https://insight.sensiolabs.com/projects/01074784-ea2d-4902-8a62-53a5f743bc6f)
About
-----
Provides integration between [LiipImagineBundle](https://github.com/liip/LiipImagineBundle "LiipImagineBundle") and
[JMSSerializerBundle](https://github.com/schmittjoh/JMSSerializerBundle "JMSSerializerBundle").
Allows to generate full or relative URIs to entity fields mapped with `@Bukashk0zzz` and `@JMS` annotations during the serialization.
Also bundle supports [VichUploaderBundle](https://github.com/dustin10/VichUploaderBundle "VichUploaderBundle") field type.
Installation Symfony Flex
------------
```bash
composer config extra.symfony.allow-contrib true
composer require bukashk0zzz/liip-imagine-serialization-bundle
```
Installation without Symfony Flex
------------
```bash
composer require bukashk0zzz/liip-imagine-serialization-bundle
```
Add the bundle to `app/AppKernel.php`
```php
$bundles = array(
// ... other bundles
new Bukashk0zzz\LiipImagineSerializationBundle\Bukashk0zzzLiipImagineSerializationBundle(),
);
```
Configuration
-------------
Add this to your `config.yml`:
```yaml
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.
```php
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:
```json
{
"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:
```json
{
"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`:
```json
{
"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.
```yaml
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.
```yaml
bukashk0zzz_liip_imagine_serialization:
filteredUrlNormalizer: AppBundle\Normalizer\UrlNormalizer
```
UrlNormalizer class must implement [UrlNormalizerInterface](https://github.com/Bukashk0zzz/LiipImagineSerializationBundle/blob/master/Normalizer/UrlNormalizerInterface.php)
```php
[
['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