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

Apply a filter within Controller #82

Closed
targzeta opened this issue Sep 28, 2012 · 10 comments
Closed

Apply a filter within Controller #82

targzeta opened this issue Sep 28, 2012 · 10 comments

Comments

@targzeta
Copy link

Hi, it is possible to apply a filter within Controller? I have found these link:
#35
#62

I have 'my_thumb' filter (app/config.yml) and within a Controller (called via AJAX) I have an array of paths. I would apply 'my_thumb' filter for each element of this array so I can return (via JSON) the list of original photos and created thumbnails.

Thank you in advance,
Emanuele

@avalanche123
Copy link
Owner

I think you found the right issues. The second one shows how to use a filter once you got ahold of filter manager. Filter manager is registered in DI as imagine.filter.manager.

So from a classic controller you could do:

<?php
$filterName           = 'my_thumb';
$imagineFilterManager = $this->container->get('imagine.filter.manager');
$imagineFilterManager->get($filterName)
            ->apply($this->imagine->open('/path/to/image.png'))
            ->save(
                '/path/to/filtered/image.png',
                array('quality' => $imagineFilterManager->getOption($filterName, "quality", 100))
            )
;

Cheers!

@targzeta
Copy link
Author

targzeta commented Oct 1, 2012

Thank you. How can I find the '/path/to/filtered/'? When I apply the 'my_thumb' filter within the template, the '/path/to/filtered/' is 'web/media/cache/my_thumb/uploads/lines' or ''web/media/cache/my_thumb/uploads/photos'. How can I find this path within the controller? Can I suppose 'web/media/cache/my_thumb' as the first components?

Thank you again,
Emanuele

@avalanche123
Copy link
Owner

<?php
$imagineCachePathResolver = $this->container->get('imagine.cache.path.resolver');
urldecode(urldecode($imagineCachePathResolver->getBrowserPath('/path/to/image.png', $filterName)));

@malteo
Copy link

malteo commented Oct 3, 2012

There's a bug within the getBrowserPath() function: it returns the front controller as a subdirectory beneath web/

imagepng(): Unable to open '/Users/malte/Sites/glassdesymfony/app/../web//app_dev.php/media/cache/170x170/path/to/image.png' for writing: Not a directory

@avalanche123
Copy link
Owner

You are right, please refer to controller code to see how this is stripped out https://github.com/avalanche123/AvalancheImagineBundle/blob/develop/Controller/ImagineController.php#L94
This is not a bug.

We can't know base path without request object, but cachepathresolver service doesn't depend on request because we don't want to re-create it for every request, so I made a decision to deal with base path weirdness in controller as that is re-created for every request any way.

@malteo
Copy link

malteo commented Oct 4, 2012

I wonder why we don't have sfConfig::get('sf_web_dir') anymore...

@avalanche123
Copy link
Owner

Please confirm this resolve the issue, so I can close it

@targzeta
Copy link
Author

targzeta commented Oct 5, 2012

Ok, this is what I have done:

  • app/config/config.yml
# Avalance Configuration
avalanche_imagine:
  filters:
    170x170:
      type: thumbnail
      options: { size: [170, 170], mode: outbound }
  • within my controller:
<?php
...
private function thumb($photo)
{
        $filterName = '170x170';

        // Absolute path to symfony project's web directory
        $webPath = $this->get('kernel')->getRootDir() . '/../web/';

        // Browser path to thumbnail
        $imagineCachePathResolver = $this->container->get('imagine.cache.path.resolver');
        $thumbPath = urldecode(urldecode($imagineCachePathResolver->getBrowserPath($photo->getWebPath(), $filterName)));

        $filesystem = $this->container->get('filesystem');
        $thumbDir = dirname($webPath . $thumbPath);
        if (!is_dir($thumbDir)){
            $filesystem->mkdir($thumbDir);
        }

        $imagine = $this->container->get('imagine');
        if (!file_exists($webPath . $thumbPath)) {
            $imagineFilterManager = $this->container->get('imagine.filter.manager');
            $imagineFilterManager->get($filterName)
                ->apply($imagine->open($photo->getAbsolutePath()))
                ->save($webPath . $thumbPath);
        }

        return $thumbPath;
}

The $photo->getWebPath() returns the photo path relative to web directory.

The files are correctly created under:
web/media/cache/170x170/

So, yes, I can close this issue!

Note:
To get the absolute path of the 'web' directory, see also Relative path and context

@targzeta targzeta closed this as completed Oct 5, 2012
@lughino
Copy link

lughino commented Mar 18, 2013

has anything changed?
I tried to use the same code above and I return this error:

 Fatal error: Call to undefined method Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager::get() in

@lughino
Copy link

lughino commented Mar 18, 2013

Found it!

$imagineFilterManager->getFilter($filterName)

Bluestart83 pushed a commit to Bluestart83/AvalancheImagineBundle that referenced this issue Jun 10, 2013
fixed avalanche123#81 cache clearer only registered for sf2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants