author: Krasimir Tsonev

Hi there, I'm . Senior front-end engineer with over 13 years of experience. I write, speak and occasionally code stuff.

PHP: add text to transparent PNG image

Simple way to produce numbered image with PHP. The script uses PHP GD.
$originalImage = "group_icon.png";
  if (file_exists($originalImage)) {
    $im = imagecreatefrompng($originalImage);
    imagesavealpha($im, true);
    // important to keep the png's transparency 	
    if (!$im) {
      die("im is null");
    }
    $black = imagecolorallocate($im, 0, 0, 0);
    $width = 36;
    // the width of the image	
    $height = 36;
    // the height of the image	
    $font = 2;
    // font size
    $digit = $i;
    // digit
    $leftTextPos = 19 - (strlen($digit) * 3);
    $outputImage = "group_icon_".$digit.
    ".png";
    imagestring($im, $font, $leftTextPos, 9, $digit, $black);
    imagepng($im, $outputImage, 0);
    imagedestroy($im);
  }
If you enjoy this post, share it on Twitter, Facebook or LinkedIn. Or maybe comment below: