-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hiero.php
201 lines (172 loc) · 5.51 KB
/
Hiero.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace ArabyPHP\Hiero;
class Hiero
{
private $_language = 'Hiero';
/**
* Set the output language.
*
* @param string $value Output language (Hiero or Phoenician)
*
* @return object $this to build a fluent interface
*
*/
public function setLanguage($value)
{
$value = strtolower($value);
if ($value == 'hiero' || $value == 'phoenician') {
$this->_language = $value;
}
return $this;
}
/**
* Get the output language.
*
* @return string return current setting of the output language
*
*/
public function getLanguage()
{
return ucwords($this->_language);
}
/**
* Translate Arabic or English word into Hieroglyphics.
*
* @param string $word Arabic or English word
* @param string $dir Writing direction [ltr, rtl, ttd, dtt] (default ltr)
* @param string $lang Input language [en, ar] (default en)
* @param int $red Value of background red component (default is null)
* @param int $green Value of background green component (default is null)
* @param int $blue Value of background blue component (default is null)
*
* @return resource Image resource identifier
*
*/
public function str2graph($word, $dir = 'ltr', $lang = 'en', $red = null, $green = null, $blue = null)
{
if ($this->_language == 'phoenician') {
define(MAXH, 40);
define(MAXW, 50);
} else {
define(MAXH, 100);
define(MAXW, 75);
}
// Note: there is no theh, khah, thal, dad, zah, and ghain in Phoenician
$arabic = [
'ا' => 'alef',
'ب' => 'beh',
'ت' => 'teh',
'ث' => 'theh',
'ج' => 'jeem',
'ح' => 'hah',
'خ' => 'khah',
'د' => 'dal',
'ذ' => 'thal',
'ر' => 'reh',
'ز' => 'zain',
'س' => 'seen',
'ش' => 'sheen',
'ص' => 'sad',
'ض' => 'dad',
'ط' => 'tah',
'ظ' => 'zah',
'ع' => 'ain',
'غ' => 'ghain',
'ف' => 'feh',
'ق' => 'qaf',
'ك' => 'kaf',
'ل' => 'lam',
'م' => 'meem',
'ن' => 'noon',
'ه' => 'heh',
'و' => 'waw',
'ي' => 'yeh',
];
if ($lang != 'ar' && $this->_language == 'phoenician') {
$temp = new Transliteration();
$word = $temp->en2ar($word);
$temp = null;
$lang = 'ar';
}
if ($lang != 'ar') {
$word = strtolower($word);
} else {
$word = str_replace('ة', 'ت', $word);
$alef = ['ى', 'ؤ', 'ئ', 'ء', 'آ', 'إ', 'أ'];
$word = str_replace($alef, '?', $word);
}
$chars = [];
$max = mb_strlen($word, 'UTF-8');
for ($i = 0; $i < $max; $i++) {
$chars[] = mb_substr($word, $i, 1, 'UTF-8');
}
if ($dir == 'rtl' || $dir == 'btt') {
$chars = array_reverse($chars);
}
$max_w = 0;
$max_h = 0;
foreach ($chars as $char) {
if ($lang == 'ar') {
$char = $arabic[$char];
}
if (file_exists(__DIR__."/data/{$this->_language}/$char.gif")) {
list($width, $height) = getimagesize(__DIR__."/data/{$this->_language}/$char.gif");
} else {
$width = MAXW;
$height = MAXH;
}
if ($dir == 'ltr' || $dir == 'rtl') {
$max_w += $width;
if ($height > $max_h) {
$max_h = $height;
}
} else {
$max_h += $height;
if ($width > $max_w) {
$max_w = $width;
}
}
}
$im = imagecreatetruecolor($max_w, $max_h);
if ($red == null) {
$bck = imagecolorallocate($im, 0, 0, 255);
imagefill($im, 0, 0, $bck);
// Make the background transparent
imagecolortransparent($im, $bck);
} else {
$bck = imagecolorallocate($im, $red, $green, $blue);
imagefill($im, 0, 0, $bck);
}
$current_x = 0;
$current_y = 0;
foreach ($chars as $char) {
if ($lang == 'ar') {
$char = $arabic[$char];
}
$filename = __DIR__."/data/{$this->_language}/$char.gif";
if ($dir == 'ltr' || $dir == 'rtl') {
if (file_exists($filename)) {
list($width, $height) = getimagesize($filename);
$image = imagecreatefromgif($filename);
imagecopy(
$im, $image, $current_x, $max_h - $height,
0, 0, $width, $height
);
} else {
$width = MAXW;
}
$current_x += $width;
} else {
if (file_exists($filename)) {
list($width, $height) = getimagesize($filename);
$image = imagecreatefromgif($filename);
imagecopy($im, $image, 0, $current_y, 0, 0, $width, $height);
} else {
$height = MAXH;
}
$current_y += $height;
}
}
return $im;
}
}