Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to flip image horizontally/vertically? #918

Closed
qweluke opened this issue Apr 27, 2017 · 4 comments
Closed

How to flip image horizontally/vertically? #918

qweluke opened this issue Apr 27, 2017 · 4 comments
Labels
Level: New Feature 🆕 This item involves the introduction of new functionality. Type: Support This item pertains to support for this project.

Comments

@qweluke
Copy link

qweluke commented Apr 27, 2017

Q A
Bug Report? no
Feature Request? no
BC Break Report? no
RFC? no
Imagine Bundle Version ^1.6

quick question: how to flip image vertically and/or horizontally?

@robfrawley
Copy link
Collaborator

You must implement a custom filter as none of the filters provided by default implement a flip method. For example, here is the basic filter implementation you'd need:

<?php

namespace AppBundle\Imagine\Filter\Loader;

use Imagine\Image\ImageInterface;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;

class FlipFilter implements LoaderInterface
{
    /**
     * @param ImageInterface $image
     * @param array          $options
     *
     * @return ImageInterface
     */
    public function load(ImageInterface $image, array $options = array())
    {
        try {
            $options = (new OptionsResolver())
                ->setDefault('axis', 'x')
                ->setAllowedValues('axis', ['x', 'y'])
                ->resolve($options);
        } catch (ExceptionInterface $e) {
            throw new InvalidArgumentException('The "axis" option must be set to "x" or "y"', null, $e);
        }

        if ($options['axis'] === 'x') {
            $image->flipHorizontally();
        } else {
            $image->flipVertically();
        }

        return $image;
    }
}

Then, register the filter in your services.yml file, ensuring it is tagged with the "name" liip_imagine.filter.loader and the "loader" set to whatever name you'd like to reference the filter from your filter set configuration.

# app/config/services.yml

services:
    app.filter.flip_filter:
        class: AppBundle\Imagine\Filter\Loader\FlipFilter
        tags:
            - { name: "liip_imagine.filter.loader", loader: flip_filter }

Lastly, again, use the value of the "loader" tag to reference the filter in your configuration:

# app/config/config.yml

liip_imagine:
    filter_sets:
        my_cool_filter_set:
            filters:
                flip_filter: { axis: x }

@robfrawley robfrawley added Level: New Feature 🆕 This item involves the introduction of new functionality. Type: Support This item pertains to support for this project. State: Confirmed This item has been confirmed by maintainers as legitimate. labels Apr 28, 2017
@qweluke
Copy link
Author

qweluke commented Apr 28, 2017

@robfrawley thank you!

@robfrawley
Copy link
Collaborator

FYI: I just submitted a pull request (#920) that adds the flip filter (identical to my above example) within the core bundle; it should be included in the next minor release, likely 1.7.5.

@lsmith77 lsmith77 removed the State: Confirmed This item has been confirmed by maintainers as legitimate. label Apr 30, 2017
@robfrawley
Copy link
Collaborator

The above-detailed functionality has been merged into the core in PR #920, you can use it immediately by requiring dev-master in your composer file, or you can wait for the next release 1.7.5. Thanks for finding this notable shortcoming in the default provided filter implementations @qweluke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Level: New Feature 🆕 This item involves the introduction of new functionality. Type: Support This item pertains to support for this project.
Projects
None yet
Development

No branches or pull requests

3 participants