forked from ventrian/News-Articles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageHandler.ashx.vb
executable file
·264 lines (201 loc) · 9.5 KB
/
ImageHandler.ashx.vb
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
'
' News Articles for DotNetNuke - http://www.dotnetnuke.com
' Copyright (c) 2002-2007
' by Ventrian ( sales@ventrian.com ) ( http://www.ventrian.com )
'
Imports DotNetNuke.Common.Utilities
Imports System.Drawing
Imports System.Drawing.Drawing2d
Imports System.Drawing.Imaging
Imports System.Web
Imports System.Web.Services
Imports Ventrian.ImageResizer
Imports System.IO
Namespace Ventrian.NewsArticles
Public Class ImageHandler
Implements System.Web.IHttpHandler
#Region " Private Members "
Private _width As Integer = ArticleConstants.DEFAULT_THUMBNAIL_WIDTH
Private _height As Integer = ArticleConstants.DEFAULT_THUMBNAIL_HEIGHT
Private _homeDirectory As String = Null.NullString
Private _fileName As String = Null.NullString
Private _quality As Boolean = False
Private _cropped As Boolean = False
#End Region
#Region " Private Methods "
Private Function GetPhotoHeight(ByVal objPhoto As Image) As Integer
Dim width As Integer
If (objPhoto.Width > _width) Then
width = _width
Else
width = objPhoto.Width
End If
Dim height As Integer = Convert.ToInt32(objPhoto.Height / (objPhoto.Width / width))
If (height > _height) Then
height = _height
width = Convert.ToInt32(objPhoto.Width / (objPhoto.Height / height))
End If
Return height
End Function
Private Function GetPhotoWidth(ByVal objPhoto As Image) As Integer
Dim width As Integer
If (objPhoto.Width > _width) Then
width = _width
Else
width = objPhoto.Width
End If
Dim height As Integer = Convert.ToInt32(objPhoto.Height / (objPhoto.Width / width))
If (height > _height) Then
height = _height
width = Convert.ToInt32(objPhoto.Width / (objPhoto.Height / height))
End If
Return width
End Function
Private Sub ReadQueryString(ByVal context As HttpContext)
If Not (context.Request("Width") Is Nothing) Then
If (IsNumeric(context.Request("Width"))) Then
_width = Convert.ToInt32(context.Request("Width"))
End If
End If
If Not (context.Request("Height") Is Nothing) Then
If (IsNumeric(context.Request("Height"))) Then
_height = Convert.ToInt32(context.Request("Height"))
End If
End If
If Not (context.Request("HomeDirectory") Is Nothing) Then
_homeDirectory = context.Server.UrlDecode(context.Request("HomeDirectory"))
End If
If Not (context.Request("FileName") Is Nothing) Then
_fileName = context.Server.UrlDecode(context.Request("FileName"))
End If
If Not (context.Request("Q") Is Nothing) Then
If (context.Request("Q") = "1") Then
_quality = True
End If
End If
If Not (context.Request("S") Is Nothing) Then
If (context.Request("S") = "1") Then
_cropped = True
End If
End If
End Sub
#End Region
#Region " Properties "
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
#End Region
#Region " Event Handlers "
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
' Set up the response settings
context.Response.ContentType = "image/jpeg"
' Caching
context.Response.Cache.SetCacheability(HttpCacheability.Public)
context.Response.Cache.SetExpires(DateTime.Now.AddDays(30))
context.Response.Cache.VaryByParams("FileName") = True
context.Response.Cache.VaryByParams("HomeDirectory") = True
context.Response.Cache.VaryByParams("Width") = True
context.Response.Cache.VaryByParams("Height") = True
context.Response.Cache.VaryByParams("s") = True
context.Response.Cache.AppendCacheExtension("max-age=86400")
ReadQueryString(context)
Dim path As String = ""
If _fileName = "placeholder-600.jpg" Then
path = "Images/placeholder-600.jpg"
Else
path = _homeDirectory & "/" & _fileName
End If
context.Items.Add("httpcompress.attemptedinstall", "true")
If Not (System.IO.File.Exists(context.Server.MapPath(path))) Then
path = path & ".resources"
If Not (System.IO.File.Exists(context.Server.MapPath(path))) Then
Return
End If
End If
If (_cropped = False) Then
Dim photo As Image = Image.FromFile(context.Server.MapPath(path))
Dim width As Integer = GetPhotoWidth(photo)
Dim height As Integer = GetPhotoHeight(photo)
photo.Dispose()
_width = width
_height = height
End If
Dim objQueryString As New NameValueCollection()
For Each key As String In context.Request.QueryString.Keys
Dim values() As String = context.Request.QueryString.GetValues(key)
For Each value As String In values
If (key.ToLower() = "width" Or key.ToLower() = "height") Then
If (key.ToLower() = "width") Then
objQueryString.Add("maxwidth", _width.ToString())
objQueryString.Add(key, _width.ToString())
End If
If (key.ToLower() = "height") Then
objQueryString.Add("maxheight", _height.ToString())
objQueryString.Add(key, _height.ToString())
End If
Else
objQueryString.Add(key, value)
End If
Next
Next
If (_cropped) Then
objQueryString.Add("crop", "auto")
End If
Dim objImage As Bitmap = ImageManager.getBestInstance().BuildImage(context.Server.MapPath(path), objQueryString, New WatermarkSettings(objQueryString))
If (path.ToLower().EndsWith("jpg")) Then
objImage.Save(context.Response.OutputStream, ImageFormat.Jpeg)
Else
If (path.ToLower().EndsWith("gif")) Then
context.Response.ContentType = "image/gif"
Dim ios As ImageOutputSettings = New ImageOutputSettings(ImageOutputSettings.GetImageFormatFromPhysicalPath(context.Server.MapPath(path)), objQueryString)
ios.SaveImage(context.Response.OutputStream, objImage)
Else
If (path.ToLower().EndsWith("png")) Then
Dim objMemoryStream As New MemoryStream()
context.Response.ContentType = "image/png"
objImage.Save(objMemoryStream, ImageFormat.Png)
objMemoryStream.WriteTo(context.Response.OutputStream)
Else
objImage.Save(context.Response.OutputStream, ImageFormat.Jpeg)
End If
End If
End If
'Dim photo As Image = Image.FromFile(context.Server.MapPath(path))
'Dim width As Integer = GetPhotoWidth(photo)
'Dim height As Integer = GetPhotoHeight(photo)
'Dim bmp As New Bitmap(width, height)
'Dim g As Graphics = Graphics.FromImage(DirectCast(bmp, Image))
'If (_quality) Then
' g.InterpolationMode = InterpolationMode.HighQualityBicubic
' g.SmoothingMode = SmoothingMode.HighQuality
' g.PixelOffsetMode = PixelOffsetMode.HighQuality
' g.CompositingQuality = CompositingQuality.HighQuality
'End If
'g.FillRectangle(Brushes.White, 0, 0, width, height)
'g.DrawImage(photo, 0, 0, width, height)
'photo.Dispose()
'If (_quality) Then
' Dim info As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
' Dim params As New EncoderParameters
' params.Param(0) = New EncoderParameter(Encoder.Quality, 90L)
' bmp.Save(context.Response.OutputStream, info(1), params)
' bmp.Dispose()
'Else
' bmp.Save(context.Response.OutputStream, Imaging.ImageFormat.Jpeg)
'End If
End Sub
Public Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim i As Integer
For i = 0 To codecs.Length - 1 Step i + 1
If codecs(i).MimeType = mimeType Then
Return codecs(i)
End If
Next
Return Nothing
End Function
#End Region
End Class
End Namespace