public function getFilters()
{
$client = new \Mleko\ImageSqueeze\Client\Client();
$squeeze = new \Twig_SimpleFilter('squeeze', function ($path) use($client) {
$path = (string) $path;
if (strlen($path) > 0 && $path[0] === '@') {
$path = $this->kernel->locateResource($path);
}
if (file_exists($path)) {
$inputFile = new \Mleko\ImageSqueeze\Client\File($path);
} else {
$inputFile = new \Mleko\ImageSqueeze\Client\File($path, $this->webRoot);
}
$pathHash = str_pad(base_convert(sha1($path), 16, 36), 31, '0', STR_PAD_LEFT);
$compressedName = implode("/", str_split(substr($pathHash, 0, 6), 1)) . "/" . substr($pathHash, 6);
if (false !== ($dotPosition = strrpos($path, "."))) {
$compressedName .= substr($path, $dotPosition);
}
$newPath = '/cache/image/' . $compressedName;
$fullPath = $this->webRoot . $newPath;
if (!file_exists($fullPath)) {
$newDir = dirname($fullPath);
if (!file_exists($newDir)) {
mkdir($newDir, 0777, true);
}
return $client->shrink($inputFile)->toFile($newPath, $this->webRoot);
}
return new \Mleko\ImageSqueeze\Client\File($newPath, $this->webRoot);
});
return ['squeeze' => $squeeze];
}