我使用的是LiipImagineBundle与Symfony的2.1,并希望将它们保存到永久文件系统位置之前上传时,调整用户上传的图片(剥元,并处JPEG格式,并限制该文件的大小)。 我打电话给来自控制器的“条”和“调整大小”过滤器,然后从临时位置保存过滤的图像我在文件系统中选择的文件夹中。
我试图使用LiipImageBundle控制器作为服务如在包的自述指示但被叫动作主要是用于在当做出请求时要显示的图像的高速缓存目录中的滤波图像(在使用它上传期间过滤是另一种情况下)。 我想作为反正如下来实现它,得到它的工作。 我必须首先从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 }
类似的问题被问了ImagineAvalancheBundle ,但没有太多的细节中给出。 或许,实现从这里提供的列表中的另一个服务是一个更好的解决办法?
Answer 1:
因此,这里是创建在上传与LiipImagineBundle缩略图的方式。 诀窍是使用他们的一些其他的服务:
/**
* 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畅想库函数来完成。 我可能会考虑,在未来,但这个工程对我的情况,并有很好的表现。
Answer 2:
修改@Peter伍斯特的版本,因此,如果有人使用它没有他/她可以很容易地从benifet要花图像实体变得更加通用。 我在这里给两个版本,一个可以用来保存在公用或不控制器类。 而另一个版本是控制器类。 现在是给你你喜欢的地方:)
要使用的控制器,如外界保持它在实用工具类
/**
* Write a thumbnail image using the LiipImagineBundle
*
* @param Document $fullSizeImgWebPath path where full size upload is stored e.g. uploads/attachments
* @param string $thumbAbsPath full absolute path to attachment directory e.g. /var/www/project1/images/thumbs/
* @param string $filter filter defined in config e.g. my_thumb
* @param Object $diContainer Dependency Injection Object, if calling from controller just pass $this
*/
public function writeThumbnail($fullSizeImgWebPath, $thumbAbsPath, $filter, $diContainer) {
$container = $diContainer; // the DI container, if keeping this function in controller just use $container = $this
$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, $fullSizeImgWebPath); // find the image and determine its type
$response = $filterManager->applyFilter($image, $filter);
$thumb = $response->getContent(); // get the image from the response
$f = fopen($thumbAbsPath, 'w'); // create thumbnail file
fwrite($f, $thumb); // write the thumbnail
fclose($f); // close the file
}
要在控制器如CommonController或任何其他控制器使用。
/**
* Write a thumbnail image using the LiipImagineBundle
*
* @param Document $fullSizeImgWebPath path where full size upload is stored e.g. uploads/attachments
* @param string $thumbAbsPath full absolute path to attachment directory e.g. /var/www/project1/images/thumbs/
* @param string $filter filter defined in config e.g. my_thumb
*/
public function writeThumbnail($fullSizeImgWebPath, $thumbAbsPath, $filter) {
$container = $this->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, $fullSizeImgWebPath); // find the image and determine its type
$response = $filterManager->applyFilter($image, $filter);
$thumb = $response->getContent(); // get the image from the response
$f = fopen($thumbAbsPath, 'w'); // create thumbnail file
fwrite($f, $thumb); // write the thumbnail
fclose($f); // close the file
}
Answer 3:
由于我没有找到更好的办法,我一直在这个问题叙述的解决方案。 该解决方案似乎并没有从性能的立场来看最优的(它需要两倍移动文件,一旦应用过滤器,并取消链接1个文件),但它可以完成的工作。
更新:
我改变了我的代码,使用彼得伍斯特的回答表明了服务,如下图所示(如过滤的图像直接保存到最终目的地的解决方案是更优化的):
class MyController extends Controller
{
public function new_imageAction(Request $request)
{
$uploadedFile = $request->files->get('file');
// ...get file extension and do other validation...
$tmpFolderPathAbs = $this->get('kernel')->getRootDir() . '/../web/uploads/tmp/'; // folder to store unfiltered temp file
$tmpImageNameNoExt = rand();
$tmpImageName = $tmpImageNameNoExt . '.' . $fileExtension;
$uploadedFile->move($tmpFolderPathAbs, $tmpImageName);
$tmpImagePathRel = '/uploads/tmp/' . $tmpImageName;
// Create the filtered image:
$processedImage = $this->container->get('liip_imagine.data.manager')->find('my_filter', $tmpImagePathRel);
$filteredImage = $this->container->get('liip_imagine.filter.manager')->get($request, 'my_filter', $processedImage, $tmpImagePathRel)->getContent();
unlink($tmpFolderPathAbs . $tmpImageName); // eliminate unfiltered temp file.
$permanentFolderPath = $this->get('kernel')->getRootDir() . '/../web/uploads/path_to_folder/';
$permanentImagePath = $permanentFolderPath . 'my_image.jpeg';
$f = fopen($permanentImagePath, 'w');
fwrite($f, $filteredImage);
fclose($f);
}
}
Answer 4:
我已经写了确切的解决了这个问题一个包。 虽然VichUploaderBundle
使用ORM的生命周期回调提供方便上传, LiipImagine
做了伟大的工作,在调整大小。
这里是它的组合: https://github.com/RSSfeed/VichImagineBundle
见短自述如何实现它只有几分钟。
Answer 5:
不必加载使用LIIP数据管理器的文件,创建LIIP上传文件中的二进制对象:
use Liip\ImagineBundle\Model\Binary;
然后使用下面的代码:
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()) . '.' . $uploadedFile->guessExtension();
$contents = file_get_contents($uploadedFile);
$binary = new Binary(
$contents,
$uploadedFile->getMimeType(),
$uploadedFile->guessExtension()
);
$container = $this->container;
$filterManager = $container->get('liip_imagine.filter.manager'); // the filter manager service
$response = $filterManager->applyFilter($binary, 'my_thumb');
$thumb = $response->getContent(); // get the image from the response
$f = fopen($webRootDir .'/images_dir/' . $fileName, 'w'); // create thumbnail file
fwrite($f, $thumb); // write the thumbnail
fclose($f); // close the file
文章来源: Use LiipImagineBundle to Resize Image after Upload?