-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCountdownTimerGif.class.php
254 lines (207 loc) · 15.3 KB
/
CountdownTimerGif.class.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
<?php
/**
* Created by PhpStorm.
* User: Chlegou
* Date: 13/03/2018
* Time: 23:46
*/
include 'GIFEncoder.class.php';
// needed only for <= PHP2.5
include 'php52-fix.php';
//including the imagettftextblur function
include 'imagettftextblur.php';
class CountdownTimerGif
{
public static function create($imagePath, $time, $timezone, $backgroundColor, $counterColor, $counterGlowColor, $counterGlowSize, $indicatorsColor, $indicatorsGlowColor, $indicatorsGlowSize, $width, $height){
// http://php.net/manual/en/timezones.php
// if the given timezone isn't correct, we set it to default UTC timezone
if(!date_default_timezone_set($timezone)) {
date_default_timezone_set('UTC');
}
// calculate rgb from hexdec color
list($backgroundColorRed, $backgroundColorGreen, $backgroundColorBlue) = array_map('hexdec', str_split($backgroundColor, 2));
// calculate rgb from hexdec color
list($counterColorRed, $counterColorGreen, $counterColorBlue) = array_map('hexdec', str_split($counterColor, 2));
// calculate rgb from hexdec color
list($counterGlowColorRed, $counterGlowColorGreen, $counterGlowColorBlue) = array_map('hexdec', str_split($counterGlowColor, 2));
// calculate rgb from hexdec color
list($indicatorsColorRed, $indicatorsColorGreen, $indicatorsColorBlue) = array_map('hexdec', str_split($indicatorsColor, 2));
// calculate rgb from hexdec color
list($indicatorsGlowColorRed, $indicatorsGlowColorGreen, $indicatorsGlowColorBlue) = array_map('hexdec', str_split($indicatorsGlowColor, 2));
// default height and width in pixel
$imageDimensions = getimagesize($imagePath);
// Your image link
$image = CountdownTimerGif::copyTransparent($imagePath, $width, $height, $backgroundColorRed, $backgroundColorGreen, $backgroundColorBlue);
$delay = 100;// milliseconds
// when changing fonts, we need to adjust the position!!!!
$font = array(
'counter' => array(
'size' => 30, // Font size, in pts usually.
'angle' => 0, // Angle of the text
// The larger the number the further the distance from the left hand side, 0 to align to the left.
'x-offset' => array(// +73 offset
'days' => 174 - ($imageDimensions[0] - $width) / 2 ,
'hours' => 247 - ($imageDimensions[0] - $width) / 2 ,
'minutes' => 320 - ($imageDimensions[0] - $width) / 2 ,
'seconds' => 394 - ($imageDimensions[0] - $width) / 2 ,
),
'y-offset' => 150 - ($imageDimensions[1] - $height) / 2 , // The vertical alignment, trial and error between 20 and 60.
'file' => 'fonts/BEBASNEUE-REGULAR.ttf', // Font path
'color' => imagecolorallocatealpha($image, $counterColorRed, $counterColorGreen, $counterColorBlue, 0), // RGB Colour of the text
'glowColor' => imagecolorallocatealpha($image, $counterGlowColorRed, $counterGlowColorGreen, $counterGlowColorBlue, 0), // RGB Colour of the text
'glowSize' => $counterGlowSize,
),
'indicators' => array(
'size' => 10, // Font size, in pts usually.
'angle' => 0, // Angle of the text
// The larger the number the further the distance from the left hand side, 0 to align to the left.
'x-offset' => array(// +73 offset
'days' => 175 - ($imageDimensions[0] - $width) / 2 ,
'hours' => 246 - ($imageDimensions[0] - $width) / 2 ,
'minutes' => 323 - ($imageDimensions[0] - $width) / 2 ,
'seconds' => 400 - ($imageDimensions[0] - $width) / 2 ,
),
'y-offset' => 193 - ($imageDimensions[1] - $height) / 2 , // The vertical alignment, trial and error between 20 and 60.
'file' => 'fonts/BEBAS.ttf', // Font path
'color' => imagecolorallocatealpha($image, $indicatorsColorRed, $indicatorsColorGreen, $indicatorsColorBlue, 0), // RGB Colour of the text
'glowColor' => imagecolorallocatealpha($image, $indicatorsGlowColorRed, $indicatorsGlowColorGreen, $indicatorsGlowColorBlue, 0), // RGB Colour of the text
'glowSize' => $indicatorsGlowSize,
),
);
$future_date = new DateTime(date('r',strtotime($time)));
$time_now = time();
$now = new DateTime(date('r', $time_now));
$frames = array();
$delays = array();
for($i = 0; $i <= 60; $i++){
$interval = date_diff($future_date, $now);
if($future_date < $now){
// Open the first source image and add the text.
$image = CountdownTimerGif::copyTransparent($imagePath, $width, $height, $backgroundColorRed, $backgroundColorGreen, $backgroundColorBlue);
$image = CountdownTimerGif::applyIndicatorsToImage($image, $font);
// add the glow first
if ($font['counter']['glowSize'] > 0) {
// days
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['days'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], '00', $font['counter']['glowSize'] );
// hours
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['hours'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], '00', $font['counter']['glowSize'] );
// minutes
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['minutes'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], '00', $font['counter']['glowSize'] );
// seconds
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['seconds'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], '00', $font['counter']['glowSize'] );
}
// days
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['days'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], '00' );
// hours
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['hours'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], '00' );
// minutes
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['minutes'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], '00' );
// seconds
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['seconds'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], '00' );
ob_start();
imagegif($image);
$frames[]=ob_get_contents();
$delays[]=$delay;
$loops = 1;
ob_end_clean();
break;
} else {
// Open the first source image and add the text.
$image = CountdownTimerGif::copyTransparent($imagePath, $width, $height, $backgroundColorRed, $backgroundColorGreen, $backgroundColorBlue);
$image = CountdownTimerGif::applyIndicatorsToImage($image, $font);
$days = $interval->format('%a');
//add zero padding for days
if(strlen($days) == 1){
$days = '0'.$days;
}
// add the glow first
if ($font['counter']['glowSize'] > 0) {
// days
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['days'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], $days, $font['counter']['glowSize'] );
// hours
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['hours'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], $interval->format('%H'), $font['counter']['glowSize'] );
// minutes
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['minutes'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], $interval->format('%I'), $font['counter']['glowSize'] );
// seconds
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['seconds'] , $font['counter']['y-offset'] , $font['counter']['glowColor'] , $font['counter']['file'], $interval->format('%S'), $font['counter']['glowSize'] );
}
// days
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['days'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], $days );
// hours
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['hours'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], $interval->format('%H') );
// minutes
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['minutes'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], $interval->format('%I') );
// seconds
imagettftextblur ($image , $font['counter']['size'] , $font['counter']['angle'] , $font['counter']['x-offset']['seconds'] , $font['counter']['y-offset'] , $font['counter']['color'] , $font['counter']['file'], $interval->format('%S') );
ob_start();
imagegif($image);
$frames[]=ob_get_contents();
$delays[]=$delay;
$loops = 0;
ob_end_clean();
}
$now->modify('+1 second');
}
// expire this image instantly
header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
$gif = new AnimatedGif($frames,$delays,$loops);
return $gif;
}
// this function will create a centered copy from the source file image.
private static function copyTransparent($src, $newWidth, $newHeight, $red, $green, $blue)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor( $newWidth, $newHeight );
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
// alpha must be zero!!!!
$alpha_channel = imagecolorallocatealpha($im, $red, $green, $blue, 0);
/*
* UPDATE_2018-03-16:
* In a way to get real black background, we must bypass this function
* it was making it transparent in #000000 example.
* Instead, like this example, #775151, it must be used to clear the background.
*/
if(!($red === 0 && $green === 0 && $blue ===0)){
imagecolortransparent($im, $alpha_channel);
}
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, ($x - $newWidth < 0) ? ($newWidth- $x) / 2 : 0, ($y - $newHeight < 0) ? ($newHeight- $y) / 2 : 0, ($x - $newWidth >= 0) ? ($x - $newWidth) / 2 : 0, ($y - $newHeight >= 0) ? ($y - $newHeight) / 2 : 0, $x, $y);
imagesavealpha($im,true);
return $im;
}
private static function applyIndicatorsToImage($image, $font){
/** if we need textBlur or add shadow, the we need to integrate this library
* link: https://github.com/andrewgjohnson/imagettftextblur
*/
// add indicators glow to image
if ($font['indicators']['glowSize'] > 0) {
// days
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['days'] , $font['indicators']['y-offset'] , $font['indicators']['glowColor'] , $font['indicators']['file'], 'DAYS', $font['indicators']['glowSize'] );
// hours
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['hours'] , $font['indicators']['y-offset'] , $font['indicators']['glowColor'] , $font['indicators']['file'], 'HOURS', $font['indicators']['glowSize'] );
// minutes
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['minutes'] , $font['indicators']['y-offset'] , $font['indicators']['glowColor'] , $font['indicators']['file'], 'MINS', $font['indicators']['glowSize'] );
// seconds
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['seconds'] , $font['indicators']['y-offset'] , $font['indicators']['glowColor'] , $font['indicators']['file'], 'SEC', $font['indicators']['glowSize'] );
}
// add indicators to image
// days
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['days'] , $font['indicators']['y-offset'] , $font['indicators']['color'] , $font['indicators']['file'], 'DAYS' );
// hours
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['hours'] , $font['indicators']['y-offset'] , $font['indicators']['color'] , $font['indicators']['file'], 'HOURS' );
// minutes
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['minutes'] , $font['indicators']['y-offset'] , $font['indicators']['color'] , $font['indicators']['file'], 'MINS' );
// seconds
imagettftextblur ($image , $font['indicators']['size'] , $font['indicators']['angle'] , $font['indicators']['x-offset']['seconds'] , $font['indicators']['y-offset'] , $font['indicators']['color'] , $font['indicators']['file'], 'SEC' );
return $image;
}
}