private static string DoResize(IDictionary attributes, out int finalWidth, out int finalHeight, out string newSrc)
        {
            var fs        = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();
            var orgSrc    = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " "));
            var orgDim    = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray());
            var orgWidth  = float.Parse(orgDim[0]);
            var orgHeight = float.Parse(orgDim[1]);
            var newWidth  = int.Parse(helper.FindAttribute(attributes, "width"));
            var newHeight = int.Parse(helper.FindAttribute(attributes, "height"));

            newSrc = "";

            if (orgHeight > 0 && orgWidth > 0 && orgSrc != "")
            {
                // Check dimensions
                if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight))
                {
                    newHeight = (int)Math.Round(newWidth * (orgHeight / orgWidth));
                }
                else
                {
                    newWidth = (int)Math.Round(newHeight * (orgWidth / orgHeight));
                }

                var orgPath = fs.GetRelativePath(orgSrc);
                if (fs.FileExists(orgPath))
                {
                    var uf = new UmbracoFile(orgPath);

                    try
                    {
                        newSrc = uf.Resize(newWidth, newHeight);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error <tinyMCEImageHelper>(string.Format("The file {0} could not be resized, reverting the image src attribute to the original source: {1}", orgPath, orgSrc), ex);
                        newSrc = orgSrc;
                    }
                }
                else
                {
                    LogHelper.Warn <tinyMCEImageHelper>(string.Format("The file {0} does not exist, reverting the image src attribute to the original source: {1}", orgPath, orgSrc));
                    newSrc = orgSrc;
                }
            }

            finalWidth  = newWidth;
            finalHeight = newHeight;

            return(" width=\"" + newWidth + "\"  height=\"" + newHeight + "\"");
        }
Esempio n. 2
0
        private static string DoResize(IDictionary attributes, out int finalWidth, out int finalHeight, out string newSrc)
        {
            var fs        = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();
            var orgSrc    = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " "));
            var orgDim    = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray());
            var orgWidth  = float.Parse(orgDim[0]);
            var orgHeight = float.Parse(orgDim[1]);
            var newWidth  = int.Parse(helper.FindAttribute(attributes, "width"));
            var newHeight = int.Parse(helper.FindAttribute(attributes, "height"));

            newSrc = "";

            if (orgHeight > 0 && orgWidth > 0 && orgSrc != "")
            {
                // Check dimensions
                if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight))
                {
                    newHeight = (int)Math.Round(newWidth * (orgHeight / orgWidth));
                }
                else
                {
                    newWidth = (int)Math.Round(newHeight * (orgWidth / orgHeight));
                }

                var orgPath = fs.GetRelativePath(orgSrc);
                if (fs.FileExists(orgPath))
                {
                    var uf = new UmbracoFile(orgPath);
                    newSrc = uf.Resize(newWidth, newHeight);
                }
            }

            finalWidth  = newWidth;
            finalHeight = newHeight;

            return(" width=\"" + newWidth + "\"  height=\"" + newHeight + "\"");
        }
Esempio n. 3
0
        private static string doResize(Hashtable attributes, out int finalWidth, out int finalHeight)
        {
            var fs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();

            string resizeDim = helper.FindAttribute(attributes, "width") + "," +
                               helper.FindAttribute(attributes, "height");

            string[] resizeDimSplit = resizeDim.Split(',');
            string   orgSrc         = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " "));

            string[] orgDim    = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray());
            float    orgWidth  = float.Parse(orgDim[0]);
            float    orgHeight = float.Parse(orgDim[1]);
            string   newSrc    = "";
            int      newWidth  = int.Parse(resizeDimSplit[0]);
            int      newHeight = int.Parse(resizeDimSplit[1]);

            if (orgHeight > 0 && orgWidth > 0 && resizeDim != "" && orgSrc != "")
            {
                // Check dimensions
                if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight))
                {
                    newHeight = (int)Math.Round((float)newWidth * (orgHeight / orgWidth));
                }
                else
                {
                    newWidth = (int)Math.Round((float)newHeight * (orgWidth / orgHeight));
                }

                // update orgSrc to remove umbraco reference
                //string resolvedMedia = IOHelper.ResolveUrl(SystemDirectories.Media);
                //if (IOHelper.ResolveUrl(orgSrc).IndexOf(resolvedMedia) > -1)
                //{
                //    orgSrc = SystemDirectories.Media + orgSrc.Substring(orgSrc.IndexOf(resolvedMedia) + resolvedMedia.Length); //, orgSrc.Length - orgSrc.IndexOf(String.Format("/media/", SystemDirectories.Media)));

                //}

                //string fullSrc = IOHelper.MapPath(orgSrc);

                var orgPath = fs.GetRelativePath(orgSrc);
                if (fs.FileExists(orgPath))
                {
                    var uf = new UmbracoFile(orgPath);
                    newSrc = uf.Resize(newWidth, newHeight);
                }

                /*
                 * // Load original image
                 * Image image = Image.FromFile(fullSrc);
                 *
                 *
                 * // Create new image with best quality settings
                 * Bitmap bp = new Bitmap(newWidth, newHeight);
                 * Graphics g = Graphics.FromImage(bp);
                 * g.SmoothingMode = SmoothingMode.HighQuality;
                 * g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                 * g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                 *
                 * // Copy the old image to the new and resized
                 * Rectangle rect = new Rectangle(0, 0, newWidth, newHeight);
                 * g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                 *
                 * // Copy metadata
                 * ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                 * ImageCodecInfo codec = null;
                 * for (int i = 0; i < codecs.Length; i++)
                 * {
                 *  if (codecs[i].MimeType.Equals("image/jpeg"))
                 *      codec = codecs[i];
                 * }
                 *
                 * // Set compresion ratio to 90%
                 * EncoderParameters ep = new EncoderParameters();
                 * ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
                 *
                 * // Save the new image
                 * bp.Save(fullSrcNew, codec, ep);
                 * */
            }

            // return the new width and height
            finalWidth  = newWidth;
            finalHeight = newHeight;

            //GE: When the SystemDirectories.Media contains a ~, newSrc will also contain this and hasn't been resolved.
            //This causes the editor to save content which contains a virtual url, and thus the image doesn't serve
            //the admin editor successfully displays the image because the HTML is rewritten, but when you get the RTE content in the template
            //it hasn't been replaced
            //2011-08-12 added a IOHelper.ResolveUrl call around newSrc
            //2012-08-20 IFileSystem now takes care of URL resolution, so should be safe to assume newSrc is a full/absolute URL
            return
                (" src=\"" + newSrc + "\"  width=\"" + newWidth.ToString() + "\"  height=\"" + newHeight.ToString() +
                 "\"");
        }