Skip to content

Instantly share code, notes, and snippets.

@francescom
Created June 26, 2020 09:19
Show Gist options
  • Save francescom/6640041480473d4606cb6b5dbe575f87 to your computer and use it in GitHub Desktop.
Save francescom/6640041480473d4606cb6b5dbe575f87 to your computer and use it in GitHub Desktop.
<?php
$def_font='HelveticaBold.ttf';
$err_font='HelveticaBold.ttf';
$font_folder='fonts/';
if( isset($_GET['w']) ) $w=my_txt_img_stsl($_GET['w']);
else $w=0;
if( isset($_GET['s']) ) $size=my_txt_img_stsl($_GET['s']);
else $size=127;
if( isset($_GET['f']) ) {
$FontName=str_replace('/','',my_txt_img_stsl($_GET['f']));
$font=$font_folder.$FontName;
} else $font=$font_folder.$def_font;
if( isset($_GET['h']) ) $h=my_txt_img_stsl($_GET['h']);
else $h=0;
if( isset($_GET['c']) ) $c=my_txt_img_stsl($_GET['c']);
else $c='0,0,0';
$c=explode(',',trim($c));
foreach($c as $idx=>$cItm) {
$c[$idx]=intval(trim($cItm));
}
if( isset($_GET['bc']) ) $bc=my_txt_img_stsl($_GET['bc']);
else $bc='255,255,255';
$bc=explode(',',trim($bc));
foreach($bc as $idx=>$bcItm) {
$bc[$idx]=intval(trim($bcItm));
}
if( isset($_GET['t']) ) $t=my_txt_img_stsl($_GET['t']);
else {
$t='Missing parameter: t=text+to+write';
$c=array(255,0,0);
$font=$font_folder.$err_font;
$size=24;
}
if(!file_exists($font)) {
$t='Font "'.$FontName.'" not found in font folder (upload file or change font name)';
$c=array(255,0,0);
$font=$font_folder.$err_font;
$size=24;
}
$TextBox=imagettfbbox($size,0.0, $font,$t);
$off_x=-$TextBox[6];
$off_y=-$TextBox[7];
$TextHSize=$TextBox[2]-$TextBox[6];
$TextVSize=$TextBox[3]-$TextBox[7];
//$TextHSize=ImageFontWidth ($font ) * strlen ($t);
//$TextVSize=ImageFontHeight ($font );
if( isset($_GET['b']) ) {
$b=$_GET['b'];
$hb=$b;
$vb=$b;
} else {
$b=5;
$hb=$b;
$vb=$b;
if($w!=0) {
$hb=($w-$TextHSize)/2;
}
if($h!=0) {
$vb=($h-$TextVSize)/2;
}
}
if($w==0) $w=$TextHSize+2*$hb;
if($h==0) $h=$TextVSize+2*$vb;
//echo($TextHSize.','.$TextVSize.'<br>');
//echo($w.','.$h.'<br>');
$im = imageCreate ($w ,$h )
or die( "Cannot Initialize new GD image stream" );
$text_color =imagecolorallocate ($im ,$c[0] ,$c[1] ,$c[2] );
$back_color =imagecolorallocate ($im ,$bc[0] ,$bc[1] ,$bc[2] );
$BlackCol =imagecolorallocate ($im ,0 ,0 ,0 );
//$BlackCol =imageColorClosest ($im 0 ,0 ,0 );
if($BlackCol<0) {
$BlackCol=0;
$t='Error in color allocation';
}
imagefilledrectangle ( $im, 0, 0, $w, $h, $back_color);
imagettftext ( $im, $size, 0, $off_x+$hb,$off_y+$vb, $text_color, $font, $t );
//readfile('fonts/Chalkboard.ttf');
//echo(`locate .TTF`);
//echo('1');
//imagestring ($im,5,$hb,$vb,$t,$BlackCol );
if ( function_exists ("imagegif" )) {
header ("Content-type: image/gif" );
//imagettftext ( $im, 12, 30, 25,25, $BlackCol, $font_folder.$def_font, 'GIF' );
imagegif ($im );
} elseif ( function_exists ("imagejpeg" )) {
header ("Content-type: image/jpeg" );
//imagettftext ( $im, 12, 50, 25,25, $BlackCol, $font_folder.$def_font, 'JPG' );
imagejpeg ($im,'',80);
} else {
header ("Content-type: image/png" );
//imagettftext ( $im, 12, 50, 25,25, $BlackCol, $font_folder.$def_font, 'PNG' );
imagepng ($im );
}
imagedestroy ($im );
function my_txt_img_stsl($txt) {
return stripslashes($txt);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment