]> Raphaël G. Git Repositories - packbundle/commitdiff
Add to constructor font, size, width, fill, stroke and align arguments
authorRaphaël Gertz <git@rapsys.eu>
Sat, 29 Jul 2023 10:36:58 +0000 (12:36 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Sat, 29 Jul 2023 10:36:58 +0000 (12:36 +0200)
Return empty array without source image
Throw file do not exists exception when source file is missing

Util/FacebookUtil.php

index e2c891c84c98b5dcc28dd9ed25bb941e9dd44274..18f37d31e03beee1f0cf09e306762d7cb2ffc7d8 100644 (file)
@@ -20,45 +20,87 @@ use Symfony\Component\Routing\RouterInterface;
  * Helps manage facebook images
  */
 class FacebookUtil {
  * Helps manage facebook images
  */
 class FacebookUtil {
+       /**
+        * The align
+        *
+        * @var string
+        */
+       protected string $align;
+
        /**
         * The cache directory
         *
         * @var string
         */
        /**
         * The cache directory
         *
         * @var string
         */
-       protected $cache;
+       protected string $cache;
+
+       /**
+        * The fill
+        *
+        * @var string
+        */
+       protected string $fill;
+
+       /**
+        * The font
+        *
+        * @var string
+        */
+       protected string $font;
 
        /**
         * The fonts array
         *
         * @var array
         */
 
        /**
         * The fonts array
         *
         * @var array
         */
-       protected $fonts;
+       protected array $fonts;
 
        /**
         * The public path
         *
         * @var string
         */
 
        /**
         * The public path
         *
         * @var string
         */
-       protected $path;
+       protected string $path;
 
        /**
         * The prefix
         *
         * @var string
         */
 
        /**
         * The prefix
         *
         * @var string
         */
-       protected $prefix;
+       protected string $prefix;
 
        /**
         * The RouterInterface instance
         */
        protected RouterInterface $router;
 
 
        /**
         * The RouterInterface instance
         */
        protected RouterInterface $router;
 
+       /**
+        * The size
+        *
+        * @var int
+        */
+       protected int $size;
+
        /**
         * The source
         *
        /**
         * The source
         *
+        * @var ?string
+        */
+       protected ?string $source;
+
+       /**
+        * The stroke
+        *
         * @var string
         */
         * @var string
         */
-       protected $source;
+       protected string $stroke;
+
+       /**
+        * The width
+        *
+        * @var int
+        */
+       protected int $width;
 
        /**
         * Creates a new facebook util
 
        /**
         * Creates a new facebook util
@@ -67,25 +109,51 @@ class FacebookUtil {
         * @param string $cache The cache directory
         * @param string $path The public path
         * @param string $prefix The prefix
         * @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' ]) {
+       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 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 fonts
                $this->fonts = $fonts;
 
                //Set path
                $this->path = $path.'/'.$prefix;
 
+               //Set prefix key
+               $this->prefix = $prefix;
+
                //Set router
                $this->router = $router;
 
                //Set router
                $this->router = $router;
 
-               //Set prefix key
-               $this->prefix = $prefix;
+               //Set size
+               $this->size = $size;
 
                //Set source
                $this->source = $source;
 
                //Set source
                $this->source = $source;
+
+               //Set stroke
+               $this->stroke = $stroke;
+
+               //Set width
+               $this->width = $width;
        }
 
        /**
        }
 
        /**
@@ -102,6 +170,16 @@ 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 {
         * @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';
 
                //Set path file
                $path = $this->path.$pathInfo.'.jpeg';
 
@@ -151,12 +229,6 @@ class FacebookUtil {
                        }
                }
 
                        }
                }
 
-               //Without source
-               if ($source === null) {
-                       //Set source
-                       $source = realpath($this->source);
-               }
-
                //Create image object
                $image = new \Imagick();
 
                //Create image object
                $image = new \Imagick();
 
@@ -177,7 +249,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
                        //Read image
+                       //XXX: Imagick::readImage only supports absolute path
                        $image->readImage($source);
 
                        //Crop using aspect ratio
                        $image->readImage($source);
 
                        //Crop using aspect ratio
@@ -214,24 +296,6 @@ class FacebookUtil {
                        'right' => \Imagick::ALIGN_RIGHT
                ];
 
                        '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;
 
                //Init counter
                $i = 1;
 
@@ -241,16 +305,16 @@ class FacebookUtil {
                //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']??$defaultFont]);
+                       $draw->setFont($this->fonts[$data['font']??$this->font]);
 
                        //Set font size
 
                        //Set font size
-                       $draw->setFontSize($data['size']??$defaultSize);
+                       $draw->setFontSize($data['size']??$this->size);
 
                        //Set stroke width
 
                        //Set stroke width
-                       $draw->setStrokeWidth($data['width']??$defaultWidth);
+                       $draw->setStrokeWidth($data['width']??$this->width);
 
                        //Set text alignment
 
                        //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);
 
                        //Get font metrics
                        $metrics = $image->queryFontMetrics($draw, $text);
@@ -278,10 +342,10 @@ class FacebookUtil {
                        $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']??$defaultStroke));
+                       $draw->setStrokeColor(new \ImagickPixel($data['stroke']??$this->stroke));
 
                        //Set fill color
 
                        //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);
 
                        //Add annotation
                        $draw->annotation($data['x'], $data['y'], $text);
@@ -325,16 +389,16 @@ class FacebookUtil {
                //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']??$defaultFont]);
+                       $draw->setFont($this->fonts[$data['font']??$this->font]);
 
                        //Set font size
 
                        //Set font size
-                       $draw->setFontSize($data['size']??$defaultSize);
+                       $draw->setFontSize($data['size']??$this->size);
 
                        //Set text alignment
 
                        //Set text alignment
-                       $draw->setTextAlignment($aligns[$data['align']??$defaultAlign]);
+                       $draw->setTextAlignment($aligns[$data['align']??$this->align]);
 
                        //Set fill color
 
                        //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);
 
                        //Add annotation
                        $draw->annotation($data['x'], $data['y'], $text);