Gregwar / Image

A PHP library to handle images
MIT License
1k stars 191 forks source link

Write (semi)-transparent/clouded text #147

Open Geolim4 opened 6 years ago

Geolim4 commented 6 years ago

Just for the sharing:

If you want to write semi-transparent text, instead of passing hexa color to write(), you can pass an imagecolorallocatealpha ressource:

Classic text:

$image->write(
    'myfont.ttf',
    'mytext',
    10,
    290,
    '10',
    '0',
    '666666',
    'left'
);

Transparent text (with custom opacity from 0 to 127):

$opacity = 100; // from 0 to 127
$image->write(
    'myfont.ttf',
    'mytext',
    10,
    290,
    '10',
    '0',
        imagecolorallocatealpha(imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT), 0, 0, 0, $opacity),
    'left'
);

See imagecolorallocatealpha()

Hope this will help some people.