【问题标题】:Store both original and filtered image under the same folder - LiipImagineBundle将原始图像和过滤图像存储在同一文件夹下 - LiipImagineBundle
【发布时间】:2016-05-31 14:07:51
【问题描述】:

我正在使用 LiipImagineBundle 从上传的图像创建过滤图像。捆绑包的当前配置是:

liip_imagine:
    resolvers:
       default:
          web_path: ~

    filter_sets:
        cache: ~
        my_filter:
            filters:
                relative_resize: { heighten: 66 } 

我的目标是创建如下目录结构:

web_root\{subdirectory}\{type}_{filename_md5_hash}.jpeg

类型是s 用于小图像或o 用于原始尺寸。子目录是从上传日期创建的。

问题是 LiipImagineBundle 将过滤后的图像存储在web_root\my_filter\{subdirecory}\{type}_{filename_md5_hash}.jpeg 中。

如何省略网址的my_filter 部分?我需要将每个上传的图像及其过滤后的图像存储在同一个最终文件夹中。

我不是专家,我做了很多研究都无济于事。该配置允许为整个过滤器或单个过滤器更改缓存的 Web 路径,但过滤器名称始终出现在最终 url 中。

非常感谢您的帮助。

【问题讨论】:

    标签: php symfony liipimaginebundle


    【解决方案1】:

    你想要什么需要你创建一个自定义的CacheResolver(实现Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface)。起初这不是一件容易的事,但这是拥有这样一个特定用例的唯一方法。

    然后,将您的自定义 CacheResolver 注册为带有标签 liip_imagine.cache.resolver 的服务,为您的标签提供带有您选择的名称(例如 <tag name="liip_imagine.cache.resolver" resolver="same_folder" />)的 resolver 属性,并在您的配置中使用此解析器。 yml

    liip_imagine:
        filter_sets:
            FILTER_NAME:
                cache: same_folder
    

    【讨论】:

    • 我尝试实现这一点,但在执行此操作时,我采用了另一种方法作为答案。任何评论都非常感谢。
    【解决方案2】:

    我使用 LiipImagineBundle 提供的服务来更好地控制过滤过程。换句话说,我没有在控制器中获取liip_imagine.controller 服务,而是使用了“底层”服务:liip_imagine.data.managerliip_imagine.filter.managerliip_imagine.cache.manager

    在我的特殊情况下,我也省略了liip_imagine.cache.manager 的使用,因为我最终决定将过滤后的图像存储在与原始图像相同的目录中。因此不涉及缓存(无论是在配置中还是在目录结构中)。所以现在一切都存储在web\images

        $small_path= 's_'. $image_file->getClientOriginalName();
    
    
        $filter = 'my_filter';
        $config= array( 'widen' => 960) ;
    
        $path = $image->getWebPath();
    
        $container = $this->container;                                 
        $dataManager = $container->get('liip_imagine.data.manager');
        $filterManager = $container->get('liip_imagine.filter.manager');
    
        $binary = $dataManager->find($filter, $path);
    
        $filteredBinary =  $filterManager->applyFilter( $binary, $filter, array(
                               'filters' => array(
                                   'relative_resize' =>  $config
                                )
                            ));
    
        $small_path = str_replace("subdirectory/","subdirectory/s_",'subdirectory/'.$small_path );
        $s = $sBinary->getContent();                               
    
        $f = fopen($smallPath, 'w');                                 
        fwrite($f, $s );                                            
        fclose($f);
    

    当然,这涉及到配置的改变:

        my_filter:
            filters:
                relative_resize: [] 
    

    事实上,我现在可以动态使用relative_resize 内置过滤器的任何配置。 感谢您的时间。我希望收到有关此方法的反馈。

    【讨论】:

    • 由于您在此解决方案中根本不使用缓存,因此从长远来看,您可能会遇到性能问题。而且我不明白为什么要将文件保存到子目录中,因为您总是在运行时生成过滤后的文件并且从不使用缓存的文件。但除此之外,我同意这是可行的。这不如我建议的最佳和干净,但它确实可以完成工作。
    • @Terenoth,感谢您抽出宝贵时间,先生。我真的不明白为什么I have performance issues in the long run。我不在运行时生成过滤文件。我所做的是在上传文件时创建过滤图像(在控制器的 uploadAction 中)。然后我将cache 它放在与 liipImagineBundle 默认文件夹不同的文件夹中。非常感谢您的解释。
    • 哦,我以为您的代码是显示操作,而不是上传操作,我的错。在那种情况下,确实没有性能问题。我想唯一可以做的论点是您实际上不需要 LiipImagineBundle,而只需要 Imagine 本身,因为您并没有真正使用 LiipImagine 的缓存和过滤器功能。您可以只获取上传的二进制文件,对其应用 Imagine 函数,然后保存您的新文件,甚至无需调用 liip_imagine.data.managerliip_imagine.filter.manager。但只要一切正常,你就可以开始了:)
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多