|
13 | 13 | * <ins>IPA モナーフォント 1.0.3 で動作確認、行間をチューニングしている。</ins> |
14 | 14 | * |
15 | 15 | * Dependencies: |
16 | | - * - PHP Version: 4.2.0 or newer (rep2-expack requires 4.4.1 or newer) |
| 16 | + * - PHP Version: 4.2.0 or newer (rep2-expack requires 5.2.3 or newer) |
17 | 17 | * - PHP Extension: gd (with FreeType 2) |
18 | 18 | * - PHP Extension: mbstring |
19 | 19 | * - PHP Extension: pcre |
20 | 20 | * |
21 | 21 | * TODO: |
22 | 22 | * - ページ遷移と画像の部分拡大リンク付きHTML生成 |
23 | | - * - フォントサイズ決め打ちで描画して、あとから縮小する..かも? |
24 | 23 | * |
25 | 24 | * NOTICE: |
26 | 25 | * PHP (or PHP GDモジュール) をコンパイルするときの configure のオプションに |
|
197 | 196 | } |
198 | 197 | $inline = false; |
199 | 198 | $rotate = !empty($_POST['aas_rotate']); |
200 | | - $bold = ($_conf['expack.aas.bold'] || !empty($_POST['aas_bold'])); |
201 | 199 | } else { |
202 | 200 | $params = array('host' => 'string', 'bbs' => 'string', 'key' => 'int', 'resnum' => 'int'); |
203 | 201 | foreach ($params as $name => $type) { |
|
211 | 209 | } |
212 | 210 | $inline = !empty($_GET['inline']); |
213 | 211 | $rotate = !empty($_GET['rotate']); |
214 | | - $bold = ($_conf['expack.aas.bold'] || !empty($_GET['bold'])); |
215 | 212 | } |
216 | 213 |
|
217 | 214 | // レス読み込み |
|
287 | 284 | $c = 0; |
288 | 285 | foreach ($lines as $line) { |
289 | 286 | if (strlen($line) > 0) { |
290 | | - $b = imagettfbbox(12, 0, $font, $line); |
| 287 | + $b = imagettfbbox(16, 0, $font, $line); |
291 | 288 | if (!$c) { |
292 | 289 | $c = $b[2]; |
293 | 290 | $hint = $line; |
|
301 | 298 | } |
302 | 299 | } |
303 | 300 |
|
304 | | -// 画像サイズを決定 |
| 301 | +// 画像サイズ等を決定 |
305 | 302 | if ($inline) { |
306 | | - $default_width = $_conf['expack.aas.image_width_il']; |
307 | | - $default_height = $_conf['expack.aas.image_height_il']; |
308 | | -} elseif (!$_conf['ktai']) { |
309 | | - $default_width = $_conf['expack.aas.image_width_pc']; |
310 | | - $default_height = $_conf['expack.aas.image_height_pc']; |
| 303 | + $mode = 'inline'; |
| 304 | +} elseif ($_conf['ktai']) { |
| 305 | + $mode = 'mobile'; |
311 | 306 | } else { |
312 | | - $default_width = $_conf['expack.aas.image_width']; |
313 | | - $default_height = $_conf['expack.aas.image_height']; |
| 307 | + $mode = 'default'; |
314 | 308 | } |
| 309 | +$image_type = $_conf["expack.aas.{$mode}.type"]; |
| 310 | +$quality = $_conf["expack.aas.{$mode}.quality"]; |
| 311 | +$width = $_conf["expack.aas.{$mode}.width"]; |
| 312 | +$height = $_conf["expack.aas.{$mode}.height"]; |
| 313 | +$margin = $_conf["expack.aas.{$mode}.margin"]; |
| 314 | +$fontsize = $_conf["expack.aas.{$mode}.fontsize"]; |
| 315 | +$overflow = $_conf["expack.aas.{$mode}.overflow"]; |
| 316 | +$bold = $_conf["expack.aas.{$mode}.bold"]; |
| 317 | +$fgcolor = $_conf["expack.aas.{$mode}.fgcolor"]; |
| 318 | +$bgcolor = $_conf["expack.aas.{$mode}.bgcolor"]; |
315 | 319 | if ($rotate) { |
316 | | - list($default_width, $default_height) = array($default_height, $default_width); |
317 | | -} |
318 | | - |
319 | | -// 画像サイズに合わせてフォントサイズを調節 |
320 | | -if ($inline && $_conf['expack.aas.inline_fontsize']) { |
321 | | - $size = $_conf['expack.aas.inline_fontsize']; |
322 | | -} else { |
323 | | - $size = $_conf['expack.aas.max_fontsize']; |
324 | | - list($width, $height) = aas_getTextBoxSize($size, $font, $hint, $lc); |
325 | | - $max_width = $default_width - 2; |
326 | | - $max_height = $default_height - 2; |
327 | | - $ratio = max($width / $max_width, $height / $max_height); |
328 | | - if ($ratio > 1.0) { |
329 | | - $size = max(intval($_conf['expack.aas.max_fontsize'] / $ratio), $_conf['expack.aas.min_fontsize']); |
330 | | - while ($size > $_conf['expack.aas.min_fontsize'] && |
331 | | - !aas_isTextInPicture($size, $font, $hint, $lc, $max_width, $max_height)) |
332 | | - { |
333 | | - $size--; |
334 | | - } |
335 | | - } |
| 320 | + list($width, $height) = array($height, $width); |
336 | 321 | } |
337 | 322 |
|
338 | 323 | // イメージ作成 |
339 | | -if ($_conf['expack.aas.trim']) { |
340 | | - list($width, $height) = aas_getTextBoxSize($size, $font, $hint, $lc); |
341 | | - $width = min($default_width, $width + 5); |
342 | | - $height = min($default_height, $height + 5); |
| 324 | +list($image_width, $image_height) = aas_getTextBoxSize($fontsize, $font, $hint, $lc, $margin); |
| 325 | +if ($overflow) { |
| 326 | + $image_width = min($width, $image_width); |
| 327 | + $image_height = min($height, $image_height); |
| 328 | +} |
| 329 | +$image = imagecreatetruecolor($image_width, $image_height); |
| 330 | +if ($bgcolor && false !== ($c = aas_parseColor($bgcolor))) { |
| 331 | + $bgcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]); |
343 | 332 | } else { |
344 | | - $width = $default_width; |
345 | | - $height = $default_height; |
| 333 | + $bgcolor = imagecolorallocate($image, 255, 255, 255); |
346 | 334 | } |
347 | | - |
348 | | -$image = imagecreate($width, $height); |
349 | | -if (isset($_conf['expack.aas.fgcolor']) && |
350 | | - false !== ($c = aas_parseColor($_conf['expack.aas.fgcolor']))) |
351 | | -{ |
| 335 | +if ($fgcolor && false !== ($c = aas_parseColor($fgcolor))) { |
352 | 336 | $fgcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]); |
353 | 337 | } else { |
354 | 338 | $fgcolor = imagecolorallocate($image, 0, 0, 0); |
355 | 339 | } |
356 | | -if (isset($_conf['expack.aas.bgcolor']) && |
357 | | - false !== ($c = aas_parseColor($_conf['expack.aas.bgcolor']))) |
358 | | -{ |
359 | | - $bgcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]); |
360 | | -} else { |
361 | | - $bgcolor = imagecolorallocate($image, 255, 255, 255); |
362 | | -} |
363 | 340 | imagefill($image, 0, 0, $bgcolor); |
364 | 341 |
|
365 | 342 | // テキスト描画 |
366 | 343 | $x_adjust = 1; |
367 | | -$y_adjust = $size + floor($size / AAS_Y_ADJUST_P1) + AAS_Y_ADJUST_P2; |
368 | | -$x_pos = $x_adjust; |
369 | | -$y_pos = $y_adjust; |
| 344 | +$y_adjust = $fontsize + floor($fontsize / AAS_Y_ADJUST_P1) + AAS_Y_ADJUST_P2; |
| 345 | +$x_pos = $margin + $x_adjust; |
| 346 | +$y_pos = $margin + $y_adjust; |
370 | 347 | // まとめて描画しようとすると長い文字列でエラーが出るので |
371 | | -//imagettftext($image, $size, 0, $x_pos, $y_pos, $fgcolor, $font, $text); |
| 348 | +//imagettftext($image, $fontsize, 0, $x_pos, $y_pos, $fgcolor, $font, $text); |
372 | 349 | // 一行ずつ描画する |
373 | 350 | foreach ($lines as $line) { |
374 | | - imagettftext($image, $size, 0, $x_pos, $y_pos, $fgcolor, $font, $line); |
| 351 | + imagettftext($image, $fontsize, 0, $x_pos, $y_pos, $fgcolor, $font, $line); |
375 | 352 | // 太字は1ピクセル右に重ねて描画 |
376 | 353 | if ($bold) { |
377 | | - imagettftext($image, $size, 0, $x_pos + 1, $y_pos, $fgcolor, $font, $line); |
| 354 | + imagettftext($image, $fontsize, 0, $x_pos + 1, $y_pos, $fgcolor, $font, $line); |
378 | 355 | } |
379 | 356 | $y_pos += $y_adjust; |
| 357 | + if (!$overflow && $y_pos >= $height) { |
| 358 | + break; |
| 359 | + } |
| 360 | +} |
| 361 | + |
| 362 | +// リサイズ |
| 363 | +if (!$overflow && ($image_width > $width || $image_height > $height)) { |
| 364 | + if ($image_width > $width) { |
| 365 | + $height = $image_height * ($width / $image_width); |
| 366 | + } |
| 367 | + if ($image_height > $height) { |
| 368 | + $width = $image_width * ($height / $image_height); |
| 369 | + } |
| 370 | + $width = (int)floor($width); |
| 371 | + $height = (int)floor($height); |
| 372 | + $new_image = imagecreatetruecolor($width, $height); |
| 373 | + imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height); |
| 374 | + imagedestroy($image); |
| 375 | + $image = $new_image; |
380 | 376 | } |
381 | 377 |
|
382 | 378 | // 回転 |
|
393 | 389 |
|
394 | 390 | // 画像を出力 |
395 | 391 | if (!headers_sent()) { |
396 | | - switch ($_conf['expack.aas.image_type']) { |
| 392 | + switch ($image_type) { |
397 | 393 | case 1: |
398 | 394 | header('Content-Type: image/jpeg'); |
399 | | - imagejpeg($image, '', $_conf['expack.aas.jpeg_quality']); |
| 395 | + imagejpeg($image, '', $quality); |
400 | 396 | break; |
401 | 397 | case 2: |
402 | 398 | header('Content-Type: image/gif'); |
@@ -506,29 +502,14 @@ function aas_decodeHTMLEntity($e) |
506 | 502 | /** |
507 | 503 | * テキストボックスの大きさを計算する |
508 | 504 | */ |
509 | | -function aas_getTextBoxSize($size, $font, $hint, $lines) |
| 505 | +function aas_getTextBoxSize($size, $font, $hint, $lines, $margin) |
510 | 506 | { |
511 | | - $x_adjust = 5; |
512 | | - $y_adjust = ($size + floor($size / AAS_Y_ADJUST_P1) + AAS_Y_ADJUST_P2) * ($lines - 1); |
| 507 | + $x_adjust = ($margin * 2) + 5; |
| 508 | + $y_adjust = ($margin * 2) + (($size + floor($size / AAS_Y_ADJUST_P1) + AAS_Y_ADJUST_P2) * ($lines - 1)); |
513 | 509 | $box = imagettfbbox($size, 0, $font, $hint); |
514 | 510 | $box_width = max($box[0], $box[2], $box[4], $box[6]) - min($box[0], $box[2], $box[4], $box[6]) + $x_adjust; |
515 | 511 | $box_height = max($box[1], $box[3], $box[5], $box[7]) - min($box[1], $box[3], $box[5], $box[7]) + $y_adjust; |
516 | | - return array($box_width, $box_height); |
517 | | -} |
518 | | - |
519 | | -// }}} |
520 | | -// {{{ aas_isTextInPicture() |
521 | | - |
522 | | -/** |
523 | | - * テキストが画像に収まり切るかチェックする |
524 | | - */ |
525 | | -function aas_isTextInPicture($size, $font, $hint, $lines, $max_width, $max_height) |
526 | | -{ |
527 | | - list($box_width, $box_height) = aas_getTextBoxSize($size, $font, $hint, $lines); |
528 | | - if ($box_width > $max_width || $box_height > $max_height) { |
529 | | - return false; |
530 | | - } |
531 | | - return true; |
| 512 | + return array((int)$box_width, (int)$box_height); |
532 | 513 | } |
533 | 514 |
|
534 | 515 | // }}} |
|