Пример #1
0
        public ActionResult Resize(string url, int width, int height, bool?preserverAspectRatio, int?quality)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(url);
            }
            url = HttpUtility.UrlDecode(url);
            var index = url.IndexOf("?");

            if (index != -1)
            {
                url = url.Substring(0, index);
            }
            var imageFullPath = Server.MapPath(url);

            preserverAspectRatio = preserverAspectRatio ?? true;
            quality = quality ?? 80;
            //var cachingPath = GetCachingFilePath(imageFullPath, width, height, preserverAspectRatio.Value, quality.Value);
            var          imageFormat = ImageTools.ConvertToImageFormat(Path.GetExtension(imageFullPath));
            MemoryStream ms          = new MemoryStream();

            using (FileStream fs = new FileStream(imageFullPath, FileMode.Open, FileAccess.Read))
            {
                ImageTools.ResizeImage(fs, ms, imageFormat, width, height, preserverAspectRatio.Value, quality.Value);
                ms.Position = 0;
            }

            return(File(ms, IOUtility.MimeType(imageFullPath)));
        }
Пример #2
0
        public ActionResult Preview(string imagePath, string rotateTypes)
        {
            var    physicalPath = Server.MapPath(HttpUtility.UrlDecode(imagePath));
            var    imageFormat  = ImageTools.ConvertToImageFormat(Path.GetExtension(physicalPath));
            Stream imageStream  = new MemoryStream();
            Stream outputStream = new MemoryStream();

            try
            {
                imageStream = RotateImage(rotateTypes, physicalPath, imageFormat);

                ImageTools.ResizeImage(imageStream, outputStream, imageFormat, 600, 0, true, 80);
                outputStream.Position = 0;

                return(File(outputStream, IOUtility.MimeType(physicalPath)));
            }
            finally
            {
                if (imageStream != null)
                {
                    imageStream.Close();
                    imageStream.Dispose();
                }
            }
        }
Пример #3
0
        public ActionResult Preview(string folderName, string uuid, string rotateTypes)
        {
            //var physicalPath = Server.MapPath(HttpUtility.UrlDecode(imagePath));
            var mediaFolder  = FolderHelper.Parse <MediaFolder>(Repository, folderName);
            var mediaContent = mediaFolder.CreateQuery().WhereEquals("UUID", uuid).FirstOrDefault();

            var provider    = Kooboo.CMS.Content.Persistence.Providers.DefaultProviderFactory.GetProvider <IMediaContentProvider>();
            var data        = provider.GetContentStream(mediaContent);
            var imageFormat = ImageTools.ConvertToImageFormat(Path.GetExtension(mediaContent.VirtualPath));

            Stream imageStream  = new MemoryStream();
            Stream outputStream = new MemoryStream();

            try
            {
                using (var rawImage = new MemoryStream(data))
                {
                    imageStream = RotateImage(rotateTypes, rawImage, imageFormat);

                    ImageTools.ResizeImage(imageStream, outputStream, imageFormat, 600, 0, true, 80);
                    outputStream.Position = 0;

                    return(File(outputStream, IOUtility.MimeType(mediaContent.VirtualPath)));
                }
            }
            finally
            {
                if (imageStream != null)
                {
                    imageStream.Close();
                    imageStream.Dispose();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Resizes the image.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="preserverAspectRatio">The preserver aspect ratio.保持比例</param>
        /// <param name="quality">The quality.</param>
        /// <returns></returns>
        public virtual ActionResult ResizeImage(string url, int width, int height, bool?preserverAspectRatio, int?quality)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(url);
            }
            if (!ImageTools.IsImageExtension(Path.GetExtension(url)))
            {
                throw new HttpException(403, "");
            }
            url = HttpUtility.UrlDecode(url);
            var index = url.IndexOf("?");

            if (index != -1)
            {
                url = url.Substring(0, index);
            }

            preserverAspectRatio = preserverAspectRatio ?? true;
            quality = quality ?? 80;

            if (url.StartsWith("http://") || url.StartsWith("https://"))
            {
                //now no image cache for azure blob
                var provider     = Kooboo.CMS.Content.Persistence.Providers.DefaultProviderFactory.GetProvider <Kooboo.CMS.Content.Persistence.IMediaContentProvider>();
                var mediaContent = new MediaContent()
                {
                    VirtualPath = url
                };
                var data = provider.GetContentStream(mediaContent);
                if (data != null)
                {
                    using (var imageStream = new MemoryStream(data))
                    {
                        var    imageFormat = ImageTools.ConvertToImageFormat(Path.GetExtension(mediaContent.VirtualPath));
                        Stream outStream   = new MemoryStream();
                        ImageTools.ResizeImage(imageStream, outStream, imageFormat, width, height, preserverAspectRatio.Value, quality.Value);
                        outStream.Position = 0;
                        return(File(outStream, IOUtility.MimeType(url)));
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                var imageFullPath = Server.MapPath(url);
                var cachingPath   = GetCachingFilePath(imageFullPath, width, height, preserverAspectRatio.Value, quality.Value);

                if (!System.IO.File.Exists(cachingPath))
                {
                    if (!System.IO.File.Exists(cachingPath))
                    {
                        var dir = Path.GetDirectoryName(cachingPath);
                        IOUtility.EnsureDirectoryExists(dir);
                        var success = ImageTools.ResizeImage(imageFullPath, cachingPath, width, height, preserverAspectRatio.Value, quality.Value);
                        if (!success)
                        {
                            cachingPath = imageFullPath;
                        }
                    }
                }
                SetCache(HttpContext.Response, 2592000, "*");
                return(File(cachingPath, IOUtility.MimeType(imageFullPath)));
            }
        }
Пример #5
0
        public virtual ActionResult EditImage(string folderName, string uuid, [System.Web.Mvc.Bind(Prefix = "Crop")] ImageCropModel cropModel,
                                              [System.Web.Mvc.Bind(Prefix = "Scale")]  ImageScaleModel scaleModel, string rotateTypes, string @return)
        {
            var            mediaFolder  = FolderHelper.Parse <MediaFolder>(Repository.Current, folderName);
            var            mediaContent = mediaFolder.CreateQuery().WhereEquals("UUID", uuid).FirstOrDefault();
            JsonResultData data         = new JsonResultData(ModelState);

            Stream       targetStream = new MemoryStream();
            MemoryStream resizedImage = new MemoryStream();
            Stream       imageStream = new MemoryStream();
            int          width = 0, height = 0;

            try
            {
                ImageFormat imageFormat = ImageTools.ConvertToImageFormat(Path.GetExtension(mediaContent.PhysicalPath));
                imageStream = RotateImage(rotateTypes, mediaContent.PhysicalPath, imageFormat);

                if (cropModel != null && cropModel.X.HasValue && cropModel.Width.Value > 0 && cropModel.Height.Value > 0)
                {
                    ImageTools.CropImage(imageStream, targetStream, imageFormat, cropModel.X.Value, cropModel.Y.Value, cropModel.Width.Value, cropModel.Height.Value);
                    targetStream.Position = 0;
                }
                else
                {
                    targetStream = imageStream;
                }
                if (scaleModel != null && scaleModel.Height.HasValue && scaleModel.Width.HasValue)
                {
                    ImageTools.ResizeImage(targetStream, resizedImage, imageFormat, scaleModel.Width.Value, scaleModel.Height.Value, scaleModel.PreserveAspectRatio, scaleModel.Quality ?? 80);
                    resizedImage.Position = 0;
                    targetStream          = resizedImage;
                }

                Image image = Image.FromStream(targetStream);
                width  = image.Width;
                height = image.Height;
                targetStream.Position = 0;

                ServiceFactory.MediaContentManager.Update(Repository.Current, mediaFolder, uuid, mediaContent.FileName, targetStream, User.Identity.Name);

                image.Dispose();
                targetStream.Close();
                //data.ClosePopup = true;
                //data.AddMessage("The image has been changed.".Localize());
            }
            catch (Exception e)
            {
                data.AddException(e);
            }
            finally
            {
                if (targetStream != null)
                {
                    targetStream.Close();
                    targetStream.Dispose();
                    targetStream = null;
                }
                if (resizedImage != null)
                {
                    resizedImage.Close();
                    resizedImage.Dispose();
                    resizedImage = null;
                }
                if (imageStream != null)
                {
                    imageStream.Close();
                    imageStream.Dispose();
                    imageStream = null;
                }
            }
            data.Model            = new { Width = width, Height = height };
            data.RedirectToOpener = false;
            data.ReloadPage       = true;
            return(Json(data));
        }