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