-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
100 lines (94 loc) · 3.92 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShortPixel
{
class Program
{
private static String API_KEY = "__YOUR_API_KEY_HERE_";
static void Main(string[] args)
{
TestShortPixelReducer();
TestShortPixelReducerWebP();
TestShortPixelPostReducer();
TestShortPixelPostReducerWebP_AVIF();
}
private static void TestShortPixelReducer()
{
ShortPixelLib.ShortPixel shortPixel = new ShortPixelLib.ShortPixel();
ShortPixelLib.ShortPixelOptions shortPixelOptions = new ShortPixelLib.ShortPixelOptions() {
key = API_KEY,
resize_width = 800,
resize_height = 600
};
List<string> urls = new List<string>() { "https://shortpixel.com/img/shortpixel-on-wp.png",
"https://shortpixel.com/img/shortpixel-ai.png" };
shortPixelOptions.urllist = urls;
List<string> filepaths = new List<string>() { @"c:\temp\file1.jpg", @"c:\temp\file2.jpg" };
shortPixel.Reducer(shortPixelOptions, filepaths);
}
private static void TestShortPixelReducerWebP()
{
ShortPixelLib.ShortPixel shortPixel = new ShortPixelLib.ShortPixel();
ShortPixelLib.ShortPixelOptions shortPixelOptions = new ShortPixelLib.ShortPixelOptions() {
key = API_KEY,
convertto = "+webp",
resize = "1",
resize_width = 800,
resize_height = 600
};
List<string> urls = new List<string>() { "https://shortpixel.com/img/shortpixel-on-wp.png",
"https://shortpixel.com/img/shortpixel-ai.png" };
shortPixelOptions.urllist = urls;
List<string> filepaths = new List<string>() { @"c:\temp\file1w.jpg", @"c:\temp\file2w.jpg" };
shortPixel.Reducer(shortPixelOptions, filepaths);
}
private static void TestShortPixelPostReducer()
{
ShortPixelLib.ShortPixel shortPixel = new ShortPixelLib.ShortPixel();
ShortPixelLib.ShortPixelOptions shortPixelOptions = new ShortPixelLib.ShortPixelOptions() {
key = API_KEY,
resize = "1",
resize_width = 800,
resize_height = 600
};
string fileName = "file3.jpg";
using (Stream uploadStream = File.OpenRead(@"c:\temp\file3.jpg"))
{
ShortPixelLib.ShortPixelResult res = shortPixel.PostReducer(shortPixelOptions, uploadStream, fileName);
SaveTo(res.Optimized, @"c:\temp\file3opt.jpg");
}
}
private static void TestShortPixelPostReducerWebP_AVIF()
{
ShortPixelLib.ShortPixel shortPixel = new ShortPixelLib.ShortPixel();
ShortPixelLib.ShortPixelOptions shortPixelOptions = new ShortPixelLib.ShortPixelOptions() {
key = API_KEY,
lossy = "0",
convertto = "+webp|+avif"
};
string fileName = "file4.jpg";
using (Stream uploadStream = File.OpenRead(@"c:\temp\file4.jpg"))
{
ShortPixelLib.ShortPixelResult res = shortPixel.PostReducer(shortPixelOptions, uploadStream, fileName);
SaveTo(res.Optimized, @"c:\temp\file4opt.jpg");
SaveTo(res.OptimizedWebP, @"c:\temp\file4opt.webp");
SaveTo(res.OptimizedAVIF, @"c:\temp\file4opt.avif");
}
}
private static void SaveTo(Stream from, String toPath)
{
if (from != null)
{
using (Stream to = File.Create(toPath))
{
from.Seek(0, SeekOrigin.Begin);
from.CopyTo(to);
}
}
}
}
}