source: trunk/Packages/Common/Image.php

Last change on this file was 95, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
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
37class Brush
38{
39 public int $Color;
40
41 function __construct()
42 {
43 $this->Color = COLOR_BLACK;
44 }
45}
46
47class Image
48{
49 public $Image;
50 public int $Type;
51 public Font $Font;
52 public Pen $Pen;
53 public Brush $Brush;
54
55 function __construct()
56 {
57 $this->Image = imagecreatetruecolor(1, 1);
58 $this->Type = IMAGETYPE_PNG;
59 $this->Pen = new Pen();
60 $this->Font = new Font();
61 $this->Brush = new Brush();
62 }
63
64 function SaveToFile(string $FileName): void
65 {
66 if ($this->Type == IMAGETYPE_JPEG)
67 {
68 imagejpeg($this->Image, $FileName);
69 } else
70 if ($this->Type == IMAGETYPE_GIF)
71 {
72 imagegif($this->Image, $FileName);
73 } else
74 if ($this->Type == IMAGETYPE_PNG)
75 {
76 imagepng($this->Image, $FileName);
77 }
78 }
79
80 function LoadFromFile(string $FileName): void
81 {
82 $ImageInfo = getimagesize($FileName);
83 $this->Type = $ImageInfo[2];
84 if ($this->Type == IMAGETYPE_JPEG)
85 {
86 $this->Image = imagecreatefromjpeg($FileName);
87 } else
88 if ($this->Type == IMAGETYPE_GIF)
89 {
90 $this->Image = imagecreatefromgif ($FileName);
91 } else
92 if ( $this->Type == IMAGETYPE_PNG)
93 {
94 $this->Image = imagecreatefrompng($FileName);
95 }
96 }
97
98 function Output(): void
99 {
100 $this->SaveToFile('');
101 }
102
103 function SetSize(int $Width, int $Height): void
104 {
105 $NewImage = imagecreatetruecolor($Width, $Height);
106 imagecopy($NewImage, $this->Image, 0, 0, 0, 0, $this->GetWidth(), $this->GetHeight());
107 imagedestroy($this->Image);
108 $this->Image = $NewImage;
109 }
110
111 function GetWidth(): int
112 {
113 return imagesx($this->Image);
114 }
115
116 function GetHeight(): int
117 {
118 return imagesy($this->Image);
119 }
120
121 function TextOut(int $X, int $Y, string $Text): void
122 {
123 imagettftext($this->Image, $this->Font->Size, 0, $X, $Y, $this->ConvertColor($this->Font->Color), $this->Font->FileName, $Text);
124 }
125
126 function ConvertColor(int $Color): int
127 {
128 return imagecolorallocate($this->Image, ($Color >> 16) & 0xff, ($Color >> 8) & 0xff, $Color & 0xff);
129 }
130
131 function FillRect(int $X1, int $Y1, int $X2, int $Y2): void
132 {
133 imagefilledrectangle($this->Image, $X1, $Y1, $X2, $Y2, $this->ConvertColor($this->Brush->Color));
134 }
135
136 function Line(int $X1, int $Y1, int $X2, int $Y2): void
137 {
138 imageline($this->Image, $X1, $Y1, $X2, $Y2, $this->ConvertColor($this->Pen->Color));
139 }
140}
Note: See TracBrowser for help on using the repository browser.