@@ -361,8 +361,10 @@ public function createFromImage(string $image): GdImage|false
361361 parse_str (parse_url ($ image )['query ' ] ?? '' , $ query );
362362 $ path = is_array ($ query ['dir ' ]) ? $ query ['dir ' ]['0 ' ] : $ query ['dir ' ];
363363 $ image = ImageUtility::getRandomImageFromPath ($ path );
364- } elseif (PathUtility::isAbsolutePath (PathUtility::getAbsolutePath ($ image ))) {
365- $ image = PathUtility::getAbsolutePath ($ image );
364+ }
365+
366+ if (!file_exists ($ image )) {
367+ $ image = PathUtility::resolveFilePath ($ image );
366368 }
367369
368370 $ resource = imagecreatefromstring ((string )file_get_contents ($ image ));
@@ -723,11 +725,13 @@ public function applyFrame(GdImage $sourceResource): GdImage
723725 public function applyText (GdImage $ sourceResource ): GdImage
724726 {
725727 try {
728+ $ fontPath = PathUtility::getAbsolutePath ($ this ->fontPath );
729+ $ tempFontPath = $ _SERVER ['DOCUMENT_ROOT ' ] . '/tempfont.ttf ' ;
730+ $ isTempFont = false ;
726731 $ fontSize = $ this ->fontSize ;
727732 $ fontRotation = $ this ->fontRotation ;
728733 $ fontLocationX = $ this ->fontLocationX ;
729734 $ fontLocationY = $ this ->fontLocationY ;
730- $ fontPath = PathUtility::getAbsolutePath ($ this ->fontPath );
731735 $ textLineSpacing = $ this ->textLineSpacing ;
732736 // Convert hex color string to RGB values
733737 $ colorComponents = self ::getColorComponents ($ this ->fontColor );
@@ -736,17 +740,22 @@ public function applyText(GdImage $sourceResource): GdImage
736740 // Allocate color and set font
737741 $ color = intval (imagecolorallocate ($ sourceResource , $ r , $ g , $ b ));
738742
739- $ localFontPath = $ fontPath ;
740- $ tempFontPath = $ _SERVER ['DOCUMENT_ROOT ' ] . '/tempfont.ttf ' ;
741- if (PathUtility::isUrl ($ fontPath )) {
742- $ font = file_get_contents ($ fontPath );
743- file_put_contents ($ tempFontPath , $ font );
744- $ localFontPath = $ tempFontPath ;
743+ if (PathUtility::isUrl ($ this ->fontPath )) {
744+ $ font = @file_get_contents ($ this ->fontPath );
745+
746+ if ($ font === false ) {
747+ throw new \Exception ('Failed to download font from: ' . $ this ->fontPath );
748+ }
749+ file_put_contents ($ tempFontPath , $ this ->fontPath );
750+ $ fontPath = $ tempFontPath ;
751+ $ isTempFont = true ;
752+ } else {
753+ $ fontPath = PathUtility::resolveFilePath ($ this ->fontPath );
745754 }
746755
747756 // Add first line of text
748757 if (!empty ($ this ->textLine1 )) {
749- if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ fontLocationX , $ fontLocationY , $ color , $ localFontPath , $ this ->textLine1 )) {
758+ if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ fontLocationX , $ fontLocationY , $ color , $ fontPath , $ this ->textLine1 )) {
750759 throw new \Exception ('Could not add first line of text to resource. ' );
751760 }
752761 }
@@ -755,7 +764,7 @@ public function applyText(GdImage $sourceResource): GdImage
755764 if (!empty ($ this ->textLine2 )) {
756765 $ line2Y = $ fontRotation < 45 && $ fontRotation > -45 ? $ fontLocationY + $ textLineSpacing : $ fontLocationY ;
757766 $ line2X = $ fontRotation < 45 && $ fontRotation > -45 ? $ fontLocationX : $ fontLocationX + $ textLineSpacing ;
758- if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ line2X , $ line2Y , $ color , $ localFontPath , $ this ->textLine2 )) {
767+ if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ line2X , $ line2Y , $ color , $ fontPath , $ this ->textLine2 )) {
759768 throw new \Exception ('Could not add second line of text to resource. ' );
760769 }
761770 }
@@ -764,13 +773,15 @@ public function applyText(GdImage $sourceResource): GdImage
764773 if (!empty ($ this ->textLine3 )) {
765774 $ line3Y = $ fontRotation < 45 && $ fontRotation > -45 ? $ fontLocationY + $ textLineSpacing * 2 : $ fontLocationY ;
766775 $ line3X = $ fontRotation < 45 && $ fontRotation > -45 ? $ fontLocationX : $ fontLocationX + $ textLineSpacing * 2 ;
767- if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ line3X , $ line3Y , $ color , $ localFontPath , $ this ->textLine3 )) {
776+ if (!imagettftext ($ sourceResource , $ fontSize , $ fontRotation , $ line3X , $ line3Y , $ color , $ fontPath , $ this ->textLine3 )) {
768777 throw new \Exception ('Could not add third line of text to resource. ' );
769778 }
770779 }
771780
772- if ($ localFontPath !== $ fontPath ) {
773- unlink ($ tempFontPath );
781+ if ($ isTempFont && file_exists ($ tempFontPath )) {
782+ if (!unlink ($ tempFontPath )) {
783+ $ this ->addErrorData ('Failed to delete tmp font: ' . $ tempFontPath );
784+ }
774785 }
775786 $ this ->imageModified = true ;
776787 // Return resource with text applied
0 commit comments