@@ -130,14 +130,26 @@ public function validCode(string $code, Field $field, bool $clearSession = false
130130 protected function createImage (string $ content , bool $ addHash = true ): string
131131 {
132132 $ imageResource = imagecreatefrompng ($ this ->getBackgroundImage ());
133+ $ textSize = (float )$ this ->configuration ['textSize ' ];
134+ $ angle = $ this ->getFontAngleForCaptcha ();
135+ $ fontPathAndFilename = $ this ->getFontPathAndFilename ();
136+
137+ [$ horizontalPosition , $ verticalPosition ] = $ this ->getTextPositionForCaptcha (
138+ $ imageResource ,
139+ $ textSize ,
140+ $ angle ,
141+ $ fontPathAndFilename ,
142+ $ content
143+ );
144+
133145 imagettftext (
134146 $ imageResource ,
135- ( float ) $ this -> configuration [ ' textSize ' ] ,
136- $ this -> getFontAngleForCaptcha () ,
137- $ this -> getHorizontalDistanceForCaptcha () ,
138- $ this -> getVerticalDistanceForCaptcha () ,
147+ $ textSize ,
148+ $ angle ,
149+ $ horizontalPosition ,
150+ $ verticalPosition ,
139151 $ this ->getColorForCaptcha ($ imageResource ),
140- $ this -> getFontPathAndFilename () ,
152+ $ fontPathAndFilename ,
141153 $ content
142154 );
143155 if (imagepng ($ imageResource , $ this ->getPathAndFilename (true )) === false ) {
@@ -190,6 +202,53 @@ protected function getVerticalDistanceForCaptcha(): int
190202 return mt_rand ((int )$ distances [0 ], (int )$ distances [1 ]);
191203 }
192204
205+ /**
206+ * Calculate a text position (horizontal and vertical) that keeps the rendered captcha
207+ * string fully within the bounds of the background image, using the configured random
208+ * distances as a starting point and clamping them against the actual glyph bounding box.
209+ *
210+ * @param resource $imageResource
211+ * @return array{0: int, 1: int} [horizontal position, vertical position]
212+ */
213+ protected function getTextPositionForCaptcha (
214+ $ imageResource ,
215+ float $ textSize ,
216+ int $ angle ,
217+ string $ fontPathAndFilename ,
218+ string $ content
219+ ): array {
220+ $ boundingBox = imagettfbbox ($ textSize , $ angle , $ fontPathAndFilename , $ content );
221+ $ minX = min ($ boundingBox [0 ], $ boundingBox [2 ], $ boundingBox [4 ], $ boundingBox [6 ]);
222+ $ maxX = max ($ boundingBox [0 ], $ boundingBox [2 ], $ boundingBox [4 ], $ boundingBox [6 ]);
223+ $ minY = min ($ boundingBox [1 ], $ boundingBox [3 ], $ boundingBox [5 ], $ boundingBox [7 ]);
224+ $ maxY = max ($ boundingBox [1 ], $ boundingBox [3 ], $ boundingBox [5 ], $ boundingBox [7 ]);
225+
226+ $ imageWidth = imagesx ($ imageResource );
227+ $ imageHeight = imagesy ($ imageResource );
228+
229+ $ horizontalPosition = $ this ->clampPositionForCaptcha (
230+ $ this ->getHorizontalDistanceForCaptcha (),
231+ (int )ceil (0 - $ minX ),
232+ (int )floor ($ imageWidth - $ maxX )
233+ );
234+ $ verticalPosition = $ this ->clampPositionForCaptcha (
235+ $ this ->getVerticalDistanceForCaptcha (),
236+ (int )ceil (0 - $ minY ),
237+ (int )floor ($ imageHeight - $ maxY )
238+ );
239+
240+ return [$ horizontalPosition , $ verticalPosition ];
241+ }
242+
243+ /**
244+ * Clamp a random position into the given bounds. If the bounds are inverted (e.g. the
245+ * rendered text is wider/taller than the background image), this resolves to $min.
246+ */
247+ protected function clampPositionForCaptcha (int $ position , int $ min , int $ max ): int
248+ {
249+ return max ($ min , min ($ max , $ position ));
250+ }
251+
193252 /**
194253 * Create Random String for Captcha Image
195254 *
0 commit comments