-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVWImage.php
More file actions
419 lines (360 loc) · 13.5 KB
/
VWImage.php
File metadata and controls
419 lines (360 loc) · 13.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* Vertiacal Writing Image
*
* @author massat
* @copyright Copyright(c) 2009 Murashiki co.,ltd.
* @license http://seeds.ville.jp/vwimage/LICENSE
*/
require_once dirname(__FILE__) . '/VWInvalidParameterException.php';
/**
* Vertiacal Writing Image
*
*/
class VWImage
{
// 画像フォーマット
const PNG_FORMAT = 'png';
const JPG_FORMAT = 'jpg';
const GIF_FORMAT = 'gif';
// 水平位置
const ALIGN_RIGHT = 'right';
const ALIGN_CENTER = 'center';
const ALIGN_LEFT = 'left';
// 垂直位置
const VALIGN_TOP = 'top';
const VALIGN_MIDDLE = 'middle';
const VALIGN_BOTTOM = 'bottom';
private static $encoding = 'utf-8';
private $width; // 画像幅
private $height; // 画像高
private $align; // 水平位置
private $valign; // 垂直位置
private $font; // フォント
private $font_size; // 文字サイズ(px)
private $color; // 文字色
private $bg_color; // 背景色
private $alpha; // 透過度
private $quality; // 文字クオリティ
private $lines;
private $image;
/**
* constructor
*
* @param string $string 描画する文字列
* @param array $params 描画パラメーター
* @throws VWInvalidParameterException
*
* * params (default)
* - font-size : 文字サイズ (40)
* - color : 文字色 (000000)
* - bg-color : 背景色 (FFFFFF)
* - alpha ; 透過度 (0)
* - width : 画像幅
* - height : 画像高
* - align : 水平位置 (right)
* - valign : 垂直位置 (top)
* - font : フォント (ipam)
* - quality : 文字のクオリティ
*/
public function __construct($string, array $params = array())
{
$string = mb_convert_kana($string, 'AK', self::$encoding); // 半角 -> 全角
$params = $this->validate_params($params);
$default_styles = self::get_default_styles();
$this->lines = self::explode_string($string);
$this->font_size = isset($params['font-size']) ? $params['font-size'] : $default_styles['font-size'];
$this->color = isset($params['color']) ? $params['color'] : $default_styles['color'];
$this->bg_color = isset($params['bg-color']) ? $params['bg-color'] : $default_styles['bg-color'];
$this->alpha = isset($params['alpha']) ? $params['alpha'] : $default_styles['alpha'];
$this->width = isset($params['width']) ? $params['width'] : $this->calc_image_width();
$this->height = isset($params['height']) ? $params['height'] : $this->calc_image_height();
$this->align = isset($params['align']) ? $params['align'] : $default_styles['align'];
$this->valign = isset($params['valign']) ? $params['valign'] : $default_styles['valign'];
$this->font = isset($params['font']) ? $params['font'] : $default_styles['font'];
$this->quality = isset($params['quality']) ? $params['quality'] : $default_styles['quality'];
}
/**
* destructor
*
*/
function __destruct()
{
if(is_resource($this->image)) imagedestroy($this->image);
}
/**
* 画像データをPNG形式で取得するメソッド
*
* @return string
*/
public function to_png()
{
$this->create_image(self::PNG_FORMAT);
ob_start();
imagepng($this->image, null, 1);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* 画像データをGIF形式で取得するメソッド
*
* @return string
*/
public function to_gif()
{
$this->alpha = 0; // alphaはpngのみ対応
$this->create_image(self::GIF_FORMAT);
ob_start();
imagegif($this->image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* 画像データをJPG形式で取得するメソッド
*
* @return string
*/
public function to_jpg()
{
$this->alpha = 0; // alphaはpngのみ対応
$this->create_image(self::JPG_FORMAT);
ob_start();
imagejpeg($this->image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
//////////////////////////////////////////////////////
// P R I V A T E M E T H O D S
//////////////////////////////////////////////////////
// 画像生成
private function create_image($format)
{
$this->create_image_base($format);
$this->paste_strings($format);
}
private function create_image_base($format)
{
$image = imageCreateTrueColor($this->width, $this->height);
list($r, $g, $b) = self::hex_to_rgb($this->bg_color);
$background = imageColorAllocateAlpha($image, $r, $g, $b, $this->alpha);
imageLayerEffect($image, IMG_EFFECT_REPLACE);
imageFilledRectangle($image, 0, 0, $this->width, $this->height, $background);
imageSaveAlpha($image, true);
$this->image = $image;
}
private function paste_strings($format)
{
$offset_x = $this->calc_offset_x();
$offset_y = $this->calc_offset_y();
$this->lines = array_reverse($this->lines);
foreach($this->lines as $i => $string) {
$offset_x += $i * $this->font_size;
$this->paste_string($string, $offset_x, $offset_y);
}
}
private function paste_string($string, $offset_x, $offset_y)
{
$canvas_size = $this->quality;
$canvas = imageCreateTrueColor($canvas_size, $canvas_size);
$bg_color = self::hex_to_rgb($this->bg_color);
$bg_color = imageColorAllocateAlpha($canvas, $bg_color[0], $bg_color[1], $bg_color[2], $this->alpha);
$color = self::hex_to_rgb($this->color);
$color = imageColorAllocate($canvas, $color[0], $color[1], $color[2]);
$length = mb_strlen($string, self::$encoding);
for($i=0; $i<$length; $i++) {
$char = mb_substr($string, $i, 1, self::$encoding);
$y = $offset_y + $i * $this->font_size;
imageLayerEffect($canvas, IMG_EFFECT_REPLACE);
imageFilledRectangle($canvas, 0, 0, $canvas_size, $canvas_size, $bg_color); // 背景色で塗りつぶして使いまわす
imageLayerEffect($canvas, IMG_EFFECT_ALPHABLEND);
imageTTFText($canvas, $canvas_size * 60 / 96, 0, 0, $canvas_size * 0.9, $color, $this->font, $char);
// 一部の文字は回転
if(($angle = self::get_rotate_angle($char)) !== 0) {
$canvas = imagerotate($canvas, $angle, $bg_color, true);
}
imageCopyResampled($this->image, $canvas, $offset_x, $y, 0, 0, $this->font_size, $this->font_size, $canvas_size, $canvas_size);
imagesavealpha($this->image, true);
}
imagedestroy($canvas);
}
// ヘルパー /////////////////
private static function get_default_styles()
{
return array('width' => null,
'height' => null,
'align' => self::ALIGN_RIGHT,
'valign' => self::VALIGN_TOP,
'font' => 'ipam',
'font-size' => 40,
'color' => '000000',
'bg-color' => 'FFFFFF',
'alpha' => 0,
'format' => self::PNG_FORMAT,
'quality' => 200);
}
private static function validate_params(array $params)
{
$clean = array();
if(isset($params['font-size'])) {
$font_size = $params['font-size'];
if(!ctype_digit($font_size) || !$font_size) {
throw new VWInvalidParameterException("invalid font-size: {$font_size}");
}
$clean['font-size'] = $font_size;
}
if(isset($params['color'])) {
$color = $params['color'];
$length = strlen($color);
if(!preg_match('/^[0-9a-f]+$/i', $color) || $length != 6) {
throw new VWInvalidParameterException("invalid color: {$color}");
}
$clean['color'] = $color;
}
if(isset($params['bg-color'])) {
$bg_color = $params['bg-color'];
$length = strlen($bg_color);
if(!preg_match('/^[0-9a-f]+$/i', $bg_color) || $length != 6) {
throw new VWInvalidParameterException("invalid bg-color: {$bg_color}");
}
$clean['bg-color'] = $bg_color;
}
if(isset($params['alpha'])) {
$alpha = $params['alpha'];
if(!ctype_digit($alpha) || $alpha < 0 || $alpha > 127) {
throw new VWInvalidParameterException("invalid alpha: {$alpha}");
}
$clean['alpha'] = $alpha;
}
if(isset($params['width'])) {
$width = $params['width'];
if(!ctype_digit($width) || !$width) {
throw new VWInvalidParameterException("invalid width: {$width}");
}
$clean['width'] = $width;
}
if(isset($params['height'])) {
$height = $params['height'];
if(!ctype_digit($height) || !$height) {
throw new VWInvalidParameterException("invalid height: {$height}");
}
$clean['height'] = $height;
}
if(isset($params['align'])) {
$align = $params['align'];
if(!in_array($align, array(self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_LEFT))) {
throw new VWInvalidParameterException("invalid align: {$align}");
}
$clean['align'] = $align;
}
if(isset($params['valign'])) {
$valign = $params['valign'];
if(!in_array($valign, array(self::VALIGN_TOP, self::VALIGN_MIDDLE, self::VALIGN_BOTTOM))) {
throw new VWInvalidParameterException("invalid valign: {$valign}");
}
$clean['valign'] = $valign;
}
if(isset($params['font'])) {
$font = $params['font'];
$font_paths = explode(PATH_SEPARATOR, getenv('GDFONTPATH'));
$found = false;
foreach($font_paths as $font_path) {
if(is_file(realpath($font_path) . "/{$font}.ttf")) {
$found = true;
break;
}
}
if(!$found) {
throw new VWInvalidParameterException("invalid font: {$font}");
}
$clean['font'] = $font;
}
if(isset($params['quality'])) {
$quality = $params['quality'];
if(!ctype_digit($quality) || !$quality) {
throw new VWInvalidParameterException("invalid quality: {$quality}");
}
$clean['quality'] = $quality;
}
return $clean;
}
private function calc_image_width()
{
return count($this->lines) * $this->font_size;
}
private function calc_image_height()
{
$max_length = 0;
foreach($this->lines as $line) {
$max_length = max($max_length, mb_strlen($line, self::$encoding) * $this->font_size);
}
return $max_length;
}
private function calc_offset_x()
{
$offset = 0;
switch($this->align) {
case self::ALIGN_RIGHT:
$offset = $this->width - $this->calc_image_width();
break;
case self::ALIGN_CENTER:
$offset = ($this->width - $this->calc_image_width()) / 2;
break;
case self::ALIGN_LEFT:
$offset = 0;
}
return $offset;
}
private function calc_offset_y()
{
$offset = 0;
switch($this->valign) {
case self::VALIGN_TOP:
$offset = 0;
break;
case self::VALIGN_MIDDLE:
$offset = ($this->height - $this->calc_image_height()) / 2;
break;
case self::VALIGN_BOTTOM:
$offset = $this->height - $this->calc_image_height();
break;
}
return $offset;
}
private static function get_rotate_angle($char)
{
$angle = 0;
if(strpos('-ー–−—「」[][]【】()()==', $char) !== false) $angle = -90;
if(strpos('、。', $char) !== false) $angle = 180;
return $angle;
}
private static function explode_string($string)
{
$string = str_replace(array("\r\n", "\r"), "\n", $string);
return explode("\n", $string);
}
private static function hex_to_rgb($hex)
{
$r = substr($hex, 0, 2);
$g = substr($hex, 2, 2);
$b = substr($hex, 4, 2);
return array(hexdec($r), hexdec($g), hexdec($b));
}
}
/**
* GDがフォントを検索するパスを指定
*
*/
function fix_font_path()
{
$font_dir = dirname(__FILE__) . '/font/';
putenv("GDFONTPATH={$font_dir}" . PATH_SEPARATOR . getenv('GDFONTPATH'));
$entries = scandir($font_dir);
foreach($entries as $entry) {
$path = $font_dir . $entry;
if(is_dir($path) && !preg_match('/^\.+$/', $entry)) putenv("GDFONTPATH={$path}" . PATH_SEPARATOR . getenv('GDFONTPATH'));
}
}
fix_font_path();