-
Notifications
You must be signed in to change notification settings - Fork 1
/
Print Sub EN.cs
335 lines (294 loc) · 11.5 KB
/
Print Sub EN.cs
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
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Net;
using System.Threading;
using System.Drawing.Drawing2D;
//for discord option
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public class Globals
{
public static String ts = DateTime.Now.ToString("yyyyMMddHHmmssfff");
public static System.Random rand = new Random();
public static String myTempFile = Path.Combine(Path.GetTempPath(), "lastSub_"+ rand.Next().ToString() + ts + ".jpg");
}
public class CPHInline
{
private Font printFont;
public bool Execute()
{
String timestamp = DateTime.Now.ToString();
String subtype = args["__source"].ToString();
String msg = "";
String title ="";
String user = args["user"].ToString();
String tier = args["tier"].ToString();
String[] line = new String[10]; //10 lines are going to be used in total
System.Drawing.Font[] pfont = new System.Drawing.Font[10]; //
String total="";
String cumulative="";
String monthStreak="";
String profilepic = args["targetUserProfileImageUrl"].ToString();
int extra =0;
int cut = 0;
//DOWNLOADING PROFILEPIC
WebRequest webreq = WebRequest.Create(profilepic);
WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
Image photo = Image.FromStream(stream);
stream.Close();
//Customizing for each type of sub
switch(subtype)
{
case "TwitchSub":
title = "SUBSCRIPTION";
photo = resizeImage(photo, new Size(200, 200));
extra = 40;
cut = 0;
break;
case "TwitchReSub":
title = "RE-SUBSCRIPTION";
//Check for plurals
if(Convert.ToInt32(args["cumulative"])>1)
{
cumulative = args["cumulative"].ToString() + " months";
}else{
cumulative = args["cumulative"].ToString() + " month";
}
if(Convert.ToInt32(args["monthStreak"])>1)
{
monthStreak = " - " + args["monthStreak"].ToString() + " month streak";
}
line[5] = "Subscribed for " + cumulative + monthStreak;
photo = resizeImage(photo, new Size(200, 200));
extra = 40;
cut = 0;
break;
case "TwitchGiftSub":
title = "GIFTED SUBSCRIPTION";
if((int)args["totalSubsGifted"]>0){
total = " (Total of: " + args["totalSubsGifted"].ToString() + ")";
}
line[3] = "GIFTER" + total;
line[5] = "RECIPIENT";
line[6] = args["recipientUser"].ToString();
if(Convert.ToInt32(args["monthsGifted"])>1){
line[7] = args["monthsGifted"].ToString() + " Months";
}else{
line[7] = args["monthsGifted"].ToString() + " Month";
}
photo = resizeImage(photo, new Size(145, 145));
extra = 25;
cut = 30;
break;
case "TwitchGiftBomb":
title = "BOMB OF SUBSCRIPTIONS";
if((int)args["totalGifts"]>0){
total = " (Total of: " + args["totalGifts"].ToString()+ ")";
}
line[3] = "GIFTER" + total;
if(Convert.ToInt32(args["gifts"])>1){
line[5] = "Bomb of " + args["gifts"].ToString() + " subscriptions";
}else{
line[5] = "Bomb of " + args["gifts"].ToString() + " subscription";
}
photo = resizeImage(photo, new Size(165, 165));
extra = 40;
cut = 20;
break;
}
line[0] = title;
line[1] = timestamp;
line[2] = "profilepic";
line[4] = user;
//Comentar la siguiente linea si no se quieres traducir
//tier = tier.Replace("tier", "Nivel");
line[8] = tier;
if (args.ContainsKey("rawInput")){
line[9] = args["rawInput"].ToString();
}
//Customizing font, size and fontstyle for each line
for (int i = 0; i < 10; i++){
if(line[i]==null){continue;}
//Font specifications for each line
//We can set the Font, size and style
if(i==0){ pfont[i] = new Font("Arial", 16,FontStyle.Bold);}
if(i==1){ pfont[i] = new Font("Arial", 7);}
if(i==3){ pfont[i] = new Font("Arial", 12,FontStyle.Bold);}
if(i==4){ pfont[i] = new Font("Arial", 20,FontStyle.Bold);}
if(i==5){ pfont[i] = new Font("Arial", 12);}
if(i==6){ pfont[i] = new Font("Arial", 12,FontStyle.Bold);}
if(i==7){ pfont[i] = new Font("Arial", 12);}
if(i==8){ pfont[i] = new Font("Arial", 12);}
if(i==9){ pfont[i] = new Font("Arial", 6);}
}
////////////CREATING IMAGE/////////////
float linesPerPage = 0;
float leftMargin = 0;
float topMargin = 0;
float pageWidth = Convert.ToSingle(args["paperWidth"]);
float pageHeight = Convert.ToSingle(args["paperHeight"]);
float yPos = topMargin;
int linespace = 2; //space between lines
//////
const int dotsPerInch = 135; // define the quality in DPI
double widthInInch = (double)pageWidth/100; // width of the bitmap in INCH
double heightInInch = (double)11; // height of the bitmap in INCH - We use 11 inches just to have a number with plenty room
////CALCULATE FINAL HEIGHT
using (Bitmap bitmapTmp = new Bitmap((int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch)))
{
bitmapTmp.SetResolution(dotsPerInch, dotsPerInch);
using (Graphics graphicsTmp = Graphics.FromImage(bitmapTmp))
{
graphicsTmp.Clear(Color.White);
pageHeight = 0; //adding some bottom margin
////CALCULATING FINAL PAGE HEIGHT NEEDED
for (int i = 0; i < 10; i++){
if(line[i]==null){continue;}
if(i==0){ printFont = pfont[i];}
if(i==1){ printFont = pfont[i];}
if(i==3){ printFont = pfont[i];}
if(i==4){ printFont = pfont[i];}
if(i==5){ printFont = pfont[i];}
if(i==6){ printFont = pfont[i];}
if(i==7){ printFont = pfont[i];}
if(i==8){ printFont = pfont[i];}
if(i==9){ printFont = pfont[i];}
if(line[i]=="profilepic"){
pageHeight = pageHeight + linespace*extra + photo.Height;
}else{
float stringWidth = graphicsTmp.MeasureString(line[i], printFont).Width+20; //adding +20 to give some margin
int stringLines = (int)Math.Ceiling(stringWidth / pageWidth );
pageHeight = pageHeight + (linespace) + printFont.GetHeight(graphicsTmp)*stringLines;
}
}
}
}
//Now pageHeight has the real height needed
pageHeight = (float)(pageHeight/1.35)-cut;
heightInInch = (double)pageHeight/100;
//CREATING FINAL IMAGE
using (Bitmap bitmap = new Bitmap((int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch)))
{
bitmap.SetResolution(dotsPerInch, dotsPerInch);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.White);
for (int i = 0; i < 10; i++){
if(line[i]==null){continue;}
if(i==0){ printFont = pfont[i];}
if(i==1){ printFont = pfont[i];}
if(i==3){ printFont = pfont[i];}
if(i==4){ printFont = pfont[i];}
if(i==5){ printFont = pfont[i];}
if(i==6){ printFont = pfont[i];}
if(i==7){ printFont = pfont[i];}
if(i==8){ printFont = pfont[i];}
if(i==9){ printFont = pfont[i];}
if(line[i]=="profilepic"){
int proPicX = ((int)((pageWidth*1.35) - photo.Width)/2)-35; //fix of -20 in X position
Point ulCorner = new Point(proPicX, (int)yPos);
// Sending to image
graphics.DrawImage(photo,ulCorner);
yPos = yPos + linespace*extra + photo.Height;
}else{
float stringWidth = graphics.MeasureString(line[i], printFont).Width+20; //adding +20 to give some margin
int stringLines = (int)Math.Ceiling(stringWidth / (pageWidth*1.35) );
// Sending to image
float ancho = Convert.ToSingle(pageWidth*1.35)-20;
graphics.DrawString(line[i], printFont, Brushes.Black, new RectangleF(10, yPos, ancho, printFont.GetHeight(graphics)*stringLines), new StringFormat() { Alignment = StringAlignment.Center });
yPos = yPos + (linespace) + printFont.GetHeight(graphics)*stringLines;
}
}
}
// Save the bitmap
bitmap.Save(Globals.myTempFile);
}
//POSTING TO DISCORD
if(args["postToDiscord"].ToString().ToLower()=="true"){
string discordWebhookURL = args["discordWebhook"].ToString();
string FileName = Globals.myTempFile;
//Taken from StreamUP Tools - OBSCord by Andilippi
string displayName = args["user"].ToString();
string userName = args["userName"].ToString();
string userProfilePicture = args["targetUserProfileImageUrl"].ToString();
string discordMessage = JsonConvert.SerializeObject(new {embeds = new[] { new { title = "New subscription!", color = 7009535, author = new { name = $"~ {displayName}", url = $"https://twitch.tv/{userName}", icon_url = userProfilePicture } } } });
// Open and Read the created file by discord and make it into a BLOB
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
byte[] imageBlob = reader.ReadBytes((int)fs.Length);
// Close the open file
fs.Close();
reader.Close();
// Creating a HttpClient so that we can send the data to discord and upload it.
HttpClient client = new HttpClient();
// Set the correct type of data to send and put in the discordMessage also
MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Add(new ByteArrayContent(imageBlob, 0, imageBlob.Length), "image", $"{FileName}");
formData.Add(new StringContent(discordMessage), "payload_json");
// Send it and after it is done dispose of client.
client.PostAsync(discordWebhookURL, formData).Wait();
client.Dispose();
}
//PRINT
if(args["print"].ToString().ToLower()=="true"){
pageHeight = pageHeight + 30;
if(Convert.ToInt32(args["paperHeight"])>0)
{
pageHeight = Convert.ToInt32(args["paperHeight"]);
}
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrinterSettings.PrinterName = args["printer"].ToString();
pd.DefaultPageSettings.PaperSize = new PaperSize("SubsPaper", Convert.ToInt32(pageWidth)+6, Convert.ToInt32(pageHeight)+6);
pd.Print();
Thread.Sleep(2000);
}
File.Delete(Globals.myTempFile);
return true;
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
using (System.Drawing.Image photo = System.Drawing.Image.FromFile(Globals.myTempFile))
{
Point ulCorner = new Point(-11,0);
//Sending to printer
ev.Graphics.DrawImage(photo, ulCorner);
}
}
//TO RESIZE PICTURES
//use:
//photo = resizeImage(photo, new Size(200, 200));
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
//Get the image current width
int sourceWidth = imgToResize.Width;
//Get the image current height
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//Calulate width with new desired size
nPercentW = ((float)size.Width / (float)sourceWidth);
//Calculate height with new desired size
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//New Width
int destWidth = (int)(sourceWidth * nPercent);
//New Height
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw image with new width and height
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
}