6

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!

Fabian
  • 1,706
  • 5
  • 21
  • 38

2 Answers2

11

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

$newimage_string = $this->container->get('liip_imagine.filter.manager')->applyFilter($processedImage, 'profilepic')->getContent();
Fabian
  • 1,706
  • 5
  • 21
  • 38
0

(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.

  • But you cannot get content with cache manager (without calling file_get_content in an absolute URL) – Erdal G. Aug 25 '20 at 18:16