22
33namespace LasseRafn \InitialAvatarGenerator ;
44
5+ use Intervention \Image \Drivers \Gd \Driver as GdDriver ;
6+ use Intervention \Image \Drivers \Imagick \Driver as ImagickDriver ;
7+ use Intervention \Image \Geometry \Factories \CircleFactory ;
58use Intervention \Image \Image ;
69use Intervention \Image \ImageManager ;
710use LasseRafn \InitialAvatarGenerator \Translator \Base ;
1013use LasseRafn \InitialAvatarGenerator \Translator \ZhCN ;
1114use LasseRafn \Initials \Initials ;
1215use LasseRafn \StringScript ;
16+ use RuntimeException ;
1317use SVG \Nodes \Shapes \SVGCircle ;
1418use SVG \Nodes \Shapes \SVGRect ;
1519use SVG \Nodes \Structures \SVGFont ;
@@ -78,7 +82,15 @@ public function __construct()
7882 */
7983 protected function setupImageManager ()
8084 {
81- $ this ->image = new ImageManager (['driver ' => $ this ->getDriver ()]);
85+ $ driver = $ this ->getDriver ();
86+
87+ $ driverInstance = match ($ driver ) {
88+ 'gd ' => new GdDriver (),
89+ 'imagick ' => new ImagickDriver (),
90+ default => throw new RuntimeException ("Unsupported driver. " ),
91+ };
92+
93+ $ this ->image = new ImageManager ($ driverInstance );
8294 }
8395
8496 /**
@@ -264,9 +276,9 @@ public function autoColor(bool $foreground = true, bool $background = true, int
264276 }
265277
266278 /**
267- * Set the font file by path or int (1-5) .
279+ * Set the font file by path.
268280 *
269- * @param string|int $font
281+ * @param string $font
270282 *
271283 * @return $this
272284 */
@@ -517,7 +529,7 @@ public function getFontSize()
517529 /**
518530 * Will return the font file parameter.
519531 *
520- * @return string|int
532+ * @return string
521533 */
522534 public function getFontFile ()
523535 {
@@ -691,12 +703,21 @@ protected function makeAvatar($image)
691703 $ height *= 5 ;
692704 }
693705
694- $ avatar = $ image ->canvas ($ width , $ height , !$ this ->getRounded () ? $ bgColor : null );
706+ $ avatar = $ image ->create ($ width , $ height );
707+
708+ if (!$ this ->getRounded ()) {
709+ $ avatar = $ avatar ->fill ($ bgColor );
710+ }
695711
696712 if ($ this ->getRounded ()) {
697- $ avatar = $ avatar ->circle ($ width - 2 , $ width / 2 , $ height / 2 , function ($ draw ) use ($ bgColor ) {
698- return $ draw ->background ($ bgColor );
699- });
713+ $ avatar = $ avatar ->drawCircle (
714+ x: $ width / 2 ,
715+ y: $ height / 2 ,
716+ init: function (CircleFactory $ circle ) use ($ width , $ bgColor ) {
717+ $ circle ->radius ($ width -2 );
718+ $ circle ->background ($ bgColor );
719+ }
720+ );
700721 }
701722
702723 if ($ this ->getRounded () && $ this ->getSmooth ()) {
@@ -706,7 +727,10 @@ protected function makeAvatar($image)
706727 }
707728
708729 return $ avatar ->text ($ name , $ width / 2 , $ height / 2 , function ($ draw ) use ($ width , $ color , $ fontFile , $ fontSize ) {
709- $ draw ->file ($ fontFile );
730+ if ($ fontFile !== null ) {
731+ $ draw ->filename ($ fontFile );
732+ }
733+
710734 $ draw ->size ($ width * $ fontSize );
711735 $ draw ->color ($ color );
712736 $ draw ->align ('center ' );
@@ -755,6 +779,10 @@ protected function makeSvgAvatar()
755779 return $ image ;
756780 }
757781
782+
783+ /**
784+ * @return string|null
785+ */
758786 protected function findFontFile ()
759787 {
760788 $ fontFile = $ this ->getFontFile ();
@@ -763,10 +791,6 @@ protected function findFontFile()
763791 $ fontFile = $ this ->getFontByScript ();
764792 }
765793
766- if (is_int ($ fontFile ) && \in_array ($ fontFile , [1 , 2 , 3 , 4 , 5 ], false )) {
767- return $ fontFile ;
768- }
769-
770794 $ weightsToTry = ['Regular ' ];
771795
772796 if ($ this ->preferBold ) {
@@ -791,7 +815,7 @@ protected function findFontFile()
791815 }
792816 }
793817
794- return 1 ;
818+ return null ;
795819 }
796820
797821 protected function getFontByScript ()
0 commit comments