Skip to content

Instantly share code, notes, and snippets.

@xyulex
Last active July 21, 2018 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xyulex/aab2e131815c3f260b12 to your computer and use it in GitHub Desktop.
Save xyulex/aab2e131815c3f260b12 to your computer and use it in GitHub Desktop.
PHP: Add text to transparent PNG image
<?php
$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);
}
?>
@sridharkalaibala
Copy link

$i; not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment