When handling images and text in PHP, the commonly used GD library offers a rich set of functions to help us dynamically generate graphics. This article focuses on how to combine the imagefontwidth() and imagecopy() functions to achieve a text mapping effect. Text mapping means “cutting” text into small pieces or tiles and then assembling them onto a target image to create a more flexible way of displaying text.
imagefontwidth(int $font): int
Returns the width of built-in font characters, where $font is the font size identifier ranging from 1 to 5. This function is useful for determining text width, making it easier to calculate where to place pieces.
imagecopy(resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h): bool
Copies a portion of the source image to a specified position on the destination image. This can be used to copy and paste individual characters or sliced text segments.
Prepare a source image containing the desired text (usually text written in a small image).
According to the font width and number of characters, extract each character block from the source image one by one.
Use the imagecopy() function to copy each character block sequentially to specified positions on the target image.
Adjust position parameters to achieve a smooth text mapping effect.
<?php
// Create the destination image (white background)
$dst_width = 300;
$dst_height = 50;
$dst_im = imagecreatetruecolor($dst_width, $dst_height);
$white = imagecolorallocate($dst_im, 255, 255, 255);
imagefill($dst_im, 0, 0, $white);
<p>// Source image, assuming it's a text "HELLO" map, width and height equal to character width * number of characters and character height respectively<br>
// In practice, prepare an image resource containing the complete text<br>
$src_im = imagecreatefrompng('<a rel="noopener" target="_new" class="" href="http://m66.net/images/letters.png">http://m66.net/images/letters.png</a>');</p>
<p>// Set font size (built-in font, 1-5)<br>
$font = 5;<br>
$char_width = imagefontwidth($font);<br>
$char_height = imagefontheight($font);</p>
<p>// Text to be mapped<br>
$text = "HELLO";<br>
$len = strlen($text);</p>
<p>// Calculate starting position to center the text<br>
$start_x = ($dst_width - $char_width * $len) / 2;<br>
$start_y = ($dst_height - $char_height) / 2;</p>
<p>// Copy each character one by one<br>
for ($i = 0; $i < $len; $i++) {<br>
$char = $text[$i];<br>
// ASCII offset, assuming source text is arranged continuously starting from 'A'<br>
$offset = ord($char) - ord('A');<br>
$src_x = $offset * $char_width;<br>
$src_y = 0;</p>
imagecopy(
$dst_im,
$src_im,
$start_x + $i * $char_width,
$start_y,
$src_x,
$src_y,
$char_width,
$char_height
);
}
// Output the image
header('Content-Type: image/png');
imagepng($dst_im);
// Free resources
imagedestroy($dst_im);
imagedestroy($src_im);
?>
This example assumes the text source image (letters.png) contains letters arranged sequentially in a horizontal row, with width equal to single character width multiplied by the number of characters, and height equal to the font height.
imagefontwidth() and imagefontheight() help us accurately get character width and height, facilitating character-by-character copying.
If the text content is complex, you can combine imagettftext() to render fonts first before processing, or use imagecopyresampled() for scaling effects.
This method can be used to create interesting text animation effects, fragmented letter style mappings, or even custom watermarks.