I know that PHP's GD library is used for manipulating images, and it has methods available to create images which contain text, e.g. imagettftext()
. This is used for things like CAPTCHAs.
However, I've come across the example below, where an image is created, presumably using GD, using just dots, however arranging them such that there's a higher density in the shape of the text to display in the image.
I'd like to learn how to utilise such a technique, however can't piece together how to do so. I'd imagine that the background is all just random dots, however, I just can't seem to grasp how it's being done to group the dots in the shape of a character in a specified font.
header('Content-Type: image/png');
putenv('GDFONTPATH=' . realpath('.'));
$im = imagecreatetruecolor(400, 200);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefttext($im, 120.0, -20.0, 200, 120, $white, 'arialblack', '4');
for ($i = 0; $i < 1e5; $i++) {
imagesetpixel($im, rand(0, 400), rand(0, 200), $black);
}
for ($i = 0; $i < 2e3; $i++) {
imagesetpixel($im, rand(0, 400), rand(0, 200), $white);
}
imagepng($im);
imagedestroy($im);
And the result looks like this
You can make the effect harder by using more black dots or using lighter fonts, maybe an outlined one.
You can improve the performance by using image noisemaps and blend them together.