2013-05-01 3 views
5

Я использую liipImagineBundle и пытаюсь применить фильтр непосредственно в контроллер.Динамический фильтр с liipImagineBundle

В документе я нашел два раздела, в которых объясняется, как использовать liipImagineBundle с контроллера. Это один https://github.com/liip/LiipImagineBundle#using-the-controller-as-a-service

public function indexAction() 
{ 
    // RedirectResponse object 
    $imagemanagerResponse = $this->container 
     ->get('liip_imagine.controller') 
      ->filterAction(
       $this->getRequest(), 
       'uploads/foo.jpg',  // original image you want to apply a filter to 
       'my_thumb'    // filter defined in config.yml 
    ); 

    // string to put directly in the "src" of the tag <img> 
    $srcPath = $imagemanagerResponse->headers->get('location'); 

    // .. 
} 

И https://github.com/liip/LiipImagineBundle/blob/master/Resources/doc/filters.md#dynamic-filters

public function filterAction(Request $request, $path, $filter) 
{ 
$targetPath = $this->cacheManager->resolve($request, $path, $filter); 
if ($targetPath instanceof Response) { 
    return $targetPath; 
} 

$image = $this->dataManager->find($filter, $path); 

$filterConfig = $this->filterManager->getFilterConfiguration(); 
$config = $filterConfig->get($filter); 
$config['filters']['thumbnail']['size'] = array(300, 100); 
$filterConfig->set($filter, $config); 

$response = $this->filterManager->get($request, $filter, $image, $path); 

if ($targetPath) { 
    $response = $this->cacheManager->store($response, $targetPath, $filter); 
} 

return $response; 
} 

Я проверил, как указано в разделе «Использование контроллера в качестве службы», и это работает, проблема у меня есть, что я не знаю, как получить доступ настройки фильтра, чтобы изменить его.

liip_imagine: 
driver:    gd 
web_root:    %kernel.root_dir%/../web 
data_root:   %kernel.root_dir%/../web 
cache_mkdir_mode:  0777 
cache_prefix:   /media/cache 
cache:    web_path 
cache_clearer:  true 
data_loader:   filesystem 
controller_action: liip_imagine.controller:filterAction 
formats:    [] 
filter_sets: 
    my_thumb: 
     filters: 
      crop: { start: [0, 0], size: [200, 150] } 
    my_paste: 
     quality: 90 
     filters: 
      paste: { start: [30, 60], image: ../web/uploads/images/firma.jpg } 

Второй, действительно, я не понимаю, когда он говорит: «С помощью специального загрузчика данных ...».

В этом примере он только модифицирует метод filteraction() из класса ImagineController (Liip \ ImagineBundle \ Controller). Интересно, как я могу динамически модифицировать этот метод? Например, из моего контроллера indexAction().

Также я прочитал это сообщение https://stackoverflow.com/questions/16166719/loading-your-custom-filters-with-liipimaginebundle, где @NSCoder говорит, что «вы можете использовать встроенный фильтр и изменять их конфигурацию». но я этого не понимаю.

Я искал несколько дней, но я не нашел примера, с которого можно начать.

ответ

4

заводится еще я нашел другую LiipImagineBundle тему (Use LiipImagineBundle to Resize Image after Upload?), который помог мне сделать то, что я хотел ..

здесь я оставляю код я использую, чтобы динамически применить фильтр

public function indexAction() 
{ 

    $container = $this->container; 

    # The controller service 
    $imagemanagerResponse = $container->get('liip_imagine.controller'); 

    # The filter configuration service 
    $filterConfiguration = $container->get('liip_imagine.filter.configuration'); 

    # Get the filter settings 
    $configuracion = $filterConfiguration->get('my_thumb'); 

    # Update filter settings 
    $configuracion['filters']['crop']['size'] = array(50, 150); 
    $configuracion['filters']['crop']['start'] = array(10, 10); 
    $filterConfiguration->set('my_thumb', $configuracion); 

    # Apply the filter 
    $imagemanagerResponse->filterAction($this->getRequest(),'uploads/images/logo.jpg','my_thumb'); 

    # Move the img from temp 
    $fileTemporal = new File('media/cache/my_thumb/uploads/images/logo.jpg'); 

    $fileTemporal->move('uploads/images/', 'mini-logo.jpg'); 

    #################################### 

} 
Смежные вопросы