Liipimaginebundle: How to apply filter in controller?

Created on 9 Sep 2014  路  3Comments  路  Source: liip/LiipImagineBundle

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/';
  $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!

Most helpful comment

How to get url of the picture after apply filter?

All 3 comments

ok i got it to work through another users issue question:

$newimage_string = $this->container->get('liip_imagine.filter.manager')->applyFilter($processedImage, 'profilepic')->getContent();

Should be mentioned in the docs i guess.

How to get url of the picture after apply filter?

@seddighi78 Inject the cache service and call like below:

<?php

namespace App\Controller;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class MediaManagerController extends AbstractController
{
    /**
     * @Route("/account/media/upload-handler", name="media_manager_uploadhandler")
     */
    public function uploadHandler(
        Request $request,
        CacheManager $cacheManager
    ): JsonResponse
    {
            // Process your image, and then...
            // "thumbnail" is the name of the LiipImagineBundle filter I'm applying
            return new JsonResponse([
                'url' => $cacheManager->getBrowserPath(parse_url($i->getFullPath(), PHP_URL_PATH), 'thumbnail', [], null),
            ]);
    }
}
Was this page helpful?
0 / 5 - 0 ratings