message_image_generator.php:最適化
@@ -40,17 +40,20 @@ | ||
40 | 40 | 'sex_male' => array('R' => 0, 'G' => 0, 'B' => 255), |
41 | 41 | 'wisp' => array('R' => 170, 'G' => 102, 'B' => 255), |
42 | 42 | 'step' => array('R' => 102, 'G' => 153, 'B' => 51) |
43 | - ); | |
43 | + ); | |
44 | 44 | |
45 | 45 | public function __construct($list, $font) { |
46 | - $font = IMAGE_FONT_PATH . $font; | |
47 | - $size = ($trans = $list == 'WishRoleList') ? 12 : 11; | |
46 | + $font = IMAGE_FONT_PATH . $font; | |
47 | + $trans = $list == 'WishRoleList'; | |
48 | + $size = $trans ? 12 : 11; | |
48 | 49 | $this->generator = new MessageImageGenerator($font, $size, 3, 3, $trans); |
49 | - $this->list = new $list(); | |
50 | + $this->list = new $list(); | |
50 | 51 | } |
51 | 52 | |
52 | 53 | public function LoadDelimiter($delimiter, $colors) { |
53 | - if (! is_array($colors)) $colors = $this->color_list[$colors]; | |
54 | + if (! is_array($colors)) { | |
55 | + $colors = $this->color_list[$colors]; | |
56 | + } | |
54 | 57 | return new Delimiter($delimiter, $colors['R'], $colors['G'], $colors['B']); |
55 | 58 | } |
56 | 59 |
@@ -61,8 +64,12 @@ | ||
61 | 64 | } |
62 | 65 | |
63 | 66 | public function SetDelimiter(array $list) { |
64 | - if (isset($list['type'])) $this->SetDelimiter($this->list->{$list['type']}); | |
65 | - if (! isset($list['delimiter'])) $list['delimiter'] = array(); | |
67 | + if (isset($list['type'])) { | |
68 | + $this->SetDelimiter($this->list->{$list['type']}); | |
69 | + } | |
70 | + if (! isset($list['delimiter'])) { | |
71 | + $list['delimiter'] = array(); | |
72 | + } | |
66 | 73 | $this->AddDelimiter($list); |
67 | 74 | } |
68 | 75 |
@@ -8,10 +8,10 @@ | ||
8 | 8 | */ |
9 | 9 | |
10 | 10 | class Delimiter { |
11 | - public $c; // デリミタ文字 | |
12 | - public $r; // RGB色指定の赤成分値 | |
13 | - public $g; // RGB色指定の緑成分値 | |
14 | - public $b; // RGB色指定の青成分値 | |
11 | + public $c; //デリミタ文字 | |
12 | + public $r; //RGB色指定の赤成分値 | |
13 | + public $g; //RGB色指定の緑成分値 | |
14 | + public $b; //RGB色指定の青成分値 | |
15 | 15 | |
16 | 16 | /* |
17 | 17 | コンストラクタ |
@@ -29,16 +29,17 @@ | ||
29 | 29 | } |
30 | 30 | |
31 | 31 | class MessageImageGenerator { |
32 | - public $font; // フォントパス | |
33 | - public $size; // フォントサイズ | |
34 | - public $width; // 半角1文字あたりの幅 | |
35 | - public $height; // 半角1文字あたりの高さ | |
36 | - public $x_margin; // マージン幅 | |
37 | - public $y_margin; // マージン高さ | |
38 | - public $def_col; // デフォルト文字色のRGB値 | |
39 | - public $def_bgc; // デフォルト背景色のRGB値 | |
40 | - public $is_trans; // 背景色を透明にするかどうか | |
41 | - public $delimiters; // デリミタ情報、色を格納する配列 | |
32 | + public $font; //フォントパス | |
33 | + public $size; //フォントサイズ | |
34 | + public $width; //半角1文字あたりの幅 | |
35 | + public $height; //半角1文字あたりの高さ | |
36 | + public $x_margin; //マージン幅 | |
37 | + public $y_margin; //マージン高さ | |
38 | + public $def_col; //デフォルト文字色のRGB値 | |
39 | + public $def_bgc; //デフォルト背景色のRGB値 | |
40 | + public $is_trans; //背景色を透明にするかどうか | |
41 | + public $delimiters; //デリミタ情報、色を格納する配列 | |
42 | + | |
42 | 43 | /* |
43 | 44 | コンストラクタ |
44 | 45 | $font 使用するTrueTypeフォントのパス |
@@ -45,17 +46,18 @@ | ||
45 | 46 | $size フォントサイズ |
46 | 47 | $x_margin マージン幅 |
47 | 48 | $y_margin マージン高さ |
49 | + $is_trans 透過処理 | |
48 | 50 | */ |
49 | 51 | public function __construct($font = "C:\\WINDOWS\\Fonts\\msgothic.ttc", $size = 12, |
50 | - $x_margin = 5, $y_margin = 2, $is_trans = false) { | |
51 | - $this->font = $font; | |
52 | - $this->size = $size; | |
53 | - $this->x_margin = $x_margin; | |
54 | - $this->y_margin = $y_margin; | |
55 | - $this->def_col = array(0, 0, 0); | |
56 | - $this->def_bgc = array(255, 255, 255); | |
57 | - $this->is_trans = $is_trans; | |
58 | - $this->delimiters = array(); | |
52 | + $x_margin = 5, $y_margin = 2, $is_trans = false) { | |
53 | + $this->font = $font; | |
54 | + $this->size = $size; | |
55 | + $this->x_margin = $x_margin; | |
56 | + $this->y_margin = $y_margin; | |
57 | + $this->def_col = array(0, 0, 0); | |
58 | + $this->def_bgc = array(255, 255, 255); | |
59 | + $this->is_trans = $is_trans; | |
60 | + $this->delimiters = array(); | |
59 | 61 | |
60 | 62 | //フォント幅・高さの測定。もっといい定跡があればそちらに変更する予定。 |
61 | 63 | $r_a = imagettfbbox($this->size, 0, $this->font, "A"); |
@@ -64,6 +66,7 @@ | ||
64 | 66 | $this->width = $r_a2[2] - $r_a[2]; |
65 | 67 | $this->height = $r_a2v[1] - $r_a[1]; |
66 | 68 | } |
69 | + | |
67 | 70 | /* |
68 | 71 | 新規デリミタを追加する関数 |
69 | 72 | $nd 新しいデリミタと使用色を定義したDelimiterクラス |
@@ -75,14 +78,16 @@ | ||
75 | 78 | return; |
76 | 79 | } |
77 | 80 | } |
78 | - $this->delimiters[] = $nd; // 新しいデリミタを追加する | |
81 | + $this->delimiters[] = $nd; //新しいデリミタを追加する | |
79 | 82 | } |
83 | + | |
80 | 84 | /* |
81 | 85 | 登録されているデリミタを削除する関数 |
82 | 86 | $c デリミタ文字 |
83 | 87 | */ |
84 | 88 | public function DeleteDelimiter($c) { |
85 | - for ($i = 0; $i < count($this->delimiters); $i++) { | |
89 | + $count = count($this->delimiters); | |
90 | + for ($i = 0; $i < $count; $i++) { | |
86 | 91 | if ($this->delimiters[$i]->c == $c) { |
87 | 92 | array_splice($this->delimiters, $i, 1); //登録されているデリミタを削除する |
88 | 93 | return; |
@@ -89,6 +94,7 @@ | ||
89 | 94 | } |
90 | 95 | } |
91 | 96 | } |
97 | + | |
92 | 98 | /* |
93 | 99 | 登録されているデリミタクラスを取得する関数 |
94 | 100 | $c デリミタ文字 |
@@ -95,7 +101,8 @@ | ||
95 | 101 | 返り値 対応するデリミタクラス。登録されていない場合はデフォルトのデリミタクラス |
96 | 102 | */ |
97 | 103 | public function GetDelimiter($c) { |
98 | - for ($i = 0; $i < count($this->delimiters); $i++) { | |
104 | + $count = count($this->delimiters); | |
105 | + for ($i = 0; $i < $count; $i++) { | |
99 | 106 | if ($this->delimiters[$i]->c == $c) { |
100 | 107 | return $this->delimiters[$i]; |
101 | 108 | } |
@@ -102,6 +109,7 @@ | ||
102 | 109 | } |
103 | 110 | return new Delimiter('', 0, 0, 0); |
104 | 111 | } |
112 | + | |
105 | 113 | /* |
106 | 114 | 登録されているデリミタから分割用の正規表現文字列を作成する関数 |
107 | 115 | 返り値 正規表現文字列 |
@@ -108,6 +116,7 @@ | ||
108 | 116 | */ |
109 | 117 | public function GenerateDelimiterRegEx() { |
110 | 118 | if (count($this->delimiters) == 0) return ''; |
119 | + | |
111 | 120 | $regex_str = '/['; |
112 | 121 | foreach ($this->delimiters as $d) { |
113 | 122 | //正規表現で特別な意味を持つ文字をデリミタとして使う場合は、ここで\を挿入する必要あり |
@@ -127,6 +136,7 @@ | ||
127 | 136 | } |
128 | 137 | return $regex_str . ']/'; |
129 | 138 | } |
139 | + | |
130 | 140 | /* |
131 | 141 | 文字データの整形処理 |
132 | 142 | 必要なら文字コード変換や正規表現の処理を実行する |
@@ -135,6 +145,7 @@ | ||
135 | 145 | $message = $regex == '' ? $str : preg_replace($regex, '', $str); |
136 | 146 | return mb_convert_encoding($message, 'UTF-8', 'auto'); |
137 | 147 | } |
148 | + | |
138 | 149 | /* |
139 | 150 | 役職説明、能力実行結果などのメッセージ用画像ファイルを生成する関数 |
140 | 151 | $msg 作成したいメッセージ文。改行有効。||で囲んだ部分を指定した色で書く |
@@ -152,33 +163,37 @@ | ||
152 | 163 | $plain_r = imagettfbbox($this->size, 0, $this->font, $plain_msg); |
153 | 164 | //echo print_r($plain_r, true) . '<br>'; |
154 | 165 | |
155 | - // 画像の生成 | |
166 | + //画像の生成 | |
156 | 167 | $img = imagecreatetruecolor($plain_r[2] - $plain_r[6] + $this->x_margin * 2, |
157 | 168 | $plain_r[3] - $plain_r[7] + $this->y_margin * 2); |
158 | 169 | $col_char = imagecolorallocate($img, $this->def_col[0], $this->def_col[1], $this->def_col[2]); |
159 | 170 | $col_back = imagecolorallocate($img, $this->def_bgc[0], $this->def_bgc[1], $this->def_bgc[2]); |
160 | - $color = $col_char; // 文字描画色をデフォルト文字色に設定 | |
161 | - if ($this->is_trans) imagecolortransparent($img, $col_back); // 背景を透明色に設定する場合 | |
171 | + $color = $col_char; //文字描画色をデフォルト文字色に設定 | |
172 | + if ($this->is_trans) { //背景を透明色に設定する場合 | |
173 | + imagecolortransparent($img, $col_back); | |
174 | + } | |
162 | 175 | imagefill($img, 0, 0, $col_back); |
163 | 176 | |
164 | - // 各行ごとに処理 | |
177 | + //各行ごとに処理 | |
165 | 178 | $y_disp = $this->y_margin; |
166 | 179 | foreach (preg_split('/\n/', $msg, -1, PREG_SPLIT_NO_EMPTY) as $line) { |
167 | 180 | //この行でどれだけ消費するか計算 |
168 | 181 | //echo $line.'<br>'; |
169 | - $line_len = mb_strlen($line); | |
170 | - $line_plain = $this->GetMessage($line, $regex_str); | |
171 | - $r = imagettfbbox($this->size, 0, $this->font, $line_plain); | |
172 | - //echo "line_r: $line_len "; print_r($r); echo "<br>"; | |
182 | + $r = imagettfbbox($this->size, 0, $this->font, $this->GetMessage($line, $regex_str)); | |
183 | + //echo "line_r: " . mb_strlen($line); print_r($r); echo "<br>"; | |
173 | 184 | |
174 | 185 | // 強調部分の色を変えつつ表示 |
175 | - $array_msg = $regex_str == '' ? array(array($line, 0)) | |
176 | - : preg_split($regex_str, $line, -1, PREG_SPLIT_OFFSET_CAPTURE); | |
186 | + if ($regex_str == '') { | |
187 | + $array_msg = array(array($line, 0)); | |
188 | + } else { | |
189 | + $array_msg = preg_split($regex_str, $line, -1, PREG_SPLIT_OFFSET_CAPTURE); | |
190 | + } | |
177 | 191 | //$x_disp = $this->x_margin; |
178 | 192 | //echo print_r($array_msg, true) . '<br>'; |
179 | 193 | $str_total = ''; |
180 | 194 | $r_str_old = ''; //位置補正用 |
181 | - for ($i = 0; $i < count($array_msg); $i++) { | |
195 | + $count = count($array_msg); | |
196 | + for ($i = 0; $i < $count; $i++) { | |
182 | 197 | $str_len = mb_strlen($array_msg[$i][0]); |
183 | 198 | //echo 'str_r: ' . $str_len . ' -> "' . $array_msg[$i][0] . '"<br>'; |
184 | 199 | $str = mb_convert_encoding($array_msg[$i][0], 'UTF-8', 'auto'); |
@@ -188,7 +203,9 @@ | ||
188 | 203 | $r_str_total = imagettfbbox($this->size, 0, $this->font, $str_total); |
189 | 204 | if (is_array($r_str_old) && count($calib) > 0) { //位置補正処理 |
190 | 205 | $diff = $r_str_total[2] - ($r_str[2] + $r_str_old[2]); |
191 | - if ($diff != 0) $r_str[2] += floor($diff * array_shift($calib)); | |
206 | + if ($diff != 0) { | |
207 | + $r_str[2] += floor($diff * array_shift($calib)); | |
208 | + } | |
192 | 209 | } |
193 | 210 | $r_str_old = $r_str_total; //補正用の現在値を保存 |
194 | 211 | //echo print_r($r_str, true) . '<br>'; |
@@ -202,8 +219,7 @@ | ||
202 | 219 | //既に同じデリミタがスタックにある→現在の色指定を解除 |
203 | 220 | array_shift($d_stack); |
204 | 221 | $c_d = $this->GetDelimiter($d_stack[0]); |
205 | - } | |
206 | - else { | |
222 | + } else { | |
207 | 223 | //現在のデリミタをスタックに追加 |
208 | 224 | array_unshift($d_stack, $c_d->c); |
209 | 225 | } |