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
Given that I did not find a better way, I kept the solution described in the question description. That solution does not seem the optimal one from a performance stand point (it requires moving the file twice, apply the filter once, and unlink 1 file), but it gets the job accomplished.
UPDATE:
I changed my code to use the services indicated in Peter Wooster's answer, as shown below (that solution is more optimal as the filtered image is saved directly to the final destination):
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);
}
}