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
<?php
/* ---------------------------------------------------------------------------------------
* 「GDによる図形描画した画像を出力する処理」のサンプル
* 線幅を設定し、線、三角形、矩形を塗りつぶしと描画して、円の塗りつぶしと楕円を描画する
* 2016.08.29 作成 Yoshi of CXMedia Inc.
* --------------------------------------------------------------------------------------- */
// 出力先のパス
$out_path = 'img_sample/gd_draw01.png'; //PNG画像ファイル指定
/* ----- 新画像の領域生成 ----- */
$width = 500;
$height = 400;
$img_res = imagecreatetruecolor( $width, $height );
/* ----- 利用する色を作成 ----- */
$red = imagecolorallocate($img_res, 255, 0, 0);
$black = imagecolorallocate($img_res, 0x00, 0x00, 0x00);
$white = imagecolorallocate($img_res, 0xFF, 0xFF, 0xFF);
$blue = imagecolorallocate($img_res, 0x00, 0x00, 0xFF);
$yellow = imagecolorallocate($img_res, 0xFF, 0xFF, 0x00);
$green = imagecolorallocate($img_res, 0x00, 0xFF, 0x00);
$pink = imagecolorallocate($img_res, 0xFF, 0xCC, 0xFF);
/* ----- 矩形の背景カラー:黒で塗りつぶす ----- */
imagefilledrectangle($img_res, 0, 0, ($width-1), ($height-1), $black);
/* ----- 線幅を設定 -----
imagesetthickness ( $img_res, $thickness )
長方形、多角形、弧などを描画する際の線幅 thickness:ピクセル単位の線幅 */
imagesetthickness ( $img_res, '5');
/* ----- 線引き -----
imageline ( $img_res, $x1,$y1 , $x2,$y2 , $color )
x1,y1 : 開始位置(座標)、x2,y2 : 終了位置(座標)、$color:塗りつぶし色 */
imageline($img_res, 350, 50, 450, 350, $red);
/* ----- 四角の塗りつぶしと描画 -------
imagerectangle ( $img_res, $x1, $y1, $x2, $y2, $color) */
imagefilledrectangle($img_res,50,50,300,300,$blue);
imagerectangle($img_res,50,50,300,300,$white);
/* ----- 円の塗りつぶし -------
imagefilledellipse ( $img_res, $cx, $cy, $width, $height, $color )
cx:中心のx座標、cy:中心のy座標 */
imagefilledellipse($img_res,150,150,100,100,$yellow);
/* ----- 楕円を描く -------
imageellipse ( $img_res , $cx, $cy, $width, $height, $color ) */
imageellipse($img_res, 220, 170, 300, 200, $pink);
/* ----- 三角形の塗りつぶしと描画 -------
imagefilledpolygon ( $img_res, $points, $num_points, $color )
points:多角形の頂点の座標 x および y を含む配列、num_points:頂点の総数 */
$points = array(
'300','100',
'350','200',
'250','200'
);
imagefilledpolygon($img_res, $points, 3, $green);
imagepolygon($img_res, $points, 3, $yellow);
//png画像出力
imagepng($img_res,$out_path);
//画像リソースの開放
imagedestroy($img_res);
// 作成した画像の表示処理
echo "<img src=\"$out_path\" alt='作成した図形のPNG画像'>";
?>