上传后使用 LiipImagineBundle 调整图像大小? [英] Use LiipImagineBundle to Resize Image after Upload?

查看:28
本文介绍了上传后使用 LiipImagineBundle 调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony 2.1 中使用 LiipImagineBundle 并且想在上传之前调整用户上传的图像的大小将它们保存到永久文件系统位置(去除元数据、强制使用 jpeg 格式并限制文件大小).我必须从控制器调用条带"和调整大小"过滤器,然后将过滤后的图像从临时位置保存到文件系统中我选择的文件夹中.

I'm using the LiipImagineBundle with Symfony 2.1 and would like to resize user uploaded images upon upload before saving them to the permanent filesystem location (to strip metadata, impose jpeg format, and limit the size of the file). I have to call a 'strip' and 'resize' filter from the controller and then save the filtered image from a temporary location to a folder of my choosing in the filesystem.

我尝试使用 LiipImageBundle Controller 作为服务,如图所示在包的自述文件中,但调用的 Action 主要用于在请求显示图像时在缓存目录中创建过滤的图像(在上传过程中使用它进行过滤是另一种情况).无论如何,我尝试按如下方式实现它,并使其正常工作.我必须首先将文件从 web 服务器的 php 临时目录移动到 web 文件夹中的目录才能应用过滤器.其次,我应用了过滤器并删除了 (unlink()) 初始未过滤文件.最后,我必须将 (rename()) 过滤后的文件移动到文件系统中的永久位置.有必要移动文件两次,应用过滤器一次,然后删除(取消链接)1 个文件以使其全部工作.有没有更好的方法(不需要中间移动)在上传时使用捆绑包?

I tried to use the LiipImageBundle Controller as a service as indicated in the bundle's readme but the called Action is mainly for creating a filtered image in the cache directory when a request is made to display the image (using it for filtering during uploading is another case). I tried to implement it as follows anyways, and got it to work. I had to first move the file from the web server's php temporary directory to a directory in the web folder to be able to apply the filter. Secondly, I applied the filter and deleted (unlink()) the initial unfiltered file. Lastly, I had to move (rename()) the filtered file to the permanent location in the filesystem. It was necessary to move the file twice, apply the filter once, and delete (unlink) 1 file to make it all work. Is there a better way (not requiring the intermediate move) to use the bundle at upload?

class MyController extends Controller
{
    public function new_imageAction(Request $request)
    {
        $uploadedFile = $request->files->get('file');
        $tmpFolderPathAbs = $this->get('kernel')->getRootDir() . '/../web/uploads/tmp/';
        $tmpImageNameNoExt = rand();
        $tmpImageName = $tmpImageNameNoExt . '.' . $fileExtension;
        $uploadedFile->move($tmpFolderPathAbs, $tmpImageName);
        $tmpImagePathRel = '/uploads/tmp/' . $tmpImageName;
        // Create the filtered image in a tmp folder:
        $this->container->get('liip_imagine.controller')->filterAction($request, $tmpImagePathRel, 'my_filter');
        unlink($tmpFolderPathAbs . $tmpImageName);
        $filteredImagePathAbs = $this->get('kernel')->getRootDir() . '/../web/uploads/cache/my_filter/uploads/tmp/' . $tmpImageNameNoExt . '.jpeg';
        $imagePath = $imageManagerResponse->headers->get('location');
        // define permanent location ($permanentImagePathAbs)...
        rename($filteredImagePathAbs, $permanentImagePathAbs);
    }
}

我在 app/config/config.yml 中的过滤器如下:

My filter in the app/config/config.yml is as follows:

liip_imagine:
    filter_sets:
        my_filter:
            format: jpeg
            filters:
                strip: ~
                thumbnail: { size: [1600, 1000], mode: inset }

对 ImagineAvalancheBundle 提出了类似的问题,但没有提供太多细节.也许实施此处提供的列表中的另一项服务是更好的解决方案?

A similar question was asked for the ImagineAvalancheBundle but no much details are given. Perhaps implementing another service from the here provided list is a better solution?

推荐答案

这里提供了一种使用 LiipImagineBundle 在上传时创建缩略图的方法.诀窍是使用他们的一些其他服务:

So here's a way to create thumbnails at upload with LiipImagineBundle. The trick is to use some of their other services:

    /**
     * Write a thumbnail image using the LiipImagineBundle
     * 
     * @param Document $document an Entity that represents an image in the database
     * @param string $filter the Imagine filter to use
     */
    private function writeThumbnail($document, $filter) {
        $path = $document->getWebPath();                                // domain relative path to full sized image
        $tpath = $document->getRootDir().$document->getThumbPath();     // absolute path of saved thumbnail

        $container = $this->container;                                  // the DI container
        $dataManager = $container->get('liip_imagine.data.manager');    // the data manager service
        $filterManager = $container->get('liip_imagine.filter.manager');// the filter manager service

        $image = $dataManager->find($filter, $path);                    // find the image and determine its type
        $response = $filterManager->get($this->getRequest(), $filter, $image, $path); // run the filter 
        $thumb = $response->getContent();                               // get the image from the response

        $f = fopen($tpath, 'w');                                        // create thumbnail file
        fwrite($f, $thumb);                                             // write the thumbnail
        fclose($f);                                                     // close the file
    }

如果您没有其他理由包含 LiipImagineBundle,这也可以通过直接调用 Imagine 库函数来完成.我可能会在未来研究这个问题,但这对我的情况很有效,并且表现非常好.

This could also be done by directly calling the Imagine library functions if you had no other reason to include the LiipImagineBundle. I'll probably look into that in the future, but this works for my case and performs very well.

这篇关于上传后使用 LiipImagineBundle 调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆