Skip to content

Instantly share code, notes, and snippets.

@lacymorrow
Created May 24, 2023 03:34
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 lacymorrow/f9e9669d46d6bfad8b91c9af1ef9c351 to your computer and use it in GitHub Desktop.
Save lacymorrow/f9e9669d46d6bfad8b91c9af1ef9c351 to your computer and use it in GitHub Desktop.
Uses an uploaded font to change text into an image.
<?php
// Copyright (c) 2005, Lacy Morrow
// All rights reserved.
// ******************Text-Image PHP Script***********************
// Uses an uploaded font to change text into an image. Multiple options.
// USE:
// ONLY SUPPORTS .ttf fonts
// Use the format "http://www.yourdomain.com/text.php?s=11&text=hello&font=TIMES"
// S:Size
// Text:Output Text
// Font:Output Font
// Fonts must be located in "/fonts/" folder.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
Header("Content-type: image/png");
if(!isset($s)) $s=11;
if(!isset($text)) $text="undefined";
if(!isset($font)) $font="TIMES";
$size = imagettfbbox($s,0,"/fonts/".$font.".ttf",$text);
$image = imageCreateTrueColor(abs($size[2]) + abs($size[0]), abs($size[7]) + abs($size[1]));
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);
$transparentColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparentColor);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$textColor = $black;
imagettftext($image, $s, 0, 0, abs($size[5]), $textColor, "/fonts/".$font.".ttf", $text);
imagepng($image);
imagedestroy($image);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment