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
<?php
/* ---------------------------------------------------------------------------------------
* 「GDによるフォント指定して、矩形上に文字列の画像を出力する処理」のサンプル
* 3種類のフォント出力形態と、それぞれPNG,JPG,GIF画像でファイル出力
* 2016.08.29 作成 Yoshi of CXMedia Inc.
* --------------------------------------------------------------------------------------- */
// 出力先のパス
$out_path = 'img_sample/gd_rect_string.png'; //PNG画像ファイル指定
$out2_path = 'img_sample/gd_rect_string2.jpg'; //JPG画像ファイル指定
$out3_path = 'img_sample/gd_rect_string3.gif'; //GIF画像ファイル指定
/* ----- 新画像の領域生成 -----
imagecreatetruecolor( $width,$height ); */
$width = 200;
$height = 50;
$img_res = imagecreatetruecolor( $width, $height );
$img_copy = imagecreatetruecolor( $width, $height );
$img_frt = imagecreatetruecolor( $width, $height );
/* ----- 利用する色を作成 -----
* imagecolorallocate ( $img_res,$R ,$G ,$B );
RGBは、カラー値(0 ~ 255) 又は (0x00 ~ 0XFFの16進数) */
$red = imagecolorallocate($img_res, 255, 0, 0);
$black = imagecolorallocate($img_res, 0x00, 0x00, 0x00);
$white = imagecolorallocate($img_res, 0xFF, 0xFF, 0xFF);
/* ----- 矩形のカラーで塗りつぶす -----
* imagefilledrectangle( $img_res, $x1,$y1 , $x2,$y2 , $color );
x1,y1 : 開始位置(座標)、x2,y2 : 終了位置(座標)、$color:塗りつぶし色 */
imagefilledrectangle($img_res, 0, 0, ($width-1), ($height-1), $black);
imagefilledrectangle($img_frt, 0, 0, ($width-1), ($height-1), $red);
/* ----- 画像リソースのコピー ------
* imagecopy ( $dst_im , $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h )
dst_im:コピー先の画像リンクリソース、src_im:コピー元の画像リンクリソース、
dst_x:コピー先の x 座標、dst_y:コピー先の y 座標、
src_x:コピー元の x 座標、src_y:コピー元の y 座標、src_w:コピー元の幅、src_h:コピー元の高さ */
imagecopy($img_copy,$img_res, 0, 0, 0, 0, $width, $height);
// 描画する文字列
$text = 'CXMedia Site';
// フォントのパス設定(C:\Windows\Fontsにある'Arial極太'をコピーしてWEBサイト内に設置したもの)
// 日本語のMSゴシックは、'msgothic.ttc'です。
$font = 'fonts\ariblk.ttf';
/* ----- TrueTypeフォントを使用し、テキストを画像に書き込む -----
* imagettftext( $img_res, $size, $angle, $x, $y, $color, $font, $text );
$size:フォントサイズ、$angle:文字の方向、$x:最初の文字位置、$y:フォントのペースライン位置 */
imagettftext($img_res, 18, 0, 10, 32, $red, $font, $text);
/* ----- FreeType2 によるフォントを使用し、テキストを画像に書き込む -----
* imagefttext( $img_res, $size, $angle, $x, $y, $color, $font, $text [,$extrainfo ] );
$extrainfo:配列でキーlinespacing(描画時の行間)指定(機能確認できず)⇒例:array('linespacing'=>1.0) */
imagefttext($img_frt, 18, 0, 10, 32, $black, $font, $text );
/* ----- テキストを画像に書き込む -----
* imagestring ( $im_res, $font, $x, $y, $string, $color );
$font:latin2 (エンコーディングの組み込みのフォントの場合は 1, 2, 3, 4, 5 のいずれか)
imageloadfont() で登録したフォントの識別子のいずれか。 、$x:左上隅のx座標、$y:左上隅のy座標 */
imagestring($img_copy, 5, 20, 15, $text, $white);
/* ----- png画像作成 -----
* imagepng ( $img_res [, $out_path [, $compress_quality [, $filters ]]] ); */
imagepng($img_res,$out_path);
/* ----- jpg画像作成 -----
* imagejpeg ( $im_res [, $to [, $quality ]] ); */
imagejpeg($img_copy,$out2_path);
/* ----- gif画像作成 -----
* imagegif ( $im_res [, $to] ); */
imagegif($img_frt,$out3_path);
// 画像リソースの開放
imagedestroy($img_res);
imagedestroy($img_copy);
imagedestroy($img_frt);
// 作成した画像の表示処理
echo "<img src=\"$out_path\" alt='作成したPNG画像'>"," <img src=\"$out2_path\" alt='作成したJPG画像'>",
" <img src=\"$out3_path\" alt='作成したGIF画像'>";
?>