Esempio n. 1
0
        private void Check(LoadTestDatabase db, CheckType type, UriInfo info, ResultData request)
        {
            switch (type)
            {
            case CheckType.Css:
            case CheckType.JS:
                String value    = request.ContentEncoding2.GetString(request.Data);
                String min      = minify(value);
                String expanded = expand(value);

                double percentMin = 100F - (((double)min.Length / (double)value.Length) * 100F);
                double percentExp = 100F - (((double)value.Length / (double)expanded.Length) * 100F);

                if (percentMin > 10)
                {
                    db.insertMessages(this.TestName, "5. Otimização de arquivos (minify *.js, *.css)", "URL: <a href=\"" + info.Target.AbsoluteUri + "\" target=\"_blank\">" + info.Target.AbsoluteUri + "</a>" + Environment.NewLine + (info.Referer != null ? "Referer: <a href=\"" + info.Referer.AbsoluteUri + "\" target=\"_blank\">" + info.Referer.AbsoluteUri + "</a>" + Environment.NewLine : "") + Environment.NewLine + "Redução estimada de: " + FileResources.formatData(percentMin, ChartDataType.Percent) + Environment.NewLine + "Tamanho original: " + FileResources.formatData(value.Length, ChartDataType.Bytes) + Environment.NewLine + "Tamanho reduzido: " + FileResources.formatData(min.Length, ChartDataType.Bytes));
                    db.insertOptimization(this.TestName, request.RequestUri, value.Length, min.Length);
                }

                if (percentExp > 3)
                {
                    db.insertNonOptimization(this.TestName, request.RequestUri, value.Length, expanded.Length);
                }

                break;

            case CheckType.Image:

                String text      = "";
                String imageInfo = "";

                imageInfo = "Tamanho: " + FileResources.formatData(request.DataLength, ChartDataType.Bytes) + Environment.NewLine;


                if (request.DataLength > (100 * 1024))     //100 Kb
                {
                    text += "* Tamanho da imagem acima de 100Kb" + Environment.NewLine;
                }



                using (MemoryStream stm = new MemoryStream(request.Data))
                    using (Image bmp = Image.FromStream(stm))
                    {
                        Boolean     resolution = false;
                        PixelFormat pxFormat   = PixelFormat.Format24bppRgb;
                        float       saveRes    = bmp.HorizontalResolution;

                        try
                        {
                            imageInfo += "Resolução: " + bmp.Width + " x " + bmp.Height + "px" + Environment.NewLine;
                        }
                        catch { }

                        try
                        {
                            if ((((Int32)bmp.HorizontalResolution) > 72) || (((Int32)bmp.VerticalResolution) > 72))
                            {
                                text += "* Qualidade (DPI) da imagem acima de 72" + Environment.NewLine;
                            }

                            imageInfo += "DPI: " + bmp.HorizontalResolution + Environment.NewLine;
                            resolution = true;
                            saveRes    = 72;
                        }
                        catch { }

                        try
                        {
                            switch (bmp.PixelFormat)
                            {
                            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                            case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                            case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                            case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                            case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
                                text      += "* Qualidade (Bit depth) da imagem acima de 24 bits" + Environment.NewLine;
                                resolution = true;
                                pxFormat   = PixelFormat.Format24bppRgb;
                                break;
                            }

                            switch (bmp.PixelFormat)
                            {
                            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                                imageInfo += "Bit depth: 16 bits" + Environment.NewLine;
                                pxFormat   = bmp.PixelFormat;
                                break;

                            case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
                            case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                            case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
                            case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                                imageInfo += "Bit depth: 16 bits" + Environment.NewLine;
                                pxFormat   = bmp.PixelFormat;
                                break;


                            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                                imageInfo += "Bit depth: 24 bits" + Environment.NewLine;
                                pxFormat   = PixelFormat.Format24bppRgb;
                                break;

                            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                            case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                                imageInfo += "Bit depth: 32 bits" + Environment.NewLine;
                                pxFormat   = PixelFormat.Format24bppRgb;
                                break;

                            case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                                imageInfo += "Bit depth: 48 bits" + Environment.NewLine;
                                pxFormat   = PixelFormat.Format24bppRgb;
                                break;

                            case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                            case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
                                imageInfo += "Bit depth: 64 bits" + Environment.NewLine;
                                pxFormat   = PixelFormat.Format24bppRgb;
                                break;

                            default:
                                imageInfo += "Bit depth: " + bmp.PixelFormat.ToString() + Environment.NewLine;
                                break;
                            }
                        }
                        catch { }

                        try
                        {
                            using (ImageInfo imgInfo = new ImageInfo(bmp))
                            {
                                if (imgInfo.HasExif)
                                {
                                    text      += "* Imagem com informações EXIF. A remoção desta informação reduz o tamanho da imagem" + Environment.NewLine;
                                    resolution = true;
                                }
                            }
                        }
                        catch { }

                        //Realiza a Otimização sugerida e calcula o tamanho estimado a conseguir
                        if (resolution)
                        {
                            try
                            {
                                ImageFormat saveFormat = ImageFormat.Jpeg;
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Bmp;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Png;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Emf;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Exif;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Gif;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Icon;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Tiff;
                                }
                                if (bmp.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
                                {
                                    saveFormat = System.Drawing.Imaging.ImageFormat.Wmf;
                                }


                                Bitmap newImage = new Bitmap(bmp.Width, bmp.Height, pxFormat);
                                newImage.SetResolution(saveRes, saveRes);
                                Graphics g = Graphics.FromImage(newImage);
                                g.SmoothingMode     = SmoothingMode.AntiAlias;
                                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                                g.DrawImage(bmp, 0, 0);
                                g.Dispose();

                                MemoryStream newStm = new MemoryStream();
                                newImage.Save(newStm, saveFormat);

                                if (newStm.Length < request.DataLength)
                                {
                                    db.insertOptimization(this.TestName, request.RequestUri, request.DataLength, newStm.Length);

                                    double percent2 = 100F - (((double)newStm.Length / (double)request.DataLength) * 100F);

                                    text += Environment.NewLine;
                                    text += "Tamanho estimado após redução: " + FileResources.formatData(newStm.Length, ChartDataType.Bytes) + Environment.NewLine;
                                    text += "Redução estimada de: " + FileResources.formatData(percent2, ChartDataType.Percent) + Environment.NewLine;
                                }
                                else
                                {
                                    //Não há redução, a imagem está otimizada
                                    text = "";
                                }

                                newImage.Dispose();
                                newImage = null;
                                newStm.Dispose();
                                newStm = null;
                            }
                            catch (Exception ex)
                            {
#if DEBUG
                                text += "Erro ao calcular a redução: " + ex.Message;
#endif
                            }
                        }
                    }

                if (text != "")
                {
                    db.insertMessages(this.TestName, "6. Otimização de imagem", "Img URL: <a href=\"" + info.Target.AbsoluteUri + "\" target=\"_blank\">" + info.Target.AbsoluteUri + "</a>" + Environment.NewLine + (info.Referer != null ? "Referer: <a href=\"" + info.Referer.AbsoluteUri + "\" target=\"_blank\">" + info.Referer.AbsoluteUri + "</a>" + Environment.NewLine : "") + Environment.NewLine + imageInfo + Environment.NewLine + text);
                }

                break;
            }
        }