namespace Rapsys\PackBundle\Util;
+use Psr\Container\ContainerInterface;
+
+use Rapsys\PackBundle\RapsysPackBundle;
+
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
*/
class FacebookUtil {
/**
- * The align
- *
- * @var string
- */
- protected string $align;
-
- /**
- * The cache directory
- *
- * @var string
- */
- protected string $cache;
-
- /**
- * The fill
- *
- * @var string
- */
- protected string $fill;
-
- /**
- * The font
- *
- * @var string
- */
- protected string $font;
-
- /**
- * The fonts array
- *
- * @var array
- */
- protected array $fonts;
-
- /**
- * The public path
- *
- * @var string
- */
- protected string $path;
-
- /**
- * The prefix
- *
- * @var string
- */
- protected string $prefix;
-
- /**
- * The RouterInterface instance
+ * Alias string
*/
- protected RouterInterface $router;
+ protected string $alias;
/**
- * The size
- *
- * @var int
+ * Config array
*/
- protected int $size;
-
- /**
- * The source
- *
- * @var ?string
- */
- protected ?string $source;
-
- /**
- * The stroke
- *
- * @var string
- */
- protected string $stroke;
-
- /**
- * The width
- *
- * @var int
- */
- protected int $width;
+ protected array $config;
/**
* Creates a new facebook util
*
+ * @param ContainerInterface $container The container instance
* @param RouterInterface $router The RouterInterface instance
- * @param string $cache The cache directory
- * @param string $path The public path
- * @param string $prefix The prefix
- * @param ?string $source The source
- * @param array $fonts The fonts
- * @param string $font The font
- * @param int $size The size
- * @param int $width The width
- * @param string $fill The fill
- * @param string $stroke The stroke
- * @param string $align The align
+ * @param SluggerUtil $slugger The SluggerUtil instance
*/
- function __construct(RouterInterface $router, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'facebook', ?string $source = null, array $fonts = [ 'default' => 'ttf/default.ttf' ], string $font = 'default', int $size = 60, int $width = 15, string $fill = 'white', string $stroke = '#00c3f9', string $align = 'center') {
- //Set align
- $this->align = $align;
-
- //Set cache
- $this->cache = $cache.'/'.$prefix;
-
- //Set fill
- $this->fill = $fill;
-
- //Set font
- $this->font = $font;
-
- //Set fonts
- $this->fonts = $fonts;
-
- //Set path
- $this->path = $path.'/'.$prefix;
-
- //Set prefix key
- $this->prefix = $prefix;
-
- //Set router
- $this->router = $router;
-
- //Set size
- $this->size = $size;
-
- //Set source
- $this->source = $source;
-
- //Set stroke
- $this->stroke = $stroke;
-
- //Set width
- $this->width = $width;
+ public function __construct(protected ContainerInterface $container, protected RouterInterface $router, protected SluggerUtil $slugger) {
+ //Retrieve config
+ $this->config = $container->getParameter($this->alias = RapsysPackBundle::getAlias());
}
/**
*
* Generate simple image in jpeg format or load it from cache
*
- * @param string $pathInfo The request path info
+ * @TODO: move to a svg merging system ?
+ *
+ * @param string $path The request path info
* @param array $texts The image texts
* @param int $updated The updated timestamp
* @param ?string $source The image source
- * @param int $width The width
- * @param int $height The height
+ * @param ?int $height The height
+ * @param ?int $width The width
* @return array The image array
*/
- public function getImage(string $pathInfo, array $texts, int $updated, ?string $source = null, int $width = 1200, int $height = 630): array {
+ public function getImage(string $path, array $texts, int $updated, ?string $source = null, ?int $height = null, ?int $width = null): array {
//Without source
- if ($source === null && $this->source === null) {
+ if ($source === null && $this->config['facebook']['source'] === null) {
//Return empty image data
return [];
//Without local source
} elseif ($source === null) {
//Set local source
- $source = $this->source;
+ $source = $this->config['facebook']['source'];
+ }
+
+ //Without width
+ if ($width === null) {
+ //Set width from config
+ $width = $this->config['facebook']['width'];
+ }
+
+ //Without height
+ if ($height === null) {
+ //Set height from config
+ $height = $this->config['facebook']['height'];
}
//Set path file
- $path = $this->path.$pathInfo.'.jpeg';
+ $facebook = $this->config['cache'].'/'.$this->config['prefixes']['facebook'].$path.'.jpeg';
//Without existing path
- if (!is_dir($dir = dirname($path))) {
+ if (!is_dir($dir = dirname($facebook))) {
//Create filesystem object
$filesystem = new Filesystem();
}
//With path file
- if (is_file($path) && ($mtime = stat($path)['mtime']) && $mtime >= $updated) {
+ if (is_file($facebook) && ($mtime = stat($facebook)['mtime']) && $mtime >= $updated) {
#XXX: we used to drop texts with $data['canonical'] === true !!!
+ //Set short path
+ $short = $this->slugger->short($path);
+
+ //Set hash
+ $hash = $this->slugger->serialize([$short, $height, $width]);
+
//Return image data
return [
- 'og:image' => $this->router->generate('rapsys_pack_facebook', ['mtime' => $mtime, 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL),
+ 'og:image' => $this->router->generate('rapsyspack_facebook', ['hash' => $hash, 'path' => $short, 'height' => $height, 'width' => $width, 'u' => $mtime], UrlGeneratorInterface::ABSOLUTE_URL),
'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))),
'og:image:height' => $height,
'og:image:width' => $width
}
//Set cache path
- $cache = $this->cache.$pathInfo.'.png';
+ $cache = $this->config['cache'].'/'.$this->config['prefixes']['facebook'].$path.'.png';
//Without cache path
if (!is_dir($dir = dirname($cache))) {
//Without source
if (!is_file($source)) {
//Throw error
- throw new \Exception(sprintf('Source file "%s" do not exists', $this->source));
+ throw new \Exception(sprintf('Source file "%s" do not exists', $source));
}
//Convert to absolute path
//Draw each text stroke
foreach($texts as $text => $data) {
//Set font
- $draw->setFont($this->fonts[$data['font']??$this->font]);
+ $draw->setFont($this->config['fonts'][$data['font']??$this->config['facebook']['font']]);
//Set font size
- $draw->setFontSize($data['size']??$this->size);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
//Set stroke width
- $draw->setStrokeWidth($data['width']??$this->width);
+ $draw->setStrokeWidth($data['thickness']??$this->config['facebook']['thickness']);
//Set text alignment
- $draw->setTextAlignment($align = ($aligns[$data['align']??$this->align]));
+ $draw->setTextAlignment($align = ($aligns[$data['align']??$this->config['facebook']['align']]));
//Get font metrics
$metrics = $image->queryFontMetrics($draw, $text);
$texts[$text]['y'] = $data['y'] += $metrics['ascender'] - $metrics['textHeight']/2;
//Set stroke color
- $draw->setStrokeColor(new \ImagickPixel($data['stroke']??$this->stroke));
+ $draw->setStrokeColor(new \ImagickPixel($data['border']??$this->config['facebook']['border']));
//Set fill color
- $draw->setFillColor(new \ImagickPixel($data['stroke']??$this->stroke));
+ $draw->setFillColor(new \ImagickPixel($data['fill']??$this->config['facebook']['fill']));
//Add annotation
$draw->annotation($data['x'], $data['y'], $text);
//Draw each text
foreach($texts as $text => $data) {
//Set font
- $draw->setFont($this->fonts[$data['font']??$this->font]);
+ $draw->setFont($this->config['fonts'][$data['font']??$this->config['facebook']['font']]);
//Set font size
- $draw->setFontSize($data['size']??$this->size);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
//Set text alignment
- $draw->setTextAlignment($aligns[$data['align']??$this->align]);
+ $draw->setTextAlignment($aligns[$data['align']??$this->config['facebook']['align']]);
//Set fill color
- $draw->setFillColor(new \ImagickPixel($data['fill']??$this->fill));
+ $draw->setFillColor(new \ImagickPixel($data['fill']??$this->config['facebook']['fill']));
//Add annotation
$draw->annotation($data['x'], $data['y'], $text);
$image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
//Save image
- if (!$image->writeImage($path)) {
+ if (!$image->writeImage($facebook)) {
//Throw error
- throw new \Exception(sprintf('Unable to write image "%s"', $path));
+ throw new \Exception(sprintf('Unable to write image "%s"', $facebook));
}
+ //Set short path
+ $short = $this->slugger->short($path);
+
+ //Set hash
+ $hash = $this->slugger->serialize([$short, $height, $width]);
+
//Return image data
return [
- 'og:image' => $this->router->generate('rapsys_pack_facebook', ['mtime' => stat($path)['mtime'], 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL),
+ 'og:image' => $this->router->generate('rapsyspack_facebook', ['hash' => $hash, 'path' => $short, 'height' => $height, 'width' => $width, 'u' => stat($facebook)['mtime']], UrlGeneratorInterface::ABSOLUTE_URL),
'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))),
'og:image:height' => $height,
'og:image:width' => $width