textalignex1.html
21.9 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<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"><?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content="text/plain; charset=utf-8"</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">"</span><span class="hl-string">jpgraph/jpgraph.php</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">jpgraph/jpgraph_canvas.php</span><span class="hl-quotes">"</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">"</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">"</span><span class="hl-code">.
</span><span class="hl-quotes">"</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">"</span><span class="hl-code">.
</span><span class="hl-quotes">"</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">"</span><span class="hl-code">;
</span><span class="hl-var">$txt</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">TextAlign()</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Initial width and height since we need a "dummy" 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">-></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">-></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">-></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">-></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">-></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">-></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">-></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"> < </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">-></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">-></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">-></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">-></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">"</span><span class="hl-string">left</span><span class="hl-quotes">"</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">-></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">-></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">-></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">("%s","%s")</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">-></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">-></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">-></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">-></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">-></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">-></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">-></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">?></span></pre></td></tr></table></div>