source: trunk/Packages/Common/Image.php

Last change on this file was 888, checked in by chronos, 2 years ago
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File size: 2.9 KB
Line 
1<?php
2
3define('COLOR_BLACK', 0x000000);
4define('COLOR_RED', 0xff0000);
5define('COLOR_GREEN', 0x00ff00);
6define('COLOR_BLUE', 0x0000ff);
7define('COLOR_WHITE', 0xffffff);
8
9class Font
10{
11 public int $Size;
12 public string $FileName;
13 public int $Color;
14
15 function __construct()
16 {
17 $this->Color = COLOR_BLACK;
18 $this->FileName = '../../Common/Fonts/FreeSans.ttf';
19 $this->Size = 10;
20 }
21}
22
23class Pen
24{
25 public int $Color;
26 public int $X;
27 public int $Y;
28
29 function __construct()
30 {
31 $this->Color = COLOR_BLACK;
32 $this->X = 0;
33 $this->Y = 0;
34 }
35
36}
37
38class Brush
39{
40 public int $Color;
41
42 function __construct()
43 {
44 $this->Color = COLOR_BLACK;
45 }
46
47}
48
49class Image
50{
51 public $Image;
52 public int $Type;
53 public Font $Font;
54 public Pen $Pen;
55 public Brush $Brush;
56
57 function __construct()
58 {
59 $this->Image = imagecreatetruecolor(1, 1);
60 $this->Type = IMAGETYPE_PNG;
61 $this->Pen = new Pen();
62 $this->Font = new Font();
63 $this->Brush = new Brush();
64 }
65
66 function SaveToFile(string $FileName): void
67 {
68 if ($this->Type == IMAGETYPE_JPEG)
69 {
70 imagejpeg($this->Image, $FileName);
71 } else
72 if ($this->Type == IMAGETYPE_GIF)
73 {
74 imagegif ($this->Image, $FileName);
75 } else
76 if ($this->Type == IMAGETYPE_PNG)
77 {
78 imagepng($this->Image, $FileName);
79 }
80 }
81
82 function LoadFromFile(string $FileName): void
83 {
84 $ImageInfo = getimagesize($FileName);
85 $this->Type = $ImageInfo[2];
86 if ($this->Type == IMAGETYPE_JPEG)
87 {
88 $this->Image = imagecreatefromjpeg($FileName);
89 } else
90 if ($this->Type == IMAGETYPE_GIF)
91 {
92 $this->Image = imagecreatefromgif ($FileName);
93 } else
94 if ( $this->Type == IMAGETYPE_PNG)
95 {
96 $this->Image = imagecreatefrompng($FileName);
97 }
98 }
99
100 function Output(): void
101 {
102 $this->SaveToFile(NULL);
103 }
104
105 function SetSize(int $Width, int $Height): void
106 {
107 $NewImage = imagecreatetruecolor($Width, $Height);
108 imagecopy($NewImage, $this->Image, 0, 0, 0, 0, $this->GetWidth(), $this->GetHeight());
109 imagedestroy($this->Image);
110 $this->Image = $NewImage;
111 }
112
113 function GetWidth(): int
114 {
115 return imagesx($this->Image);
116 }
117
118 function GetHeight(): int
119 {
120 return imagesy($this->Image);
121 }
122
123 function TextOut(int $X, int $Y, string $Text): void
124 {
125 imagettftext($this->Image, $this->Font->Size, 0, $X, $Y, $this->ConvertColor($this->Font->Color), $this->Font->FileName, $Text);
126 }
127
128 function ConvertColor(int $Color): int
129 {
130 return imagecolorallocate($this->Image, ($Color >> 16) & 0xff, ($Color >> 8) & 0xff, $Color & 0xff);
131 }
132
133 function FillRect(int $X1, int $Y1, int $X2, int $Y2): void
134 {
135 imagefilledrectangle($this->Image, $X1, $Y1, $X2, $Y2, $this->ConvertColor($this->Brush->Color));
136 }
137
138 function Line(int $X1, int $Y1, int $X2, int $Y2): void
139 {
140 imageline($this->Image, $X1, $Y1, $X2, $Y2, $this->ConvertColor($this->Pen->Color));
141 }
142}
Note: See TracBrowser for help on using the repository browser.