我正在将LiipImagineBundle与 Symfony 2.1 一起使用,并希望在上传时调整用户上传的图像大小,然后再将它们保存到永久文件系统位置(去除元数据、强制 jpeg 格式并限制文件的大小)。我必须从控制器调用“条带”和“调整大小”过滤器,然后将过滤后的图像从临时位置保存到我在文件系统中选择的文件夹中。
我尝试将LiipImageBundle 控制器用作捆绑包自述文件中所示的服务但是被调用的Action主要是为了在请求显示图像时在缓存目录中创建一个过滤后的图像(在上传过程中使用它进行过滤是另一种情况)。无论如何,我尝试按如下方式实现它,并让它工作。我必须首先将文件从 web 服务器的 php 临时目录移动到 web 文件夹中的目录才能应用过滤器。其次,我应用了过滤器并删除了(unlink())初始未过滤的文件。最后,我必须将过滤后的文件移动(重命名())到文件系统中的永久位置。必须移动文件两次,应用过滤器一次,然后删除(取消链接)1 个文件以使其全部工作。有没有更好的方法(不需要中间移动)在上传时使用捆绑包?
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 中的过滤器如下:
liip_imagine:
filter_sets:
my_filter:
format: jpeg
filters:
strip: ~
thumbnail: { size: [1600, 1000], mode: inset }
有人对 ImagineAvalancheBundle 提出了类似的问题,但没有给出太多细节。也许从此处提供的列表中实施另一项服务是更好的解决方案?