Skip to content

Instantly share code, notes, and snippets.

@cp6
Last active July 19, 2019 03:52
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 cp6/a4ee3f12029850614530ba4c638e4c04 to your computer and use it in GitHub Desktop.
Save cp6/a4ee3f12029850614530ba4c638e4c04 to your computer and use it in GitHub Desktop.
Create png letter icon with PHP
<?php
function create_icon($first_letter, $second_letter, $save_as)
{
$image = ImageCreate(64, 64) or die ("Error making image");//Make the base image (64 x 64)
$background_color = ImageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));//Base image background color (random)
$text_color = ImageColorAllocate($image, 255, 255, 255);//White text for font
$font = '/var/www/html/ariel.ttf';//Defined font file
if ($second_letter == ''){//Positioning for one vs two letters
$string = "$first_letter";
$img = imagettftext($image, 36, 0, 17, 47, $text_color, $font, $string);
} else {
$string = "$first_letter".$second_letter."";
$img = imagettftext($image, 36, 0, 4, 47, $text_color, $font, $string);
}
$img = Imagepng($image, $save_as);//Finish the image generation
imagedestroy($image);//Clear memory?
return true;
}
create_icon('L', 'S', 'icon.png');//Create an icon
echo "<img src='icon.png'>";//Show it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment