So, what i’m trying to do is use a gradient to write out text using GD.
The general theory is… put the gradient under the image (scaled to fit the text), put a ‘mask layer’ on top, write the text onto the mask, and make it transparent. Trick is that the transparency doesnt quite work (because the text isnt rendered as single-color pixels but is smoothed), resulting in black border around parts of the gradient text (and in small enough font sizes, removing the gradient entirely). Is there a better way to do this? Or a way to better carve the ‘mask layer’?
<?php
//Size of the text.
$tfbox = imagettfbbox(32,0,"./tahoma.ttf",$_GET['string']);
$newwidth = abs($tfbox[2] - $tfbox[0]);
$newheight = abs($tfbox[7] - $tfbox[1]);
//Canvases
$im = imagecreatefrompng('images/scale.png');
$temp = imagecreatetruecolor($newwidth,$newheight);
$out = imagecreatetruecolor($newwidth,$newheight);
//Color definitions
$grey = imagecolorallocate($out,177,179,180);
$black = imagecolorallocate($out,0,0,0);
//Carve text out of solid grey.
imagefill($out,0,0,$grey);
imagettftext($out,32,0,0,$newheight,$black,"./tahoma.ttf",$_GET['string']);
imagecolortransparent($out,$black);
list($width,$height) = getimagesize('images/scale.jpg');
//Combine Image Assets
imagecopyresized($temp,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopymerge($temp,$out,0,0,0,0,$newwidth,$newheight,100);
//Output result
header('Content-Type: image/png');
//imagepng($out);
imagepng($temp);
//Cleanup
imagedestroy($temp);
imagedestroy($out);
imagedestroy($im);
?>
(PS: Yes, I know $_GET[‘string’] isnt checked/sanitized/urldecoded)