Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiipImagineBundle: How to apply filter in controller?

I want to use LiipImagineBundle to resize my uploaded pictures in the controller.

  $extension = $file->guessExtension();
  $newfilename = sha1(uniqid(mt_rand(), true));
  $tmp_folder = $this->get('kernel')->getRootDir() . '/../web/uploads/tmp/'; // folder to store unfiltered temp file
  $tmp_imagename = $newfilename.'.'.$extension;
  $file->move($tmp_folder, $tmp_imagename);

  $tmpImagePathRel = '/uploads/tmp/' . $tmp_imagename;
  $processedImage = $this->container->get('liip_imagine.data.manager')->find('profilepic', $tmpImagePathRel);
  $newimage_string = $this->container->get('liip_imagine.filter.manager')->get($request, 'profilepic', $processedImage, $tmpImagePathRel)->getContent();

  unlink($tmp_folder . $tmp_imagename); // eliminate unfiltered temp file.
  $perm_folder = $this->get('kernel')->getRootDir() . '/../web/uploads/userimages/';
  $perm_imagepath = $perm_folder . $newfilename . '.jpeg';
  $f = fopen($perm_imagepathh, 'w');
  fwrite($f, $newimage_string); 
  fclose($f);

That brings the following error:

Attempted to call method "get" on class "Liip\ImagineBundle\Imagine\Filter\FilterManager" in C:\...\UserController.php line xx. Did you mean to call: "getFilterConfiguration"?

Then i try instead

$newimage_string = $this->container->get('liip_imagine.filter.manager')->getFilterConfiguration($request, 'profilepic', $processedImage, $tmpImagePathRel);

And it brings me

Warning: fwrite() expects parameter 2 to be string, object given in C:\..

I hardly cannot find any documentation or examples to get this task done :( Im pretty disappointed with the bundle docs ://

Any help is really appreciated!

Thanks in advance!

like image 262
Fabian Avatar asked Sep 09 '14 16:09

Fabian


2 Answers

Ok i got it to work, maybe usefull for others:

$newimage_string = $this->container->get('liip_imagine.filter.manager')->applyFilter($processedImage, 'profilepic')->getContent();
like image 140
Fabian Avatar answered Sep 22 '22 08:09

Fabian


(For Symfony 4.2)

the solution is only for binary data not for string. this is working for me. I am using VichUploaderBundle with Imagine Bundle.

$helper is instance of \Vich\UploaderBundle\Templating\Helper\UploaderHelper. Check their documentation, if you don't know what I am talking about here.

$path = $helper->asset($entity, 'imageFile');

$imagine = $this->container->get('liip_imagine.cache.resolver.default');
$imageString = $imagine->resolve($path, 'public_large');

$this->container->get('liip_imagine.cache.resolver.context');

this depends on context you used. Check your resolvers config on liip_imagine.yaml

$imagine->resolve($path, filter you used);

Hope you save your time.

Update

this code get issue. it is generated only image Path (if the image already resized, then it will show up. however if the image just uploaded and not resized yet. then the solution you need is this one below)

$this->container->get('liip_imagine.cache.manager');

Use Cache Manager instead.

like image 40
user3392555 Avatar answered Sep 23 '22 08:09

user3392555