-
Notifications
You must be signed in to change notification settings - Fork 40
/
raidpicture.php
636 lines (541 loc) · 23.7 KB
/
raidpicture.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
// Include requirements and perfom initial steps
include_once(__DIR__ . '/core/bot/requirements.php');
include_once(CORE_BOT_PATH . '/db.php');
if ($metrics){
$requests_total->inc(['raidpicture']);
}
// Create GD image object from given URI regardless of file type
function grab_img($uri){
try {
$img = imagecreatefromstring(file_get_contents($uri));
} catch (Exception $e) {
info_log($uri, 'Failed to get image:');
info_log($e->getMessage(), 'Reason: ');
return false;
}
return $img;
}
if(isset($_GET['sa']) && $_GET['sa'] == 1) $standalone_photo = true; else $standalone_photo = false;
// Debug switch
$debug = false;
if(isset($_GET['debug']) && $_GET['debug'] == 1) {
$debug = true;
}
$required_parameters = ['pokemon', 'pokemon_form', 'start_time', 'end_time', 'gym_id', 'ex_raid'];
$failed = [];
// Raid info
foreach($required_parameters as $required) {
if(!array_key_exists($required, $_GET)) {
$failed[] = $required;
}
}
if(count($failed) > 0) {
info_log('Raidpicture called without '.join(', ',$failed).', ending execution');
exit();
}
$raid = [];
$raid['pokemon'] = preg_replace("/\D/","",$_GET['pokemon']);
$raid['gym_id'] = preg_replace("/\D/","",$_GET['gym_id']);
$raid['raid_costume'] = false;
if($_GET['start_time'] == 0) {
$raid_ongoing = false;
}else {
$raid_ongoing = true;
$raid['start_time'] = date("Y-M-d H:i:s",preg_replace("/\D/","",$_GET['start_time']));
$raid['end_time'] = date("Y-M-d H:i:s",preg_replace("/\D/","",$_GET['end_time']));
}
if(in_array($_GET['pokemon_form'], ['-1','-2','-3'])) {
$raid['pokemon_form'] = $_GET['pokemon_form'];
}else {
$raid['pokemon_form'] = preg_replace("/\D/","",$_GET['pokemon_form']);
}
$raid['costume'] = 0;
if(array_key_exists('costume', $_GET) && $_GET['costume'] != '') {
$raid['costume'] = preg_replace("/\D/","",$_GET['costume']);
}
$q_pokemon_info = my_query("
SELECT
pokemon_form_name, min_cp, max_cp, min_weather_cp, max_weather_cp, weather, shiny, asset_suffix, type, type2
FROM pokemon
WHERE pokedex_id = '".$raid['pokemon']."'
AND pokemon_form_id = '".$raid['pokemon_form']."' LIMIT 1")->fetch();
$q_gym_info = my_query("SELECT img_url, gym_name, ex_gym FROM gyms WHERE id='".$raid['gym_id']."'")->fetch();
$raid = array_merge($raid, $q_pokemon_info, $q_gym_info);
// Fonts
$font_gym = FONTS_PATH . '/' . $config->RAID_PICTURE_FONT_GYM;
$font_text = FONTS_PATH . '/' . $config->RAID_PICTURE_FONT_TEXT;
$font_ex_gym = FONTS_PATH . '/' . $config->RAID_PICTURE_FONT_EX_GYM;
// Canvas size
$canvas_width = 700;
$canvas_height = 356;
// Creating an empty canvas
$canvas = imagecreatetruecolor($canvas_width,$canvas_height);
imagesavealpha($canvas,true);
// Background color
// Default: White
$bg_rgb = [255,255,255];
$config_bg_color = explode(',',$config->RAID_PICTURE_BG_COLOR);
if(count($config_bg_color) == 3) {
$bg_rgb = $config_bg_color;
} else {
info_log($config->RAID_PICTURE_BG_COLOR, 'Invalid value RAID_PICTURE_BG_COLOR:');
}
$bg_color = imagecolorallocate($canvas,$bg_rgb[0],$bg_rgb[1], $bg_rgb[2]);
imagefill($canvas, 0, 0, $bg_color);
// Text / Font color
// Default: Black
$font_rgb = [0,0,0];
$config_font_color = explode(',',$config->RAID_PICTURE_TEXT_COLOR);
if(count($config_font_color) == 3) {
$font_rgb = $config_font_color;
} else {
info_log($config->RAID_PICTURE_TEXT_COLOR, 'Invalid value RAID_PICTURE_TEXT_COLOR:');
}
$font_color = imagecolorallocate($canvas,$font_rgb[0],$font_rgb[1],$font_rgb[2]);
// Defining RBG values that are used to create transparent color
// Should be different from RAID_PICTURE_BG_COLOR and RAID_PICTURE_TEXT_COLOR
$transparent_rgb = [0,255,0];
// Gym image
$gym_url = $raid['img_url'];
if($config->RAID_PICTURE_STORE_GYM_IMAGES_LOCALLY && !empty($gym_url)) {
if(substr($gym_url, 0, 7) == 'file://') {
$gym_image_path = $gym_url;
debug_log($gym_image_path, 'Found an image imported via a portal bot: ');
}else {
$file_name = explode('/', $gym_url)[3];
$gym_image_path = PORTAL_IMAGES_PATH .'/'. $file_name.'.png';
debug_log($gym_image_path, 'Attempting to use locally stored gym image');
if(!file_exists($gym_image_path)) {
debug_log($gym_url, 'Gym image not found, attempting to downloading it from: ');
if(is_writable(PORTAL_IMAGES_PATH)) {
download_Portal_Image($gym_url, PORTAL_IMAGES_PATH, $file_name . '.png');
}else {
$gym_image_path = $gym_url;
info_log(PORTAL_IMAGES_PATH, 'Failed to write new gym image, incorrect permissions in directory ');
}
}
}
}else {
$img_gym = false;
if (!empty($gym_url)) {
$gym_image_path = $gym_url;
}
}
$img_gym = grab_img($gym_image_path);
if($img_gym == false) {
info_log($gym_image_path, 'Loading the gym image failed, using default gym image');
if(is_file($config->RAID_DEFAULT_PICTURE)) {
$img_gym = grab_img($config->RAID_DEFAULT_PICTURE);
} else {
info_log($config->RAID_DEFAULT_PICTURE, 'Cannot read default gym image:');
$img_gym = grab_img(IMAGES_PATH . "/gym_default.png");
}
}
// Get the width and height of the gym picture
$gym_w = imagesx($img_gym);
$gym_h = imagesy($img_gym);
// Crop gym image
if($gym_w > $gym_h) {
$size = $gym_h;
$crop_x = floor((($gym_w/2)-($gym_h/2)));
$crop_y = 0;
} else {
$size = $gym_w;
$crop_x = 0;
$crop_y = floor((($gym_h/2)-($gym_w/2)));
}
// Create mask
$new_w = 300;
$new_h = 300;
$mask = imagecreatetruecolor($new_w,$new_h);
// Fill the mask with background color
$bg = imagecolorallocate($mask,$bg_rgb[0],$bg_rgb[1], $bg_rgb[1]);
imagefill($mask,0,0,$bg);
// Define transparent color for the mask
$transparent = imagecolorallocate($mask,$transparent_rgb[0],$transparent_rgb[1],$transparent_rgb[2]);
imagecolortransparent($mask,$transparent);
// Creating the orange circle around the gym photo
$color_ellipse = imagecolorallocate($mask,254,193,161);
imagefilledellipse($mask,$new_w/2,$new_h/2,$new_w-9,$new_h-9,$color_ellipse);
imagefilledellipse($mask,$new_w/2,$new_h/2,$new_w-16,$new_h-16,$bg);
// Creating a circle that is filled with transparent color
imagefilledellipse($mask,$new_w/2,$new_h/2,$new_w-30,$new_h-30,$transparent);
// Merging the desired part of the gym picture with canvas
imagecopyresampled($canvas,$img_gym,0,0,$crop_x,$crop_y,$new_w,$new_h, $size,$size);
// Merging the mask with a circular cutout to the canvas
imagecopymerge($canvas, $mask, 0, 0, 0, 0, $new_w, $new_h, 100);
// Is ex gym?
if($raid['ex_gym'] == 1) {
$ex_text_size = 20;
$ex_text_angle = 0;
$corner = 16; // Roundness of the corners
$extra = $ex_text_size/5+1; // Some extra height
$ex_mark_bg_color = [94,169,190];
$ex_mark_text_color = [255,255,255];
// Get the text with local translation for EX Raid gym
$ex_raid_gym_text = strtoupper(getPublicTranslation('ex_gym'));
// Finding out the size of text
$ex_text_box = imagettfbbox($ex_text_size,$ex_text_angle,$font_ex_gym,$ex_raid_gym_text);
$ex_logo_width = $ex_text_box[2]+($corner);
$ex_logo_height = $ex_text_size+$extra;
// Create the canvas for EX RAID indicator
$ex_logo = imagecreatetruecolor($ex_logo_width,$ex_logo_height);
// Defining the transparent color
$ex_transparent = imagecolorallocate($ex_logo,$transparent_rgb[0],$transparent_rgb[1],$transparent_rgb[2]);
imagecolortransparent($ex_logo,$ex_transparent);
// Defining background color
$ex_logo_bg = imagecolorallocate($mask,$ex_mark_bg_color[0],$ex_mark_bg_color[1], $ex_mark_bg_color[2]);
$ex_text_color = imagecolorallocate($ex_logo,$ex_mark_text_color[0],$ex_mark_text_color[1],$ex_mark_text_color[2]);
//Filling the canvas with transparent color
imagefill($ex_logo,0,0,$ex_transparent);
// Creating 4 balls, one in each corner
imagefilledellipse($ex_logo,$corner/2,$corner/2,$corner,$corner,$ex_logo_bg);
imagefilledellipse($ex_logo,$corner/2,$ex_logo_height-$corner/2,$corner,$corner,$ex_logo_bg);
imagefilledellipse($ex_logo,$ex_logo_width-$corner/2,$corner/2,$corner,$corner,$ex_logo_bg);
imagefilledellipse($ex_logo,$ex_logo_width-$corner/2,$ex_logo_height-$corner/2,$corner,$corner,$ex_logo_bg);
// And two rectangles to fill the rest
imagefilledrectangle($ex_logo,$corner/2,0,$ex_logo_width-($corner/2),$ex_logo_height,$ex_logo_bg);
imagefilledrectangle($ex_logo,0,$corner/2,$ex_logo_width,$ex_logo_height-($corner/2),$ex_logo_bg);
// Draw the text
imagettftext($ex_logo,$ex_text_size,$ex_text_angle,$corner/2,$ex_text_size+1,$ex_text_color,$font_ex_gym,$ex_raid_gym_text);
// Copy icon into canvas
imagecopy($canvas,$ex_logo,20,20,0,0,$ex_logo_width,$ex_logo_height);
}
$show_boss_pokemon_types = false;
// Raid running
if($raid_ongoing) {
if(strlen($raid['asset_suffix']) > 2) {
$icon_suffix = $raid['asset_suffix'];
}else {
$pad_zeroes = '';
for ($o=3-strlen($raid['pokemon']);$o>0;$o--) {
$pad_zeroes .= 0;
}
$icon_suffix = $pad_zeroes.$raid['pokemon'] . "_" . $raid['asset_suffix'];
}
// Raid Egg
if($raid['pokemon'] > 9990) {
// Getting the actual icon
$img_pokemon = grab_img(IMAGES_PATH . "/raid_eggs/pokemon_icon_" . $raid['pokemon'] . "_00.png");
// Position and size of the picture
$dst_x = $dst_y = 150;
$dst_w = $dst_h = 200;
$src_w = $src_h = 128;
//Pokemon
} else {
// Formatting the id from 1 digit to 3 digit (1 -> 001)
$pokemon_id = str_pad($raid['pokemon'], 3, '0', STR_PAD_LEFT);
// Check pokemon icon source and create image
$img_file = null;
$p_sources = explode(',', $config->RAID_PICTURE_POKEMON_ICONS);
$addressable_icon = 'pm'.$raid['pokemon'];
if($raid['pokemon_form_name'] != 'normal') $addressable_icon .= '.f'.strtoupper($raid['pokemon_form_name']);
// Getting the actual icon filename
$p_icon = "pokemon_icon_" . $icon_suffix;
if($raid['costume'] != 0) {
$p_icon .= '_' . str_pad($raid['costume'], 2, '0', STR_PAD_LEFT);
$costume = json_decode(file_get_contents(ROOT_PATH . '/protos/costume.json'), true);
$addressable_icon .= '.c' . array_search($raid['costume'],$costume);
}
if($raid['shiny'] == 1 && $config->RAID_PICTURE_SHOW_SHINY) {
$p_icon = $p_icon . "_shiny";
$addressable_icon .= '.s';
$shiny_icon = grab_img(IMAGES_PATH . "/shinystars.png");
}
$addressable_icon .= '.icon.png';
$p_icon = $p_icon . ".png";
foreach($p_sources as $p_dir) {
// Set pokemon icon dir
$p_img = IMAGES_PATH . "/pokemon_" . $p_dir . "/" . $p_icon;
$p_add_img = IMAGES_PATH . "/pokemon_" . $p_dir . "/Addressable_Assets/" . $addressable_icon;
// Icon dir named 'pokemon'? Then change path to not add '_repo-owner' to icon folder name
if($p_dir == 'pokemon') {
$p_img = IMAGES_PATH . "/pokemon/" . $p_icon;
$p_add_img = IMAGES_PATH . "/pokemon/Addressable_Assets" . $addressable_icon;
}
// Check if file exists in this collection
// Prioritize addressable asset file
if(file_exists($p_add_img) && filesize($p_add_img) > 0) {
$img_file = $p_add_img;
break;
}else if(file_exists($p_img) && filesize($p_img) > 0) {
$img_file = $p_img;
break;
}
}
// If no image was found, substitute with a fallback
if($img_file === null) {
info_log($p_icon, 'Failed to find an image in any pokemon image collection for:');
$img_fallback_file = null;
// If we know the raid level, fallback to egg image
if(array_key_exists('level', $raid) && $raid['level'] !== null && $raid['level'] != 0) {
$img_fallback_file = IMAGES_PATH . "/raid_eggs/pokemon_icon_999" . $raid['level'] . "_00.png";
} else {
info_log('Unknown raid level, using fallback icon.');
$img_fallback_file = $config->RAID_PICTURE_POKEMON_FALLBACK;
}
$img_file = $img_fallback_file;
}
$img_pokemon = grab_img($img_file);
// Position and size of the picture
$dst_x = $dst_y = 100;
$dst_w = $dst_h = $src_w = $src_h = 256;
if($raid['type'] != '') $show_boss_pokemon_types = true;
}
// Raid ended
} else {
// Raid won image
$img_pokemon = grab_img(IMAGES_PATH . "/raidwon.png");
// Position and size of the picture
$dst_x = $dst_y = 172;
$src_w = 444;
$src_h = 512;
$dst_w = 160;
$dst_h = floor($dst_w/$src_w*$src_h);
}
// Create pokemon image.
imagesavealpha($img_pokemon,true);
// Debug - Add border around pokemon image
if($debug) {
$im = imagecreate($src_w,$src_h);
$black = imagecolorallocate($im,0,0,0);
imagerectangle($img_pokemon,0,0,$src_w-1,$src_h-1,$black);
}
// Add pokemon to image
imagecopyresampled($canvas,$img_pokemon,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$src_w,$src_h);
// Add pokemon types
if($config->RAID_PICTURE_POKEMON_TYPES && $show_boss_pokemon_types) {
$img_type = grab_img(IMAGES_PATH . "/types/".$raid['type'].".png");
$type1_x = 300;
imagesavealpha($img_type, true);
if($raid['type2'] != '') {
$img_type2 = grab_img(IMAGES_PATH . "/types/".$raid['type2'].".png");
imagesavealpha($img_type2, true);
imagecopyresampled($canvas,$img_type2,300,300,0,0,40,40,64,64);
$type1_x -= 50;
}
imagecopyresampled($canvas,$img_type,$type1_x,300,0,0,40,40,64,64);
}
if(isset($shiny_icon)) {
imagesavealpha($shiny_icon,true);
$light_white = imagecolorallocatealpha($canvas, 255,255,255,50);
imagefilledellipse($canvas, $type1_x-35 ,320,40,40,$light_white);
imagecopyresampled($canvas,$shiny_icon,$type1_x-52,301,0,0,35,35,100,100);
}
// Ex-Raid?
if($_GET['ex_raid'] == '1') {
$img_expass = grab_img(IMAGES_PATH . "/expass.png");
imagesavealpha($img_expass,true);
// Debug - Add border around expass image
if($debug) {
$im = imagecreate(256,256);
$black = imagecolorallocate($im,0,0,0);
imagerectangle($img_expass,0,0,255,255,$black);
}
imagecopyresampled($canvas,$img_expass,0,225,0,0,100,100,256,256);
}
// Adding the gym name to the image
$text_size = 23; // Font size of additional text
$text_size_cp_weather = 20;// Font size of weather cp text
$left_after_poke = 356; // First left position behind the pokemon icon.
$angle = 0; // Angle of the text
$spacing = 10; // Spacing between lines
$spacing_right = 10; // Empty space on the right for weather icons and CP text
// Gym name
// Largest gym name we found so far for testing:
//$gym_name = 'Zentrum für Junge Erwachsene der Kirche Jesu Christi der Heiligen der Letzten Tage Pfahl Düsseldorf';
$gym_name = $raid['gym_name'];
// Get length, the shortest and largest word of the gym name
$gym_name_words = explode(SP, $gym_name);
$gym_name_word_lengths = array_map('strlen', array_map('utf8_decode', $gym_name_words));
$gym_name_word_largest = max($gym_name_word_lengths);
$gym_name_word_shortest = min($gym_name_word_lengths);
$gym_name_total_chars = strlen(utf8_decode($gym_name));
// Number of rows based on number of words or total chars
$gym_name_rows = 1;
if(count($gym_name_words) > 1 && $gym_name_total_chars >= 18 && $gym_name_total_chars <= 50) {
$gym_name_rows = 2;
} else if($gym_name_total_chars > 50) {
$gym_name_rows = 3;
}
// Wrap gym name to multiple lines if too long
$gym_name_lines = explode(PHP_EOL,wordwrap(trim($gym_name),floor(($gym_name_total_chars+$gym_name_word_largest)/$gym_name_rows),PHP_EOL));
debug_log($gym_name_total_chars, 'Gym name length:');
debug_log($gym_name_lines, 'Gym name lines:');
// Target width and height
$targetWidth = imagesx($canvas) - imagesx($mask) - $spacing_right;
$targetHeight = 95;
$targetHeight = $targetHeight/$gym_name_rows;
// Get largest possible fontsize for each gym name line
for($l=0; $l<count($gym_name_lines); $l++) {
for($s=1; $s<70/count($gym_name_lines); $s=$s+0.5){
$box = imagettfbbox($s, 0, $font_gym, $gym_name_lines[$l]);
$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
$max_y = max(array($box[1], $box[3], $box[5], $box[7]));
$width = ($max_x - $min_x);
$height = ($max_y - $min_y);
$targetsize = $s;
// Exit once we exceed width or height
if($width >= $targetWidth || $height >= $targetHeight){
break;
}
}
// Gym name font size and spacing
if($l == 0 || $targetsize < $fontsize_gym) {
$fontsize_gym = $targetsize;
$spacing_gym = $height * 0.30;
}
}
// Add gym name to image
for($y=0;$y<count($gym_name_lines);$y++){
// Get box around text
$box = imagettfbbox($fontsize_gym,$angle,$font_gym,$gym_name_lines[$y]);
// Get min and max positions for x and y
$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
$max_y = max(array($box[1], $box[3], $box[5], $box[7]));
// Get text width and height
$textwidth = ($max_x - $min_x);
$textheight = floor($fontsize_gym*1.1);
// Calculate distance from left and top for positioning the gym name text.
$gym_name_top = floor((($y+1)*($textheight))+($y*$spacing_gym));
$gym_name_left = floor(imagesx($mask) + (((imagesx($canvas) - imagesx($mask) - $spacing_right) - $textwidth)/2));
imagettftext($canvas, $fontsize_gym, $angle, $gym_name_left, $gym_name_top, $font_color, $font_gym, $gym_name_lines[$y]);
}
// Raid times
if($raid_ongoing) {
$time_text = get_raid_times($raid, true, true);
} else {
$time_text = getPublicTranslation('raid_done');
}
// Adjust margins, font size and raid time text itself
$time_text_lines = array();
if(strpos($time_text, ',') !== false) {
$time_text_lines[] .= explode(',', $time_text)[0] . ',';
// Thursday, 18:00 - 18:45
if(count(explode(SP, explode(',', $time_text, 2)[1])) == 4) {
$time_top = 150;
$time_text_size = 35;
$tmp_time_text_line = explode(SP, explode(',', $time_text)[1]);
$time_text_lines[] .= $tmp_time_text_line[0] . SP . $tmp_time_text_line[1] . SP . $tmp_time_text_line[2] . SP . $tmp_time_text_line[3];
// Thursday, 12. December 18:00 - 18:45
} else {
$time_top = 140;
$time_text_size = 30;
$tmp_time_text_line = explode(SP, explode(',', $time_text)[1]);
$time_text_lines[] .= $tmp_time_text_line[0] . SP . $tmp_time_text_line[1] . SP . $tmp_time_text_line[2];
$time_text_lines[] .= $tmp_time_text_line[3] . SP . $tmp_time_text_line[4] . SP . $tmp_time_text_line[5];
}
} else {
// 18:00 - 18:45 or raid ended text.
$time_text_size = 40;
$time_top = 175;
$time_text_lines[] .= $time_text;
}
$num_text_lines = count($time_text_lines);
// If the photo is sent without caption, we want to keep the bottom right corcer clear of text because Telegram covers it with a timestamp
if($standalone_photo) $time_top -= 10;
// Go through every line...
for($ya=0;$ya<$num_text_lines;$ya++){
// ...and draw them to image
$time_text_top = ($time_top+($ya*($time_text_size+$spacing)));
// Align text to center between pokemon icon and right edge
$box = imagettfbbox($time_text_size, $angle, $font_text, $time_text_lines[$ya]);
$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
$textwidth = ($max_x - $min_x);
$time_left = $left_after_poke + floor((((imagesx($canvas) - $left_after_poke - $spacing_right) - $textwidth)/2));
imagettftext($canvas,$time_text_size,$angle,$time_left,$time_text_top,$font_color,$font_text,$time_text_lines[$ya]);
}
// Pokemon raid boss
$pokemon_name = get_local_pokemon_name($raid['pokemon'], $raid['pokemon_form'], true);
// Pokemon name and form?
$pokemon_text_lines = array($pokemon_name);
if(strlen($pokemon_name) > 20) {
$pokemon_text_lines = explode(SP,$pokemon_name);
if(count($pokemon_text_lines) == 1) {
// Wrapping the time text if too long (to 20 letters)
$pokemon_text_lines = explode(PHP_EOL,wordwrap(trim($pokemon_name),20,PHP_EOL));
}
}
$num_pokemon_lines = count($pokemon_text_lines);
// Target width and height
$targetWidth = imagesx($canvas) - $left_after_poke - (strlen($raid['weather']) * 42) - $spacing_right;
$targetHeight = 80;
// Get largest possible fontsize for each pokemon name and form line
for($p=0; $p<($num_pokemon_lines); $p++) {
for($s=1; $s<40; $s=$s+0.5){
$box = imagettfbbox($s, 0, $font_text, $pokemon_text_lines[$p]);
$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
$max_y = max(array($box[1], $box[3], $box[5], $box[7]));
$width = ($max_x - $min_x);
$height = ($max_y - $min_y);
$targetsize = $s;
// Exit once we exceed width or height
if($width >= $targetWidth || $height >= $targetHeight){
break;
}
}
// Gym name font size and spacing
if($p == 0 || $targetsize < $fontsize_poke) {
$fontsize_poke = $targetsize;
}
}
// Pokemon name (and form) in 1 row
$poke_text_top = 310;
// Pokemon name and form in one or two lines?
if($num_pokemon_lines > 1) {
$poke_text_top = 272;
}
// If the photo is sent without caption, we want to keep the bottom right corcer clear of text because Telegram covers it with a timestamp
if($standalone_photo) $poke_text_top -= 50;
// Add pokemon name to image
for($pa=0;$pa<$num_pokemon_lines;$pa++){
// Get text width and height
$textwidth = ($max_x - $min_x);
$textheight = ($max_y - $min_y);
// Position from top
$poke_text_top = ($poke_text_top+($pa*($fontsize_poke+$spacing)));
imagettftext($canvas,$fontsize_poke,$angle,$left_after_poke,$poke_text_top,$font_color,$font_text,$pokemon_text_lines[$pa]);
}
// Pokemon CP
if($raid['pokemon'] < 9990) {
$cp_text_top = $poke_text_top+$text_size+$spacing;
$cp_text = $raid['min_cp']." - ".$raid['max_cp'];
$cp_text2 = "(".$raid['min_weather_cp']."-".$raid['max_weather_cp'].")";
imagettftext($canvas,$text_size,$angle,$left_after_poke,$cp_text_top,$font_color,$font_text,$cp_text);
$cp_weather_text_box = imagettfbbox($text_size_cp_weather,$angle,$font_text,$cp_text2);
imagettftext($canvas,$text_size_cp_weather,$angle,($canvas_width-$cp_weather_text_box[2]-$spacing_right),$cp_text_top,$font_color,$font_text,$cp_text2);
$count_weather = strlen($raid['weather']);
for($i=0;$i<$count_weather;$i++) {
$we = substr($raid['weather'],$i,1);
$weather_icon_path = IMAGES_PATH . "/weather/";
// Use white icons?
if($config->RAID_PICTURE_ICONS_WHITE) {
$weather_icon_path = IMAGES_PATH . "/weather_white/";
}
$weather_icon = grab_img($weather_icon_path . $we . ".png"); // 64x64
imagecopyresampled($canvas,$weather_icon,$canvas_width-$spacing_right-($count_weather-$i)*40,$poke_text_top-30,0,0,38,38,64,64);
}
}
// Define and print picture
// PNG
if($config->RAID_PICTURE_FILE_FORMAT == 'png') {
header("Content-type: image/png");
imagepng($canvas);
// JPEG
} else if($config->RAID_PICTURE_FILE_FORMAT == 'jpeg' || $config->RAID_PICTURE_FILE_FORMAT == 'jpg') {
header("Content-type: image/jpeg");
imagejpeg($canvas, NULL, 90);
// Use GIF as default - smallest file size without compression
} else {
header("Content-type: image/gif");
imagegif($canvas);
}
?>