Replace member variables by config parameters
Cleanup
namespace Rapsys\PackBundle\Util;
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;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
*/
class FacebookUtil {
/**
*/
class FacebookUtil {
/**
- * The default fonts
- */
- const fonts = [ 'default' => 'ttf/default.ttf' ];
-
- /**
- * The default font
- */
- const font = 'default';
-
- /**
- * The default font size
- */
- const size = 60;
-
- /**
- * The default width
- */
- const width = 15;
-
- /**
- * The default fill
- */
- const fill = 'white';
-
- /**
- * The default stroke
- const stroke = '#00c3f9';
+ protected string $alias;
- const align = 'center';
+ protected array $config;
/**
* Creates a new facebook util
*
/**
* Creates a new facebook util
*
+ * @param ContainerInterface $container The container instance
* @param RouterInterface $router The RouterInterface 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(protected RouterInterface $router, protected string $cache = '../var/cache', protected string $path = './bundles/rapsyspack', protected string $prefix = 'facebook', protected ?string $source = null, protected array $fonts = self::fonts, protected string $font = self::font, protected int $size = self::size, protected int $width = self::width, protected string $fill = self::fill, protected string $stroke = self::stroke, protected string $align = self::align) {
+ 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
*
*
* 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 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
*/
* @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 {
- 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
//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'];
- $path = $this->path.'/'.$this->prefix.$pathInfo.'.jpeg';
+ $facebook = $this->config['cache'].'/'.$this->config['prefixes']['facebook'].$path.'.jpeg';
- if (!is_dir($dir = dirname($path))) {
+ if (!is_dir($dir = dirname($facebook))) {
//Create filesystem object
$filesystem = new Filesystem();
//Create filesystem object
$filesystem = new Filesystem();
- 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 !!!
#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 [
//Return image data
return [
- 'og:image' => $this->router->generate('rapsyspack_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
'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))),
'og:image:height' => $height,
'og:image:width' => $width
- $cache = $this->cache.'/'.$this->prefix.$pathInfo.'.png';
+ $cache = $this->config['cache'].'/'.$this->config['prefixes']['facebook'].$path.'.png';
//Without cache path
if (!is_dir($dir = dirname($cache))) {
//Without cache path
if (!is_dir($dir = dirname($cache))) {
//Without source
if (!is_file($source)) {
//Throw error
//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
}
//Convert to absolute path
//Draw each text stroke
foreach($texts as $text => $data) {
//Set font
//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']]);
- $draw->setFontSize($data['size']??$this->size);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
- $draw->setStrokeWidth($data['width']??$this->width);
+ $draw->setStrokeWidth($data['thickness']??$this->config['facebook']['thickness']);
- $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);
//Get font metrics
$metrics = $image->queryFontMetrics($draw, $text);
$texts[$text]['y'] = $data['y'] += $metrics['ascender'] - $metrics['textHeight']/2;
//Set stroke color
$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']));
- $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);
//Add annotation
$draw->annotation($data['x'], $data['y'], $text);
//Draw each text
foreach($texts as $text => $data) {
//Set font
//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']]);
- $draw->setFontSize($data['size']??$this->size);
+ $draw->setFontSize($data['size']??$this->config['facebook']['size']);
- $draw->setTextAlignment($aligns[$data['align']??$this->align]);
+ $draw->setTextAlignment($aligns[$data['align']??$this->config['facebook']['align']]);
- $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);
//Add annotation
$draw->annotation($data['x'], $data['y'], $text);
$image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
//Save image
$image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
//Save image
- if (!$image->writeImage($path)) {
+ if (!$image->writeImage($facebook)) {
- 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 [
//Return image data
return [
- 'og:image' => $this->router->generate('rapsyspack_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
'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))),
'og:image:height' => $height,
'og:image:width' => $width