LonghornPHP 2026

Voting

: max(five, nine)?
(Example: nine)

The Note You're Voting On

SW
20 years ago
This works! A Black-Image with vertical centered white Aliased Arial-Text and same left and right margin - used for Menu-Buttons.

<?php

  function createImgText ($string="", $fontsize=0, $marginX=0, $imgH=0 , $fontfile="", $imgColorHex="", $txtColorHex=""){
    if($string!=""){
        Header("Content-type: image/png");     
        //
        $spacing = 0;
          $line = array("linespacing" => $spacing);
        $box = @imageftbbox($fontsize,0,$fontfile,$string,$line)
        or die("ERROR");
        $tw=$box[4]-$box[0]; //image width
        $marginY = $imgH - (($imgH - $fontsize) / 2);
        $imgWidth = $tw + (2*$marginX);
        $im = ImageCreate($imgWidth, $imgH); 
        $int = hexdec($imgColorHex);
        $arr = array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
        $black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]); 
        $int = hexdec($txtColorHex);
        $arr = array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
        $white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]); 
        ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array()); 
        ImagePng($im); 
        ImageDestroy($im);
    }else{
        echo "ERROR!";
    }
}
createImgText ("Hello World", 9, 10, 18, "arial.ttf", "000000", "FFFFFF");
?>

<< Back to user notes page

To Top