[42] | 1 | <?php // content="text/plain; charset=utf-8" |
---|
| 2 | require_once "jpgraph/jpgraph.php"; |
---|
| 3 | require_once "jpgraph/jpgraph_canvas.php"; |
---|
| 4 | |
---|
| 5 | // We accept a URI argument to adjust the angle at what we display the text |
---|
| 6 | if( empty($_GET['a']) ) { |
---|
| 7 | $angle=40; |
---|
| 8 | } |
---|
| 9 | else { |
---|
| 10 | $angle=$_GET['a']; |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | // Caption below the image |
---|
| 14 | $caption = "Demonstration of different anchor points for texts as specified with\n". |
---|
| 15 | "TextAlign(). The red cross marks the coordinate that was given to\n". |
---|
| 16 | "stroke each instance of the string.\n(The green box is the bounding rectangle for the text.)"; |
---|
| 17 | |
---|
| 18 | $txt="TextAlign()"; |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | // Initial width and height since we need a "dummy" canvas to |
---|
| 22 | // calculate the height of the text strings |
---|
| 23 | $w=480;$h=50; |
---|
| 24 | $xm=90;$ym=80; |
---|
| 25 | |
---|
| 26 | $g = new CanvasGraph($w,$h); |
---|
| 27 | |
---|
| 28 | // Make the image easier to access |
---|
| 29 | $img = $g->img; |
---|
| 30 | |
---|
| 31 | // Get the bounding box for text |
---|
| 32 | $img->SetFont(FF_ARIAL,FS_NORMAL,16); |
---|
| 33 | $tw=$img->GetBBoxWidth($txt,$angle); |
---|
| 34 | $th=$img->GetBBoxHeight($txt,$angle); |
---|
| 35 | |
---|
| 36 | $img->SetFont(FF_ARIAL,FS_NORMAL,11); |
---|
| 37 | $ch=$img->GetBBoxHeight($caption); |
---|
| 38 | |
---|
| 39 | // Calculate needed height for the image |
---|
| 40 | $h = 3*$th+2*$ym + $ch; |
---|
| 41 | $g = new CanvasGraph($w,$h); |
---|
| 42 | $img = $g->img; |
---|
| 43 | |
---|
| 44 | // Alignment for anchor points to use |
---|
| 45 | $anchors = array('left','top', |
---|
| 46 | 'center','top', |
---|
| 47 | 'right','top', |
---|
| 48 | 'left','center', |
---|
| 49 | 'center','center', |
---|
| 50 | 'right','center', |
---|
| 51 | 'left','bottom', |
---|
| 52 | 'center','bottom', |
---|
| 53 | 'right','bottom'); |
---|
| 54 | |
---|
| 55 | $n = count($anchors)/2; |
---|
| 56 | |
---|
| 57 | for( $i=0,$r=0,$c=0; $i < $n; ++$i ) { |
---|
| 58 | |
---|
| 59 | $x = $c*($tw+$xm)+$xm/2; |
---|
| 60 | $y = $r*($th+$ym)+$ym/2-10; |
---|
| 61 | |
---|
| 62 | $img->SetColor('blue'); |
---|
| 63 | $img->SetTextAlign($anchors[$i*2],$anchors[$i*2+1]); |
---|
| 64 | $img->SetFont(FF_ARIAL,FS_NORMAL,16); |
---|
| 65 | $img->StrokeText($x,$y,$txt,$angle,"left",true); |
---|
| 66 | |
---|
| 67 | $img->SetColor('black'); |
---|
| 68 | $img->SetFont(FF_FONT1,FS_BOLD); |
---|
| 69 | $img->SetTextAlign('center','top'); |
---|
| 70 | $align = sprintf('("%s","%s")',$anchors[$i*2],$anchors[$i*2+1]); |
---|
| 71 | $img->StrokeText($c*($tw/2+$xm)+$xm/2+$tw/2,$r*($th/2+$ym)+$th+$ym/2-4,$align); |
---|
| 72 | |
---|
| 73 | $c++; |
---|
| 74 | if( $c==3 ) { |
---|
| 75 | $c=0;$r++; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // Draw the caption text |
---|
| 80 | $img->SetTextAlign('center','bottom'); |
---|
| 81 | $img->SetFont(FF_ARIAL,FS_ITALIC,11); |
---|
| 82 | $img->StrokeText($w/2,$h-10,$caption,0,'left'); |
---|
| 83 | |
---|
| 84 | $img->SetColor('navy'); |
---|
| 85 | $img->Rectangle(0,0,$w-1,$h-1); |
---|
| 86 | |
---|
| 87 | // .. and send back to browser |
---|
| 88 | $g->Stroke(); |
---|
| 89 | |
---|
| 90 | ?> |
---|
| 91 | |
---|