I'm using the LiipImagineBundle with Symfony 2.1 and would like to resize user uploaded images upon upload before saving them to the permanent filesystem location (to strip metadata, impose jpeg format, and limit the size of the file). I have to call a 'strip' and 'resize' filter from the controller and then save the filtered image from a temporary location to a folder of my choosing in the filesystem.
I tried to use the LiipImageBundle Controller as a service as indicated in the bundle's readme but the called Action is mainly for creating a filtered image in the cache directory when a request is made to display the image (using it for filtering during uploading is another case). I tried to implement it as follows anyways, and got it to work. I had to first move the file from the web server's php temporary directory to a directory in the web folder to be able to apply the filter. Secondly, I applied the filter and deleted (unlink()) the initial unfiltered file. Lastly, I had to move (rename()) the filtered file to the permanent location in the filesystem. It was necessary to move the file twice, apply the filter once, and delete (unlink) 1 file to make it all work. Is there a better way (not requiring the intermediate move) to use the bundle at upload?
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);
}
}
My filter in the app/config/config.yml is as follows:
liip_imagine:
filter_sets:
my_filter:
format: jpeg
filters:
strip: ~
thumbnail: { size: [1600, 1000], mode: inset }
A similar question was asked for the ImagineAvalancheBundle but no much details are given. Perhaps implementing another service from the here provided list is a better solution?
So here's a way to create thumbnails at upload with LiipImagineBundle. The trick is to use some of their other services:
/**
* 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
}
This could also be done by directly calling the Imagine library functions if you had no other reason to include the LiipImagineBundle. I'll probably look into that in the future, but this works for my case and performs very well.
Modified version of @Peter Wooster, and made it more generic so if someone is using it without Image entity he/she can easily take benifet from it. I'm giving here two versions, one that can be kept in utility or non-controller class. And the other version is for controller classes. It's up to you now where you like :)
To use outside of controller e.g. keeping it in utility classes:
/**
* 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
}
To use in controller e.g. CommonController or any other controller:
/**
* 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
}
Instead of loading the file using liip data manager, create liip binary object from the uploaded file:
use Liip\ImagineBundle\Model\Binary;
then use the following code:
// 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With