-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageOptions.cs
161 lines (137 loc) · 6.41 KB
/
ImageOptions.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HtmlExporterPlugin
{
public class ImageOptions
{
public string ImageMagickLocation { get; set; } = String.Empty;
public bool ConvertToJpg { get; set; } = false;
public bool ConvertToPng { get; set; } = false;
public string JpgQuality { get; set; } = "85";
public bool ResizeCoverImage { get; set; } = false;
public string CoverImageWidth { get; set; } = "180";
public string CoverImageHeight { get; set; } = "270";
public bool ResizeBackgroundImage { get; set; } = false;
public string BackgroundImageWidth { get; set; } = "1280";
public string BackgroundImageHeight { get; set; } = "720";
public bool ResizeIconImage { get; set; } = false;
public string IconImageWidth { get; set; } = "48";
public string IconImageHeight { get; set; } = "48";
public bool ForceConversion { get; set; } = false;
public string MaxTasks { get; set; } = "1";
public bool AlwaysProcess { get; set; } = false;
public bool DetectDuplicates { get; set; } = false;
private bool ImageMagickFound { get; set; } = false;
public string GetUniqueString(bool includeMaxTasks = false, bool includeAlwaysProcess = false)
{
return ConvertToJpg.ToString() + '_' + ConvertToPng.ToString() + '_' + JpgQuality.ToString() + '_' +
ResizeCoverImage.ToString() + '_' + CoverImageWidth + '_' + CoverImageHeight + '_' +
ResizeBackgroundImage.ToString() + '_' + BackgroundImageWidth + '_' + BackgroundImageHeight + '_' +
ResizeIconImage.ToString() + '_' + IconImageWidth + '_' + IconImageHeight + '_' +
ForceConversion.ToString() + '_' + DetectDuplicates.ToString() + (includeMaxTasks ? '_' + MaxTasks.ToString() : String.Empty) +
(includeAlwaysProcess ? '_' + AlwaysProcess.ToString() : String.Empty);
}
public ImageOptions()
{
CheckForImageMagick();
}
public bool CheckForImageMagick()
{
ImageMagickFound = !String.IsNullOrEmpty(ImageMagickLocation) && File.Exists(ImageMagickLocation);
return ImageMagickFound;
}
public bool BackgroundNeedsConversion(string filename)
{
return ImageMagickFound && (ConvertToJpg && (!Path.GetExtension(filename).ToLower().Equals(".jpg") || ForceConversion) || ResizeBackgroundImage);
}
public bool IconNeedsConversion(string filename)
{
return ImageMagickFound && (ConvertToPng && (!Path.GetExtension(filename).ToLower().Equals(".png") || ForceConversion) || ResizeIconImage);
}
public bool CoverNeedsConversion(string filename)
{
return ImageMagickFound && (ConvertToJpg && (!Path.GetExtension(filename).ToLower().Equals(".jpg") || ForceConversion) || ResizeCoverImage);
}
public string CoverDestFilename(string filename)
{
if (!ConvertToJpg || !ImageMagickFound)
{
return filename;
}
else
{
return Path.ChangeExtension(filename, ".jpg");
}
}
public string BackgroundDestFilename(string filename)
{
if (!ConvertToJpg || !ImageMagickFound)
{
return filename;
}
else
{
return Path.ChangeExtension(filename, ".jpg");
}
}
public string IconDestFilename(string filename)
{
if (!ConvertToPng || !ImageMagickFound)
{
return filename;
}
else
{
return Path.ChangeExtension(filename, ".png");
}
}
private string MakeOptions(string SourceFileExt, string DestFileExt, bool NeedsResize, string Width, string Height, bool ConvertToJpg, bool ConvertToPng, string JpegQuality)
{
string Result = string.Empty;
string SourceExt = SourceFileExt.ToLower();
//only icon converts to png but it seems gif files have same problem and playnite also allows ico and gif to be set for background and cover.
//This can still fail if the files got no extensions like some files do in playnite
//gif and tiff needs to take 1st frame and this is handled elsewhere
if ((ConvertToPng || SourceExt.Equals(".ico")) && !SourceExt.Equals(".gif") && !SourceExt.Equals(".tiff"))
{
//converts multiple images inside an ico to a single image otherwise imagemagick generates multiple files
//credits https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=164113#p164113
Result = "( -clone 0--1 -layers Merge ) -channel A -evaluate Multiply \"%[fx: w == u[-1].w ? 1 : 0]\" +channel +delete -background None -layers merge ";
}
if (DestFileExt.ToLower().Equals(".jpg"))
{
if (!string.IsNullOrEmpty(Result))
{
Result += " ";
}
Result += "-interlace plane -quality " + JpegQuality;
}
//> only larger images to resize
if (NeedsResize)
{
if (!string.IsNullOrEmpty(Result))
{
Result += " ";
}
Result += "-resize " + Width + "x" + Height + ">";
}
return Result;
}
public string BackgroundOptions(string SourceFileExt, string DestFileExt)
{
return MakeOptions(SourceFileExt, DestFileExt, ResizeBackgroundImage, BackgroundImageWidth, BackgroundImageHeight, ConvertToJpg, false, JpgQuality);
}
public string CoverOptions(string SourceFileExt, string DestFileExt)
{
return MakeOptions(SourceFileExt, DestFileExt, ResizeCoverImage, CoverImageWidth, CoverImageHeight, ConvertToJpg, false, JpgQuality);
}
public string IconOptions(string SourceFileExt, string DestFileExt)
{
return MakeOptions(SourceFileExt, DestFileExt, ResizeIconImage, IconImageWidth, IconImageHeight, false, ConvertToPng, JpgQuality);
}
}
}