ImageUrlBuilder.cs
5.89 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Contentful.Core.Images
{
/// <summary>
/// Utility builder class to construct a correct image manipulation query string for a Contentful image.
/// </summary>
public class ImageUrlBuilder
{
private readonly List<KeyValuePair<string, string>> _querystringValues = new List<KeyValuePair<string, string>>();
/// <summary>
/// Creates a new instance of an ImageUrlBuilder.
/// </summary>
/// <returns>The created <see cref="ImageUrlBuilder"/>.</returns>
public static ImageUrlBuilder New()
{
return new ImageUrlBuilder();
}
/// <summary>
/// Sets the format of the image returned.
/// </summary>
/// <param name="format">The <see cref="ImageFormat"/> of the image returned.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetFormat(ImageFormat format)
{
if(format == ImageFormat.Default)
{
return this;
}
_querystringValues.Add(new KeyValuePair<string, string>("fm", format.ToString().ToLower()));
return this;
}
/// <summary>
/// Sets the quality of the jpg image returned.
/// </summary>
/// <param name="quality">The quality as a percentage between 0 and 100.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetJpgQuality(int quality)
{
_querystringValues.Add(new KeyValuePair<string, string>("q", quality.ToString()));
return this;
}
/// <summary>
/// Sets the returned jpg to be progressive.
/// </summary>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder UseProgressiveJpg()
{
_querystringValues.Add(new KeyValuePair<string, string>("fl", "progressive"));
return this;
}
/// <summary>
/// Sets the width of the returned image.
/// </summary>
/// <param name="width">The width of the image.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetWidth(int width)
{
_querystringValues.Add(new KeyValuePair<string, string>("w", width.ToString()));
return this;
}
/// <summary>
/// Sets the height of the returned image.
/// </summary>
/// <param name="height">The height of the image.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetHeight(int height)
{
_querystringValues.Add(new KeyValuePair<string, string>("h", height.ToString()));
return this;
}
/// <summary>
/// Sets how an image should be resized to fit inside the height and width specified.
/// </summary>
/// <param name="resizeBehaviour">The resizebehaviour.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetResizingBehaviour(ImageResizeBehaviour resizeBehaviour)
{
if(resizeBehaviour == ImageResizeBehaviour.Default)
{
return this;
}
_querystringValues.Add(new KeyValuePair<string, string>("fit", resizeBehaviour.ToString().ToLower()));
return this;
}
/// <summary>
/// Sets what focus area should be used when resizing.
/// </summary>
/// <param name="focusArea">The area to focus the image on.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetFocusArea(ImageFocusArea focusArea)
{
if(focusArea == ImageFocusArea.Default)
{
return this;
}
_querystringValues.Add(new KeyValuePair<string, string>("f", focusArea.ToString().ToLower()));
return this;
}
/// <summary>
/// Set the corner radius of the image.
/// </summary>
/// <param name="radius">The radius to set.</param>
/// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
public ImageUrlBuilder SetCornerRadius(int radius)
{
_querystringValues.Add(new KeyValuePair<string, string>("r", radius.ToString()));
return this;
}
/// <summary>
/// Sets the background color when using <see cref="ImageResizeBehaviour.Pad"/> or <see cref="SetCornerRadius"/>.
/// </summary>
/// <param name="rgbColor">The background color in RGB format, starts with "rgb:" or "#" e.g. rgb:000080 or #000080.</param>
/// <returns></returns>
public ImageUrlBuilder SetBackgroundColor(string rgbColor)
{
if (string.IsNullOrEmpty(rgbColor))
{
return this;
}
if (rgbColor.StartsWith("#"))
{
rgbColor = "rgb:" + rgbColor.Substring(1);
}
_querystringValues.Add(new KeyValuePair<string, string>("bg", rgbColor));
return this;
}
/// <summary>
/// Builds the query and returns the formatted querystring.
/// </summary>
/// <returns>The formatted querystring.</returns>
public string Build()
{
var sb = new StringBuilder();
var hasQuery = false;
foreach (var parameter in _querystringValues)
{
sb.Append(hasQuery ? '&' : '?');
sb.Append(parameter.Key);
sb.Append('=');
sb.Append(parameter.Value);
hasQuery = true;
}
return sb.ToString();
}
}
}