X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/17218a9f78d87562272e141d6d16597a2d5866e9..1dca397fd4da988e8a9851f29ffcf808696a9b4f:/Util/FacebookUtil.php diff --git a/Util/FacebookUtil.php b/Util/FacebookUtil.php index e2c891c..91664d1 100644 --- a/Util/FacebookUtil.php +++ b/Util/FacebookUtil.php @@ -21,44 +21,39 @@ use Symfony\Component\Routing\RouterInterface; */ class FacebookUtil { /** - * The cache directory - * - * @var string + * The default fonts */ - protected $cache; + const fonts = [ 'default' => 'ttf/default.ttf' ]; /** - * The fonts array - * - * @var array + * The default font */ - protected $fonts; + const font = 'default'; /** - * The public path - * - * @var string + * The default font size */ - protected $path; + const size = 60; /** - * The prefix - * - * @var string + * The default width */ - protected $prefix; + const width = 15; /** - * The RouterInterface instance + * The default fill */ - protected RouterInterface $router; + const fill = 'white'; /** - * The source - * - * @var string + * The default stroke + */ + const stroke = '#00c3f9'; + + /** + * The default align */ - protected $source; + const align = 'center'; /** * Creates a new facebook util @@ -67,25 +62,16 @@ class FacebookUtil { * @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 */ - function __construct(RouterInterface $router, string $cache = '../var/cache', string $path = './bundles/rapsyspack', 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 router - $this->router = $router; - - //Set prefix key - $this->prefix = $prefix; - - //Set source - $this->source = $source; + 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) { } /** @@ -102,8 +88,18 @@ class FacebookUtil { * @return array The image array */ public function getImage(string $pathInfo, array $texts, int $updated, ?string $source = null, int $width = 1200, int $height = 630): array { + //Without source + if ($source === null && $this->source === null) { + //Return empty image data + return []; + //Without local source + } elseif ($source === null) { + //Set local source + $source = $this->source; + } + //Set path file - $path = $this->path.$pathInfo.'.jpeg'; + $path = $this->path.'/'.$this->prefix.$pathInfo.'.jpeg'; //Without existing path if (!is_dir($dir = dirname($path))) { @@ -126,7 +122,7 @@ class FacebookUtil { //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', ['mtime' => $mtime, 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), 'og:image:height' => $height, 'og:image:width' => $width @@ -134,7 +130,7 @@ class FacebookUtil { } //Set cache path - $cache = $this->cache.$pathInfo.'.png'; + $cache = $this->cache.'/'.$this->prefix.$pathInfo.'.png'; //Without cache path if (!is_dir($dir = dirname($cache))) { @@ -151,12 +147,6 @@ class FacebookUtil { } } - //Without source - if ($source === null) { - //Set source - $source = realpath($this->source); - } - //Create image object $image = new \Imagick(); @@ -177,7 +167,17 @@ class FacebookUtil { } } + //Without source + if (!is_file($source)) { + //Throw error + throw new \Exception(sprintf('Source file "%s" do not exists', $this->source)); + } + + //Convert to absolute path + $source = realpath($source); + //Read image + //XXX: Imagick::readImage only supports absolute path $image->readImage($source); //Crop using aspect ratio @@ -214,24 +214,6 @@ class FacebookUtil { '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; @@ -241,16 +223,16 @@ class FacebookUtil { //Draw each text stroke foreach($texts as $text => $data) { //Set font - $draw->setFont($this->fonts[$data['font']??$defaultFont]); + $draw->setFont($this->fonts[$data['font']??$this->font]); //Set font size - $draw->setFontSize($data['size']??$defaultSize); + $draw->setFontSize($data['size']??$this->size); //Set stroke width - $draw->setStrokeWidth($data['width']??$defaultWidth); + $draw->setStrokeWidth($data['width']??$this->width); //Set text alignment - $draw->setTextAlignment($align = ($aligns[$data['align']??$defaultAlign])); + $draw->setTextAlignment($align = ($aligns[$data['align']??$this->align])); //Get font metrics $metrics = $image->queryFontMetrics($draw, $text); @@ -278,10 +260,10 @@ class FacebookUtil { $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['stroke']??$this->stroke)); //Set fill color - $draw->setFillColor(new \ImagickPixel($data['stroke']??$defaultStroke)); + $draw->setFillColor(new \ImagickPixel($data['stroke']??$this->stroke)); //Add annotation $draw->annotation($data['x'], $data['y'], $text); @@ -325,16 +307,16 @@ class FacebookUtil { //Draw each text foreach($texts as $text => $data) { //Set font - $draw->setFont($this->fonts[$data['font']??$defaultFont]); + $draw->setFont($this->fonts[$data['font']??$this->font]); //Set font size - $draw->setFontSize($data['size']??$defaultSize); + $draw->setFontSize($data['size']??$this->size); //Set text alignment - $draw->setTextAlignment($aligns[$data['align']??$defaultAlign]); + $draw->setTextAlignment($aligns[$data['align']??$this->align]); //Set fill color - $draw->setFillColor(new \ImagickPixel($data['fill']??$defaultFill)); + $draw->setFillColor(new \ImagickPixel($data['fill']??$this->fill)); //Add annotation $draw->annotation($data['x'], $data['y'], $text); @@ -366,7 +348,7 @@ class FacebookUtil { //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', ['mtime' => stat($path)['mtime'], 'path' => $pathInfo], UrlGeneratorInterface::ABSOLUTE_URL), 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), 'og:image:height' => $height, 'og:image:width' => $width