我在Symfony 2.1中使用LiipImagineBundle,并希望在上载之前调整用户上传的图像的大小,然后将它们保存到永久文件系统位置(去除元数据,强制jpeg格式和限制文件大小)。我必须从控制器调用“strip”和“resize”过滤器,然后将过滤的图像从临时位置保存到文件系统中我选择的文件夹中。使用LiipImagineBundle上传后调整图像大小?
我试图使用LiipImageBundle Controller as a service as indicated in the bundle's readme,但被调用的Action主要用于在请求显示图像(在上传过程中使用它进行过滤时是另一种情况)时在缓存目录中创建过滤图像。无论如何,我试图按照如下方式实施它,并将其付诸实施。我必须首先将文件从Web服务器的php临时目录移动到Web文件夹中的目录才能应用过滤器。其次,我应用过滤器并删除(取消链接())最初的未过滤文件。最后,我必须将过滤的文件移动(重命名())到文件系统中的永久位置。有必要移动文件两次,应用一次过滤器,并删除(取消链接)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.yml滤波器如下:
liip_imagine:
filter_sets:
my_filter:
format: jpeg
filters:
strip: ~
thumbnail: { size: [1600, 1000], mode: inset }
A similar question was asked for the ImagineAvalancheBundle但没有太多的细节中给出。也许实施another service from the here provided list是更好的解决方案?
你有没有找到一个更好的解决办法?我也想要这个。过滤器运行良好,但是当我有很多缩略图时,性能并不是很好,在上传时将它们从缓存移动到已知位置会显着加快速度。 – 2013-03-27 08:50:53
@PeterWooster:我没有找到另一个解决方案,但上面描述的解决方案似乎工作正常(我保持这种方式)。 – RayOnAir 2013-03-27 16:11:53
我正在研究解决方案,并在工作时发布。 – 2013-03-27 18:12:13