@@ -318,7 +318,7 @@ public function curl(string $url, array $curlOptions = [], bool $failOnError = f
318
318
319
319
$ image = \curl_exec ($ curl );
320
320
321
- if ($ failOnError && \curl_errno ($ curl )){
321
+ if ($ failOnError && \curl_errno ($ curl )) {
322
322
throw new \Exception (\curl_error ($ curl ));
323
323
}
324
324
@@ -635,22 +635,22 @@ private static function formatColor(string $stringColor): string
635
635
* Allocate a new color to the image.
636
636
*
637
637
* @param string $color Hexadecimal string color
638
- * @return int Color id
638
+ * @return int|false Color id
639
639
*/
640
- private function colorAllocate (string $ color ): int
640
+ private function colorAllocate (string $ color )
641
641
{
642
642
$ color = static ::formatColor ($ color );
643
643
$ red = \hexdec (\substr ($ color , 0 , 2 ));
644
644
$ green = \hexdec (\substr ($ color , 2 , 2 ));
645
645
$ blue = \hexdec (\substr ($ color , 4 , 2 ));
646
646
$ alpha = \floor (\hexdec (\substr ($ color , 6 , 2 )) / 2 );
647
647
648
- $ newColor = \imagecolorexactalpha ($ this ->image , $ red , $ green , $ blue , $ alpha );
649
- if ($ newColor === -1 ) {
650
- $ newColor = \imagecolorallocatealpha ($ this ->image , $ red , $ green , $ blue , $ alpha );
648
+ $ colorId = \imagecolorexactalpha ($ this ->image , $ red , $ green , $ blue , $ alpha );
649
+ if ($ colorId === -1 ) {
650
+ $ colorId = \imagecolorallocatealpha ($ this ->image , $ red , $ green , $ blue , $ alpha );
651
651
}
652
652
653
- return $ newColor ;
653
+ return $ colorId ;
654
654
}
655
655
656
656
@@ -801,18 +801,19 @@ public function grayscale(): Image
801
801
*
802
802
* @param string $string Text to be added on the image
803
803
* @param string $fontPath Path to the TTF file
804
- * @param int $fontSize Font size
804
+ * @param float $fontSize Font size
805
805
* @param string $color Hexadecimal string color
806
- * @param int|string $posX Left position in pixel. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
807
- * @param int|string $posY Top position in pixel. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
808
- * @param int|string $anchorX Horizontal anchor of the text. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
809
- * @param int|string $anchorY Vertical anchor of the text. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
810
- * @param int $rotation Counterclockwise text rotation in degrees
806
+ * @param float|string $posX Left position in pixel. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
807
+ * @param float|string $posY Top position in pixel. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
808
+ * @param float|string $anchorX Horizontal anchor of the text. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
809
+ * @param float|string $anchorY Vertical anchor of the text. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
810
+ * @param float $rotation Counterclockwise text rotation in degrees
811
+ * @param float $letterSpacing add space between letters
811
812
* @return $this Fluent interface
812
813
*/
813
- public function writeText (string $ string , string $ fontPath , int $ fontSize , string $ color = '# ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 , int $ letter_spacing = 0 ): Image
814
+ public function writeText (string $ string , string $ fontPath , float $ fontSize , string $ color = 'ffffff ' , $ posX = 0 , $ posY = 0 , $ anchorX = Image::ALIGN_CENTER , $ anchorY = Image::ALIGN_MIDDLE , float $ rotation = 0 , float $ letterSpacing = 0 ): Image
814
815
{
815
- $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation , $ letter_spacing );
816
+ $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation , $ letterSpacing );
816
817
return $ this ;
817
818
}
818
819
@@ -821,16 +822,17 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
821
822
*
822
823
* @param string $string Text to be added on the image
823
824
* @param string $fontPath Path to the TTF file
824
- * @param int $fontSize Font size
825
+ * @param float $fontSize Font size
825
826
* @param string $color Hexadecimal string color
826
- * @param int|string $posX Left position in pixel. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
827
- * @param int|string $posY Top position in pixel. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
828
- * @param int|string $anchorX Horizontal anchor of the text. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
829
- * @param int|string $anchorY Vertical anchor of the text. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
830
- * @param int $rotation Counterclockwise text rotation in degrees
831
- * @return array Pixels positions of the
832
- */
833
- public function writeTextAndGetBoundingBox (string $ string , string $ fontPath , int $ fontSize , string $ color = '#ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 , int $ letter_spacing = 0 ): array
827
+ * @param float|string $posX Left position in pixel. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
828
+ * @param float|string $posY Top position in pixel. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
829
+ * @param float|string $anchorX Horizontal anchor of the text. You can use `Image::ALIGN_LEFT`, `Image::ALIGN_CENTER`, `Image::ALIGN_RIGHT`
830
+ * @param float|string $anchorY Vertical anchor of the text. You can use `Image::ALIGN_TOP`, `Image::ALIGN_MIDDLE`, `Image::ALIGN_BOTTOM`
831
+ * @param float $rotation Counterclockwise text rotation in degrees
832
+ * @param float $letterSpacing add space between letters
833
+ * @return array Bounding box positions of the text
834
+ */
835
+ public function writeTextAndGetBoundingBox (string $ string , string $ fontPath , float $ fontSize , string $ color = 'ffffff ' , $ posX = 0 , $ posY = 0 , $ anchorX = Image::ALIGN_CENTER , $ anchorY = Image::ALIGN_MIDDLE , float $ rotation = 0 , float $ letterSpacing = 0 ): array
834
836
{
835
837
if (!$ this ->isImageDefined ()) {
836
838
return [];
@@ -858,7 +860,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
858
860
) {
859
861
if (
860
862
($ newImg = \imagecreatetruecolor (1 , 1 )) === false ||
861
- ($ posText = $ this ->imagettftextWithSpacing ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string , $ letter_spacing )) === false
863
+ ($ posText = $ this ->imagettftextWithSpacing ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string , $ letterSpacing )) === false
862
864
) {
863
865
return [];
864
866
}
@@ -884,6 +886,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
884
886
}
885
887
886
888
$ sizeWidth = $ xMax - $ xMin ;
889
+ $ sizeHeight = $ yMax - $ yMin ;
887
890
888
891
switch ($ anchorX ) {
889
892
case static ::ALIGN_LEFT :
@@ -898,17 +901,18 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
898
901
}
899
902
switch ($ anchorY ) {
900
903
case static ::ALIGN_TOP :
901
- $ posY = $ posY + $ fontSize ;
904
+ $ posY = $ posY - $ yMin ;
902
905
break ;
903
906
case static ::ALIGN_MIDDLE :
904
- $ posY = $ posY + $ fontSize / 2 ;
907
+ $ posY = $ posY - $ sizeHeight / 2 - $ yMin ;
905
908
break ;
906
909
case static ::ALIGN_BOTTOM :
910
+ $ posY = $ posY - $ sizeHeight - $ yMin ;
907
911
break ;
908
912
}
909
913
}
910
914
911
- $ posText = $ this ->imagettftextWithSpacing ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string , $ letter_spacing );
915
+ $ posText = $ this ->imagettftextWithSpacing ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string , $ letterSpacing );
912
916
913
917
if ($ posText === false ) {
914
918
return [];
@@ -943,35 +947,52 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
943
947
944
948
/**
945
949
* @param $image
946
- * @param $size
947
- * @param $angle
948
- * @param $x
949
- * @param $y
950
- * @param $color
951
- * @param $font
952
- * @param $text
953
- * @param int $spacing
954
- * @return array
955
- */
956
- private function imagettftextWithSpacing ($ image , float $ size , float $ angle , float $ x , float $ y , int $ color , string $ font , string $ text , int $ spacing = 0 )
950
+ * @param float $size
951
+ * @param float $angle
952
+ * @param float $x
953
+ * @param float $y
954
+ * @param int $color
955
+ * @param string $font
956
+ * @param string $text
957
+ * @param float $spacing
958
+ * @return array|false
959
+ */
960
+ private function imagettftextWithSpacing ($ image , float $ size , float $ angle , float $ x , float $ y , int $ color , string $ font , string $ text , float $ spacing = 0 )
957
961
{
958
- if ($ spacing == 0 )
959
- {
962
+ if ($ spacing == 0 ) {
960
963
return \imagettftext ($ image , $ size , $ angle , $ x , $ y , $ color , $ font , $ text );
961
- }
962
- else
963
- {
964
- $ temp_x = $ x ;
965
- $ temp_y = $ y ;
966
- $ posText = [];
967
- for ($ i = 0 ; $ i < \mb_strlen ($ text ); ++$ i )
968
- {
969
- $ posText = \imagettftext ($ image , $ size , $ angle , $ temp_x , $ temp_y , $ color , $ font , $ text [$ i ]);
964
+ } else {
965
+ $ length = \mb_strlen ($ text );
966
+
967
+ if ($ length == 0 ) {
968
+ return false ;
969
+ }
970
+
971
+ $ letterPos = ['x ' => $ x , 'y ' => $ y ];
972
+ $ textWidth = $ spacing * ($ length - 1 );
973
+ $ top = 0 ;
974
+ $ bottom = 0 ;
975
+
976
+ for ($ i = 0 ; $ i < $ length ; ++$ i ) {
977
+ \imagettftext ($ image , $ size , $ angle , $ letterPos ['x ' ], $ letterPos ['y ' ], $ color , $ font , $ text [$ i ]);
970
978
$ bbox = \imagettfbbox ($ size , 0 , $ font , $ text [$ i ]);
971
- $ temp_x += \cos (\deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
972
- $ temp_y -= \sin (\deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
979
+ $ letterPos = Geometry2D::getDstXY ($ letterPos ['x ' ], $ letterPos ['y ' ], $ angle , $ spacing + $ bbox [2 ]);
980
+
981
+ $ textWidth += $ bbox [2 ];
982
+ if ($ top > $ bbox [5 ]) {
983
+ $ top = $ bbox [5 ];
984
+ }
985
+ if ($ bottom < $ bbox [1 ]) {
986
+ $ bottom = $ bbox [1 ];
987
+ }
973
988
}
974
- return $ posText ;
989
+
990
+ $ bottomLeft = Geometry2D::getDstXY ($ x , $ y , $ angle - 90 , $ bottom );
991
+ $ bottomRight = Geometry2D::getDstXY ($ bottomLeft ['x ' ], $ bottomLeft ['y ' ], $ angle , $ textWidth );
992
+ $ topLeft = Geometry2D::getDstXY ($ x , $ y , $ angle + 90 , \abs ($ top ));
993
+ $ topRight = Geometry2D::getDstXY ($ topLeft ['x ' ], $ topLeft ['y ' ], $ angle , $ textWidth );
994
+
995
+ return [$ bottomLeft ['x ' ], $ bottomLeft ['y ' ], $ bottomRight ['x ' ], $ bottomRight ['y ' ], $ topRight ['x ' ], $ topRight ['y ' ], $ topLeft ['x ' ], $ topLeft ['y ' ]];
975
996
}
976
997
}
977
998
@@ -1010,7 +1031,7 @@ public function drawRectangle(int $left, int $top, int $right, int $bottom, stri
1010
1031
* @param string $color Hexadecimal string color
1011
1032
* @return $this Fluent interface
1012
1033
*/
1013
- public function drawPolygon (array $ points , string $ color = '# 000000 ' , $ antialias = false ): Image
1034
+ public function drawPolygon (array $ points , string $ color = '000000 ' , $ antialias = false ): Image
1014
1035
{
1015
1036
if (!$ this ->isImageDefined ()) {
1016
1037
return $ this ;
@@ -1022,14 +1043,14 @@ public function drawPolygon(array $points, string $color = '#000000', $antialias
1022
1043
return $ this ;
1023
1044
}
1024
1045
1025
- if ($ antialias ) {
1046
+ if ($ antialias ) {
1026
1047
\imageantialias ($ this ->image , true );
1027
1048
\imagepolygon ($ this ->image , $ points , \count ($ points ) / 2 , $ color );
1028
1049
}
1029
1050
1030
1051
\imagefilledpolygon ($ this ->image , $ points , \count ($ points ) / 2 , $ color );
1031
1052
1032
- if ($ antialias ) {
1053
+ if ($ antialias ) {
1033
1054
\imageantialias ($ this ->image , false );
1034
1055
}
1035
1056
@@ -1280,10 +1301,10 @@ private function getData(callable $imgFunction): string
1280
1301
1281
1302
\ob_start ();
1282
1303
$ imgFunction ();
1283
- $ image_data = \ob_get_contents ();
1304
+ $ imageData = \ob_get_contents ();
1284
1305
\ob_end_clean ();
1285
1306
1286
- return $ image_data ;
1307
+ return $ imageData ;
1287
1308
}
1288
1309
1289
1310
/**
@@ -1293,7 +1314,9 @@ private function getData(callable $imgFunction): string
1293
1314
*/
1294
1315
public function getDataPNG (): string
1295
1316
{
1296
- return $ this ->getData (function () {$ this ->displayPNG ();});
1317
+ return $ this ->getData (function () {
1318
+ $ this ->displayPNG ();
1319
+ });
1297
1320
}
1298
1321
1299
1322
/**
@@ -1304,7 +1327,9 @@ public function getDataPNG(): string
1304
1327
*/
1305
1328
public function getDataJPG (int $ quality = -1 ): string
1306
1329
{
1307
- return $ this ->getData (function () use ($ quality ) {$ this ->displayJPG ($ quality );});
1330
+ return $ this ->getData (function () use ($ quality ) {
1331
+ $ this ->displayJPG ($ quality );
1332
+ });
1308
1333
}
1309
1334
1310
1335
/**
@@ -1314,7 +1339,9 @@ public function getDataJPG(int $quality = -1): string
1314
1339
*/
1315
1340
public function getDataGIF (): string
1316
1341
{
1317
- return $ this ->getData (function () {$ this ->displayGIF ();});
1342
+ return $ this ->getData (function () {
1343
+ $ this ->displayGIF ();
1344
+ });
1318
1345
}
1319
1346
1320
1347
/**
0 commit comments