-
Notifications
You must be signed in to change notification settings - Fork 166
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
Comments
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 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! |
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, |
<?php
$imagineCachePathResolver = $this->container->get('imagine.cache.path.resolver');
urldecode(urldecode($imagineCachePathResolver->getBrowserPath('/path/to/image.png', $filterName))); |
There's a bug within the
|
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 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. |
I wonder why we don't have |
Please confirm this resolve the issue, so I can close it |
Ok, this is what I have done:
# Avalance Configuration
avalanche_imagine:
filters:
170x170:
type: thumbnail
options: { size: [170, 170], mode: outbound }
<?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: So, yes, I can close this issue! Note: |
has anything changed?
|
Found it! $imagineFilterManager->getFilter($filterName) |
fixed avalanche123#81 cache clearer only registered for sf2.1
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
The text was updated successfully, but these errors were encountered: