-
Notifications
You must be signed in to change notification settings - Fork 0
/
camogen.image.gd.php
327 lines (290 loc) · 8.83 KB
/
camogen.image.gd.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/***************************************************************************************************
* CAMOuflage GENerator for PHP
* _________ _____ ___ ____ ____ ____ ____
* / ___/ __ `/ __ `__ \/ __ \/ __ `/ _ \/ __ \
* / /__/ /_/ / / / / / / /_/ / /_/ / __/ / / /
* \___/\__,_/_/ /_/ /_/\____/\__, /\___/_/ /_/
* /____/
*
* @author Neil Withnall with thanks to Gael Lederrey and Ulf Åström
* @version 0.1 ("Stealthy Salamander")
* @licence MIT
* @system-requirements PHP 5.4+ with Imagick or GD extensions
*
* camogen for PHP (c) 2024 Neil Withnall (gammalogic). This software contains code that has been
* directly derived from, or inspired by, the camogen Python application (c) 2017 Gael Lederrey.
*
* Please see LICENSE for full copyright notice and licensing terms.
***************************************************************************************************/
class Image_Generator implements Image_Generator_Core
{
public static $extension = 'GD';
public static $img, $draw, $file_type;
public static $fill_color, $stroke_color, $stroke_width;
public static $allocated_colors = array();
public static function initialize($image_width, $image_height, $file_type='png')
{
self::$draw = imagecreatetruecolor($image_width, $image_height);
self::set_antialiasing_mode(true);
foreach (Pattern::$colors as $hex_color) {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
self::$allocated_colors[$hex_color] = imagecolorallocate(self::$draw, $r, $g, $b);
}
}
public static function get_extension()
{
return self::$extension;
}
public static function set_antialiasing_mode($antialiasing=true)
{
if (function_exists('imageantialias')) {
if ($antialiasing) {
imageantialias(self::$draw, true);
} else {
imageantialias(self::$draw, false);
}
}
}
public static function update_drawing()
{
// PLACEHOLDER
}
public static function set_fill_color($hex_color)
{
if (Helper::check_hex_color($hex_color)) {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
self::$fill_color = imagecolorallocate(self::$draw, $r, $g, $b);
}
}
public static function set_stroke_color($hex_color)
{
if (Helper::check_hex_color($hex_color)) {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
self::$stroke_color = imagecolorallocate(self::$draw, $r, $g, $b);
}
}
public static function set_stroke_width($stroke_width)
{
self::$stroke_width = $stroke_width;
imagesetthickness(self::$draw, self::$stroke_width);
}
public static function draw_bezier_curve($start_x, $start_y, $points)
{
foreach ($points as $key => $point) {
self::draw_cubic_bezier_curve_line_segments(array(
$start_x, // start point, x
$start_y, // start point, y
$point['x1'], // control point 1, x
$point['y1'], // control point 1, y
$point['x2'], // control point 2, x
$point['y2'], // control point 2, y
$point['xe'], // end point, x
$point['ye'], // end point, y
));
if (Pattern::$export_svg) {
SVG_Export::add_svg_cubic_bezier_curve(
array(
$start_x,
$start_y,
$point['x1'],
$point['y1'],
$point['x2'],
$point['y2'],
$point['xe'],
$point['ye'],
)
);
}
$start_x = $point['xe'];
$start_y = $point['ye'];
}
}
public static function draw_polygon($points, $hex_color, $apply_polygon_draw_style=false)
{
if (array_key_exists($hex_color, self::$allocated_colors)) {
$color = self::$allocated_colors[$hex_color];
} else {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
$color = imagecolorallocate(self::$draw, $r, $g, $b);
}
if ($apply_polygon_draw_style && Pattern::$polygon_draw_style === 'smooth') {
$points = Helper::calculate_catmull_rom_spline_interpolation_points($points);
}
if (Pattern::$is_PHP_8) {
imagefilledpolygon(
self::$draw,
$points,
$color
);
} else {
imagefilledpolygon(
self::$draw,
$points,
(count($points) / 2),
$color
);
}
if (Pattern::$export_svg) {
SVG_Export::add_svg_polygon(
$points,
$hex_color
);
}
}
public static function draw_ellipse($x, $y, $radius_x, $radius_y, $start_angle, $end_angle, $hex_color)
{
if (array_key_exists($hex_color, self::$allocated_colors)) {
$color = self::$allocated_colors[$hex_color];
} else {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
$color = imagecolorallocate(self::$draw, $r, $g, $b);
}
imagefilledellipse(
self::$draw,
$x,
$y,
($radius_x * 2),
($radius_y * 2),
$color
);
if (Pattern::$export_svg) {
SVG_Export::add_svg_circle(
$x,
$y,
$radius_x,
$hex_color
);
}
}
public static function draw_rectangle($x1, $y1, $x2, $y2, $hex_color)
{
if (array_key_exists($hex_color, self::$allocated_colors)) {
$color = self::$allocated_colors[$hex_color];
} else {
list($r,$g,$b) = Helper::get_rgb_from_hex_color($hex_color);
$color = imagecolorallocate(self::$draw, $r, $g, $b);
}
imagefilledrectangle(
self::$draw,
$x1,
$y1,
$x2,
$y2,
$color
);
if (Pattern::$export_svg) {
SVG_Export::add_svg_rectangle(
$x1,
$y1,
abs($x1 - $x2),
abs($y1 - $y2),
$hex_color
);
}
}
public static function apply_motion_blur($radius, $sigma, $angle)
{
// UNSUPPORTED WITH THIS EXTENSION; SEE README.md
}
public static function get_pixel_color($x, $y, $return_as_hex=true)
{
$colors = array();
$pixel = imagecolorat(self::$draw, (int) $x, (int) $y);
$colors_temp = imagecolorsforindex(self::$draw, $pixel);
$colors['r'] = $colors_temp['red'];
$colors['g'] = $colors_temp['green'];
$colors['b'] = $colors_temp['blue'];
if ($return_as_hex) {
return Helper::get_hex_color_from_rgb($colors['r'], $colors['g'], $colors['b'], true);
} else {
return $colors;
}
}
public static function get_image_type()
{
return $file_type;
}
public static function save_image_to_file($filename)
{
imagepng(self::$draw, $filename, 9); // GD drawing object, filename, lossless compression amount (0 = none, 9 = most)
imagedestroy(self::$draw);
try {
if (!file_exists($filename)) {
throw new Exception('Sorry, an error occurred (image was not saved to file)');
}
if (filesize($filename) === 0) {
throw new Exception('Sorry, an error occurred (image saved but file is 0 bytes in size)');
}
} catch (Exception $e) {
die($e->getMessage());
}
}
// Drop-in Bézier curve drawing routines
public static function draw_cubic_bezier_curve_line_segments($p)
{
// Draw a cubic Bézier curve using line segments
//
// @param array $p Cubic Bézier curve control points
//
// $p will contain the following values:
//
// $p[0] = xs, start point of curve, x
// $p[1] = ys, start point of curve, y
// $p[2] = x1, start point of first control point, x
// $p[3] = y1, start point of first control point, y
// $p[4] = x2, start point of second control point, x
// $p[5] = y2, start point of second control point, y
// $p[6] = xe, end point of curve, x
// $p[7] = ye, end point of curve, y
// Define the ratio; t is equivalent to the percentage of the distance along the curve
$t_step = 0.05; // 0.05 = 5%
// Calculate the vertices for the complete drawing path
$vertices = array();
$vertices[] = (int) $p[0]; // start point of curve, x
$vertices[] = (int) $p[1]; // start point of curve, y
for ($t = 0 + $t_step; $t < 1; $t += $t_step) {
$q = Helper::calculate_cubic_bezier_curve_interpolation_point($p, $t);
$vertices[] = (int) $q[0];
$vertices[] = (int) $q[1];
}
$vertices[] = (int) $p[6]; // end point of curve, x
$vertices[] = (int) $p[7]; // end point of curve, y
$nbr_vertices = count($vertices);
for ($i = 0; $i < $nbr_vertices; $i += 2) {
if (array_key_exists($i + 2, $vertices) && array_key_exists($i + 3, $vertices)) {
// GD's imageline() function does not always draw line caps perpendicularly, which
// means that gaps will appear at certain points in the curve at higher stroke
// widths; to correct this the line is over-drawn with ellipses of the same
// diameter as the line width, which generally fills any gaps but is not meant to
// be a comprehensive solution and is offered on a "best efforts" basis only
if (self::$stroke_width >= 5) {
imagefilledellipse(
self::$draw,
$vertices[$i],
$vertices[$i + 1],
self::$stroke_width,
self::$stroke_width,
self::$stroke_color
);
imagefilledellipse(
self::$draw,
$vertices[$i + 2],
$vertices[$i + 3],
self::$stroke_width,
self::$stroke_width,
self::$stroke_color
);
}
imageline(
self::$draw,
$vertices[$i],
$vertices[$i + 1],
$vertices[$i + 2],
$vertices[$i + 3],
self::$stroke_color
);
}
}
}
}