0

我正在尝试加快我网站的各个方面。

该网站是一个购物网站,各种搜索页面都很慢。这有几个潜在的原因,一个是使用图像。

前几天晚上,我创建了一个函数,为我的 Facebook OG 标签生成缩略图。该函数向服务器写入一个 400px x 400px 的方形图像,该图像是通过平铺搜索查询生成的前几个(最多 14 个)产品图像而创建的。

在一个文件上完全隔离运行这个脚本test.aspx给了我合理的加载时间,但我不能做任何事情来加快它,即使我没有在浏览器中返回任何东西,只是执行这个过程大约需要 3 秒,这显然扩大了在整个实时站点上放慢速度。

整个代码是:

Function mergeImgs(idList, imgList, head, fileName) As String

    Try
        Dim maxRows = HttpContext.Current.Request.QueryString("r")
        Dim imgDim = 400
        Dim Image3 As New Bitmap(imgDim, imgDim)
        Dim g As Graphics = Graphics.FromImage(Image3)

        Dim i = 0
        Dim left = 0
        Dim rows = 0
        For Each item In idList
            Dim img = Common.getImageUrl(idList(i), imgList(i), "server", "-tb")
            i += 1
            If img <> "/Images/awaiting.png" Then

                Dim imageData As System.Drawing.Image = System.Drawing.Image.FromFile(img)
                Dim aspect As Double = imageData.Width / imageData.Height ' determine aspect and decide how to display image

                Dim Image1
                Dim fixedHeight = imgDim / maxRows
                Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(img)

                Dim aspectRatio = objGraphic.Height / objGraphic.Width
                Dim reduction = fixedHeight / objGraphic.Height
                Dim newwidth = objGraphic.Width * reduction
                Image1 = New Bitmap(objGraphic, objGraphic.Width * reduction, fixedHeight)

                g.DrawImage(Image1, New Point(left, fixedHeight * rows))
                If left >= imgDim Then
                    rows += 1
                    left = 0
                ElseIf left < imgDim Then
                    left += newwidth
                ElseIf left < imgDim AndAlso rows = maxRows Then
                    Exit For
                End If
            End If
        Next

        Image3.Save(HttpContext.Current.Server.MapPath("/" & fileName), System.Drawing.Imaging.ImageFormat.Jpeg)

        Return rootUrl & fileName

        g.Dispose()
        g = Nothing

    Catch ex As Exception
        Return ("<P>" & ex.ToString)
    End Try
End Function

有一个外部函数getImageUrl,它只是一系列逻辑,它根据项目 ID 写出一个目录结构,这很快,所以我怀疑它是否能坚持下去。

传递给函数的变量是:

idList = a generic List(of String) ' a list of item IDs
imgList = a generic List(of String) ' a list of image names (image names only stored in DB)
head = the Page.Header ' not actually needed in this prototype, but aimed to allow this to write the OG tag to the Page Header
fileName = simply the name to give the generated image file

我不禁想到,从

Dim imageData As System.Drawing.Image = System.Drawing.Image.FromFile(img)

以后可能会加快速度。这样做是计算加载图像的纵横比,然后重新计算大小,使它们都具有相同的高度,这样图块就可以整齐地排列在整齐的行中。

Dim img = Common.getImageUrl(idList(i), imgList(i), "server", "-tb")

此行加载相关图像的缩略图版本(最后一个变量中的“-tb”),因此所有正在处理的图像大约为 50x50px,因此加载和确定宽高比非常小

我的服务器上存储了每个图像的 4 个版本,有 4 种不同的尺寸,以便在网站上快速显示,在这里加载的不同图像弄乱了,似乎对脚本加载时间几乎没有影响。

有什么比System.Drawing.Image.FromFile(img)我可以用来加载图像并确定图像的宽度和高度更快的方法吗?

在我的网站上还有其他几种情况,我需要确定图像的尺寸,然后对其进行处理,但似乎都有些慢。

欢迎任何建议!

仅供参考,这是由上述代码生成的图像示例

http://www.hgtrs.com/recents.jpg

这是从此页面返回的产品生成的(可能加载缓慢!):

http://www.hgtrs.com/search.aspx?Type=recents

我应该说我的test.aspx文件还包括对数据库的调用,在测试了直接使用的查询后,我得到了 0.18 秒的查询时间,我不知道这个时间如何扩展到真正的应用程序中。我已经尽我所知优化了这个查询和表(它使用一个嵌套查询和一个连接)。

4

0 回答 0