コード例 #1
0
ファイル: Util.cs プロジェクト: netnanetn/Competitiveness
        public static string GetThumbLink(string imagePath, ThumbSizeEnum thumbSize, bool includeMediaDomain = true)
        {
            string subPath = "";

            if (imagePath != null)
            {
                if (imagePath.StartsWith("~/"))
                {
                    // subPath = imagePath.Substring(1, imagePath.LastIndexOf("/"));
                    subPath = imagePath.Substring(1, imagePath.Length - 1);
                }
                else
                {
                    // subPath = imagePath.Substring(0, imagePath.LastIndexOf("/"));
                    subPath = imagePath;
                }
            }

            string currentPath = "/Thumbnail/" + thumbSize.ToString() + subPath;

            if (includeMediaDomain)
            {
                currentPath = EngineContext.Current.FalconConfig.MediaDomainName + currentPath;
            }

            return(currentPath);
        }
コード例 #2
0
        public bool CreateThumnail(ThumbSizeEnum thumbSize, string imagePath)
        {
            try
            {
                if (string.IsNullOrEmpty(imagePath))
                {
                    return(false);
                }
                if (File.Exists(HttpContext.Current.Server.MapPath(imagePath)))
                {
                    string subPath;
                    if (!imagePath.StartsWith("~/"))
                    {
                        imagePath = "~" + imagePath;
                    }
                    subPath = imagePath.Substring(1, imagePath.LastIndexOf("/"));

                    string savePath = "~/Thumbnail/" + thumbSize.ToString() + subPath;

                    if (!Directory.Exists(HttpContext.Current.Server.MapPath(savePath)))
                    {
                        Directory.CreateDirectory(HttpContext.Current.Server.MapPath(savePath));
                    }

                    savePath += imagePath.Substring(imagePath.LastIndexOf("/"));

                    ThumbnailSetting setting = _thumbService.GetByThumbSize(thumbSize);
                    if (setting == null)
                    {
                        return(false);
                    }

                    return(CreateImage(imagePath, setting.Width, setting.Height, savePath));
                }
                return(false);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
コード例 #3
0
        public void ProcessRequest(HttpContext context)
        {
            string image = context.Request.QueryString["i"];
            string size  = context.Request.QueryString["s"];
            var    cache = String.Equals(context.Request.QueryString["c"], "1");

            if (!string.IsNullOrEmpty(image) && image.StartsWith("http") == false)
            {
                ThumbSizeEnum thumbSize = GetThumbSize(size);
                string        subPath;
                if (image.StartsWith("~/"))
                {
                    subPath = image.Substring(1);
                }
                else
                {
                    subPath = image;
                }

                string currentPath = "~/Thumbnail/" + thumbSize.ToString() + subPath;
                try
                {
                    currentPath = context.Server.MapPath(currentPath);

                    if (File.Exists(context.Server.MapPath(image)))
                    {
                        if (cache && File.Exists(currentPath))
                        {
                            OutputCacheResponse(context, File.GetLastWriteTime(currentPath));
                            context.Response.ContentType = "image/jpg";
                            context.Response.WriteFile(currentPath);
                        }
                        else
                        {
                            var thumbnail = new Falcon.Services.Thumbnails.Thumbnail(EngineContext.Current.Resolve <IThumbnailSettingService>());

                            bool saveSuccess = thumbnail.CreateThumnail(thumbSize, image);
                            // Tạo luôn tat cả kích cỡ
                            thumbnail.CreateAllThumbnails(image);

                            if (saveSuccess && File.Exists(currentPath))
                            {
                                context.Response.ContentType = "image/jpg";
                                context.Response.WriteFile(currentPath);
                            }
                            else
                            {
                                ReturnDefaultImage(context);
                            }
                        }
                    }
                    else
                    {
                        ReturnDefaultImage(context);
                    }
                }
                catch
                {
                    ReturnDefaultImage(context);
                }
            }
            else
            {
                ReturnDefaultImage(context);
            }
        }
コード例 #4
0
 public ThumbnailSetting GetByThumbSize(ThumbSizeEnum thumbSize)
 {
     return(_thumbnailsettingRepository.Table.SingleOrDefault(c => c.ThumbSize == thumbSize.ToString()));
 }