【问题标题】:Optimising Images for Web Application为 Web 应用程序优化图像
【发布时间】:2014-05-22 12:10:14
【问题描述】:

http://themeforest.nethttp://photodune.net/https://www.flickr.com 等网站和其他成像网站出现以优化其图像。例如,Photodune 在用户点击产品之前将要出售的图像显示为缩略图,从而将您带到主销售页面。然后您可以选择购买超小、小、中等。这也适用于 Flickr,您可以选择查看同一张照片的不同分辨率。所以,我的问题是:

当 Web 应用程序用户上传图像时,Web 应用程序是否会自动优化同一图像的多个分辨率(例如,用于在提要中显示的缩略图和用于其自己的博客文章的大版本)。如果确实如此,您将如何做到这一点? (代码示例会很棒)它是显示不同尺寸图像的流行方法吗?

【问题讨论】:

  • 还会有其他工具,但不妨看看imagemagick.org
  • 我认为你需要考虑你在优化什么?对用户的响应?服务器上的数据量?服务器上的文件数 - 例如,某些托管软件包仅允许 200,000 个文件。
  • @MarkSetchell 这就是我要问的,什么是最好的?它是如何完成的?

标签: image optimization


【解决方案1】:

几年前我在 VB 中编写了一个 ASPX 脚本来调整图像大小。原始图像文件存储在服务器上。当您请求图像时,您指定您想要的大小,图像在服务器上重新调整大小,然后发送到客户端。

因此,只要一张图片,我就可以请求任何我想要的尺寸。我不确定 Flickr 和其他人是否这样做,但它适用于分配非常少量空间的主机上的低流量站点。

这段代码很旧,可能不是最好的方法,但它可以工作。

' Open the file specified in the request.
Dim sSrc As String = Request.QueryString("SRC")
Dim oImg As System.Drawing.Image
oImg = System.Drawing.Image.FromFile(Server.MapPath(sSrc))

' Get the requested width.
Dim iWidth As Integer
iWidth = Request.QueryString("W")

' Get the requested height.
Dim iHeight As Integer
iHeight = Request.QueryString("H")

' Calculate the aspect ratio if either H or W are zero.
If iWidth > 0 And iHeight = 0 Then
    iHeight = oImg.Height / (oImg.Width / iWidth)
End If
If iWidth = 0 And iHeight > 0 Then
    iWidth = oImg.Width / (oImg.Height / iHeight)
End If

' Resize the image.
Dim iQuality As Integer = 100
Dim oThumb As New System.Drawing.Bitmap(iWidth, iHeight)
Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oThumb)
oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
oGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
oGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
oGraphics.DrawImage(oImg, 0, 0, iWidth, iHeight)

' Load the JPEG encoder and set the quality parameter.
Dim oEncoder As System.Drawing.Imaging.ImageCodecInfo
oEncoder = GetImageEncoder("JPEG")
Dim oParameters As New System.Drawing.Imaging.EncoderParameters(1)
Dim oParameter As New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, iQuality)
oParameters.Param(0) = oParameter

' Save the new image to the response stream.
Response.Clear
Response.ContentType = oEncoder.MimeType
oThumb.Save(Response.OutputStream, oEncoder, oParameters)

' Release the resources used by the image objects.
oParameter.Dispose()
oParameters.Dispose()
oGraphics.Dispose()
oThumb.Dispose()
oImg.Dispose()

Private Shared Function GetImageEncoder(ByVal FormatDescription As String) As System.Drawing.Imaging.ImageCodecInfo
    Dim oEncoders() As System.Drawing.Imaging.ImageCodecInfo
    Dim oEncoder As System.Drawing.Imaging.ImageCodecInfo
    oEncoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
    For Each oEncoder in oEncoders
        If oEncoder.FormatDescription = FormatDescription Then
            Return oEncoder
        End If
    Next
    Return Nothing
End Function

这没有任何错误处理,我只是将有用的位复制到其中。另外,这只是从服务器请求图像,它不会将图像放在服务器上。

【讨论】:

  • 这给了我很大的洞察力:) 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
  • 2017-12-30
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 2011-05-19
  • 2022-10-01
相关资源
最近更新 更多