namespace Rapsys\PackBundle\Util;
-use Symfony\Component\Asset\PackageInterface;
+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;
+use Symfony\Component\Routing\RouterInterface;
/**
* Helps manage facebook images
*/
class FacebookUtil {
/**
- * The cache directory
- *
- * @var string
- */
- protected $cache;
-
- /**
- * The fonts array
- *
- * @var array
- */
- protected $fonts;
-
- /**
- * The public path
- *
- * @var string
- */
- protected $path;
-
- /**
- * The public url
- *
- * @var string
- */
- protected $url;
-
- /**
- * The package instance
- *
- * @var PackageInterface
- */
- protected $package;
-
- /**
- * The prefix
- *
- * @var string
+ * Alias string
*/
- protected $prefix;
+ protected string $alias;
/**
- * The source
- *
- * @var string
+ * Config array
*/
- protected $source;
+ protected array $config;
/**
- * Creates a new osm util
+ * Creates a new facebook util
*
- * @param PackageInterface $package The package instance
- * @param string $cache The cache directory
- * @param string $path The public path
- * @param string $url The public url
- * @param string $prefix The prefix
+ * @param ContainerInterface $container The container instance
+ * @param RouterInterface $router The RouterInterface instance
+ * @param SluggerUtil $slugger The SluggerUtil instance
*/
- function __construct(PackageInterface $package, string $cache, string $path, string $url, string $prefix = 'facebook', string $source = 'png/facebook.png', array $fonts = [ 'default' => 'ttf/default.ttf' ]) {
- //Set cache
- $this->cache = $cache.'/'.$prefix;
-
- //Set fonts
- $this->fonts = $fonts;
-
- //Set path
- $this->path = $path.'/'.$prefix;
-
- //Set url
- $this->url = $url.'/'.$prefix;
-
- //Set package instance
- $this->package = $package;
-
- //Set prefix key
- $this->prefix = $prefix;
-
- //Set source
- $this->source = $source;
+ 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->config['facebook']['source'] === null) {
+ //Return empty image data
+ return [];
+ //Without local source
+ } elseif ($source === null) {
+ //Set local 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->package->getAbsoluteUrl('@RapsysAir/facebook/'.$mtime.$pathInfo.'.jpeg'),
- 'og:image' => $this->package->getAbsoluteUrl($this->url.'/'.$mtime.$pathInfo.'.jpeg'),
+ '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 ($source === null) {
- //Set source
- $source = realpath($this->source);
- }
-
//Create image object
$image = new \Imagick();
}
}
+ //Without source
+ if (!is_file($source)) {
+ //Throw error
+ throw new \Exception(sprintf('Source file "%s" do not exists', $source));
+ }
+
+ //Convert to absolute path
+ $source = realpath($source);
+
//Read image
+ //XXX: Imagick::readImage only supports absolute path
$image->readImage($source);
//Crop using aspect ratio
'right' => \Imagick::ALIGN_RIGHT
];
- //Set default font
- $defaultFont = 'dejavusans';
-
- //Set default align
- $defaultAlign = 'center';
-
- //Set default size
- $defaultSize = 60;
-
- //Set default stroke
- $defaultStroke = '#00c3f9';
-
- //Set default width
- $defaultWidth = 15;
-
- //Set default fill
- $defaultFill = 'white';
-
//Init counter
$i = 1;
//Draw each text stroke
foreach($texts as $text => $data) {
//Set font
- $draw->setFont($this->fonts[$data['font']??$defaultFont]);
+ $draw->setFont($this->config['fonts'][$data['font']??$this->config['facebook']['font']]);
//Set font size
- $draw->setFontSize($data['size']??$defaultSize);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
//Set stroke width
- $draw->setStrokeWidth($data['width']??$defaultWidth);
+ $draw->setStrokeWidth($data['thickness']??$this->config['facebook']['thickness']);
//Set text alignment
- $draw->setTextAlignment($align = ($aligns[$data['align']??$defaultAlign]));
+ $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']??$defaultStroke));
+ $draw->setStrokeColor(new \ImagickPixel($data['border']??$this->config['facebook']['border']));
//Set fill color
- $draw->setFillColor(new \ImagickPixel($data['stroke']??$defaultStroke));
+ $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']??$defaultFont]);
+ $draw->setFont($this->config['fonts'][$data['font']??$this->config['facebook']['font']]);
//Set font size
- $draw->setFontSize($data['size']??$defaultSize);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
//Set text alignment
- $draw->setTextAlignment($aligns[$data['align']??$defaultAlign]);
+ $draw->setTextAlignment($aligns[$data['align']??$this->config['facebook']['align']]);
//Set fill color
- $draw->setFillColor(new \ImagickPixel($data['fill']??$defaultFill));
+ $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->package->getAbsoluteUrl($this->url.'/'.stat($path)['mtime'].$pathInfo.'.jpeg'),
+ '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