PHP Code:
<?php
$font = array(
'Courier New Bold' => 'Courier New Bold.ttf',
'Times New Roman' => 'times.ttf',
'Verdana' => 'verdana.ttf'
);
$wid=400;
$hite=400;
$fontSize=12;
$angle=90;
foreach (array('a', 'angle', 'theta') as $key) {
if (isset($_REQUEST[$key])) {
$angle = $_REQUEST[$key];
break;
}
}
$text = "time (seconds)";
foreach (array('t', 'text', 'm', 'msg') as $key) {
if (isset($_REQUEST[$key])) {
$text = $_REQUEST[$key];
break;
}
}
// At various points, the width of $text in pixels is approximated as strlen($text) * $fontSize
$titleX=2*$fontSize;
$titleY=($hite/2) + (strlen($text) * $fontSize / 2);
// $img is the image to output. $title will be rotated & laid over $img
$img = ImageCreateTrueColor($wid, $hite);
$title = ImageCreateTrueColor($hite, $wid);
// in creating colors, set the BG of $title to transparent
$white = ImageColorAllocate($img, 0xFF, 0xFF, 0xFF);
$fg = ImageColorAllocate($title, 0x0, 0xCF, 0xCF);
$trans = ImageColorAllocateAlpha($title, 0, 0, 0, 0);
ImageColorTransparent($title, $trans);
ImageFill($title, 0,0, $trans);
// write some explanatory text and the rotated title (for comparison) to img
ImageTTFText($img, $fontSize, 0, $fontSize+2, $fontSize+2, $white, $font['Courier New Bold'], "angle: $angle; (" . ($wid-$titleX) . ", $titleY)");
ImageTTFText($img, $fontSize, $angle, $wid - $titleX - strlen($text)*$fontSize*cos(deg2rad($angle)), $titleY, $white, $font['Courier New Bold'], $text);
// write non-rotated title to $title, then rotate the whole image & overlay $title onto $img
ImageTTFText($title, $fontSize, 0, $hite - $titleY, $titleX, $fg, $font['Courier New Bold'], $text);
$ttl = ImageRotate($title, $angle, $trans, True);
ImageDestroy($title);
$title = $ttl;
ImageCopyMerge($img, $title, 0,0, 0,0,$wid,$hite, 100);
ImageDestroy($title);
header('Content-type: image/png');
ImagePNG($img);
ImageDestroy($img);
?>
Extending PHPlot and overriding