Skip to content

Instantly share code, notes, and snippets.

@pilot
Created May 24, 2017 15:55
Show Gist options
  • Save pilot/a83ed432b769d6bf4546cc1ecdd3d6d5 to your computer and use it in GitHub Desktop.
Save pilot/a83ed432b769d6bf4546cc1ecdd3d6d5 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class ImagePathGenerator
*/
class ImagePathGenerator
{
const LOGO_PREFIX = '/store/uploads/logo/';
private $host;
private $requestStack;
/**
* ImagePathGenerator constructor.
* @param string $host
* @param RequestStack $requestStack
*/
public function __construct($host, RequestStack $requestStack)
{
$this->host = $host;
$this->requestStack = $requestStack;
}
/**
* @param string $file
* @return array
*/
public function generateLogoSizes($file)
{
return $this->generateSizes(self::LOGO_PREFIX, $file);
}
/**
* @param string $file
* @return array
*/
public function generateImagesSizes($file)
{
return $this->generateSizes('', $file);
}
protected function generateSizes($prefix, $file)
{
$request = $this->requestStack->getCurrentRequest();
$template = $request->getScheme().'://'.$request->getHost().'/transform/resize_%sx-/%s';
return [
'small' => sprintf($template, 200, $this->host.$prefix.$file),
'medium' => sprintf($template, 500, $this->host.$prefix.$file),
'large' => $this->host.$prefix.$file,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment