Search code examples
phpgdeffectsdynamic-image-generation

Generating an image with a "Gold-plated" text effect using PHP/GD


I'm dynamically generating a PNG image using PHP 7.3/GD based on a text provided by the user.

Everything works as expected, but I'd like to apply some kind of filter/effect to obtain a Gold-plated style, such as below:

Gold-plated effect

Any idea how to achieve this? I've found solutions to apply blur/glow/shadow or to solve this via HTML5/CSS3 but I must use GD/PHP for this project.

Here's my current code:

<?php

putenv('GDFONTPATH='.realpath('.'));
header('Content-Type: image/png');
$im = imagecreatetruecolor(300, 200);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
$gold = imagecolorallocate($im, 255, 215, 0);
imagettftext($im, 28, 0, 76, 110, $gold, 'HirukoBlackAlternate.ttf', 'Stack');
imagepng($im);
imagedestroy($im);

Solution

  • Well, I played around with this a bit, and got this:

    enter image description here

    It's not exactly like the example image, but it is getting somewhat close. You'll have to fiddle it a bit more to get exactly what you want.

    I did use imagelayereffect() like this:

    // start with your code
    putenv('GDFONTPATH='.realpath('.'));
    header('Content-Type: image/png');
    $im = imagecreatetruecolor(300, 200);
    $bg = imagecolorallocate($im, 255, 255, 255);
    imagefill($im, 0, 0, $bg);
    
    // first the back drop 
    $gray = imagecolorallocate($im, 80, 80, 80);
    imagettftext($im, 28, 0, 76+3, 110+2, $gray, 'HirukoBlackAlternate.ttf', 'Stack');
    
    // then the gold
    $gold = imagecolorallocate($im, 180, 180, 150);
    imagettftext($im, 28, 0, 76, 110, $gold, 'HirukoBlackAlternate.ttf', 'Stack');
    
    // get a pattern image
    $pattern = imagecreatefromjpeg('http://i.pinimg.com/736x/96/36/3c/96363c9337b2d1aad24323b1d9efda72--texture-metal-gold-texture.jpg');
    
    // copy it in with a layer effect
    imagelayereffect($im, IMG_EFFECT_OVERLAY);
    imagecopyresampled($im, $pattern, 0, 0, 0, 0, 300, 200, 736, 552);
    
    // output and forget
    imagepng($im);
    imagedestroy($im);
    imagedestroy($pattern);
    

    So I basically used an image to get the golden shine. Seems to work, but I think this can be improved upon.