Ejemplo n.º 1
0
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imagefilePath">图片路径</param>
        /// <param name="timming">生成方式</param>
        public static void HandleThumbnail(string imagefilePath, ThumbnailTimming timming)
        {
            FileProperties _fileInfo = FileHelper.GetFileInfo(imagefilePath, @"^(.*)\\upload\\(.+)\\(day_\d+)\\(\d+)(\.[A-Za-z]+)$");

            if (_fileInfo == null)
            {
                return;
            }

            string _root      = _fileInfo.Root,
                   _folder    = _fileInfo.Folder,
                   _subFolder = _fileInfo.SubFolder,
                   _fileName  = _fileInfo.FileName,
                   _fileExt   = _fileInfo.FileNameExt;

            foreach (var pair in UploadConfigContext.ThumbnailConfigDic
                     .Where(t => t.Key.StartsWith(_fileInfo.Folder.ToLower() + "_", StringComparison.OrdinalIgnoreCase) && t.Value.Timming == timming))
            {
                ThumbnailSize _size = pair.Value;
                string        _thumbnailFileFolder = string.Format("{0}\\upload\\{1}\\{2}\\thumb",
                                                                   _root, _folder, _subFolder);

                if (!Directory.Exists(_thumbnailFileFolder))
                {
                    Directory.CreateDirectory(_thumbnailFileFolder);
                }

                string _thumbnailFilePath = string.Format("{0}\\upload\\{1}\\{2}\\thumb\\{3}_{4}_{5}{6}",
                                                          _root, _folder, _subFolder, _fileName, _size.Width, _size.Height, _fileExt);
                ThumbnailHelper.BuilderThumbnail(imagefilePath, _thumbnailFilePath, _size);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 延迟生成图片缩略图
        /// </summary>
        public static void HandlerLazyThumbnail()
        {
            foreach (var group in UploadConfigContext.UploadConfig.UploadFolders)
            {
                string _folder = Path.Combine(UploadConfigContext.UploadPath, group.Path);

                if (!Directory.Exists(_folder))
                {
                    continue;
                }

                foreach (string dayFolder in Directory.GetDirectories(_folder))
                {
                    foreach (string filePath in Directory.GetFiles(dayFolder))
                    {
                        FileProperties _fileInfo = FileHelper.GetFileInfo(filePath, @"^(.+\\day_\d+)\\(\d+)(\.[A-Za-z]+)$");

                        if (_fileInfo == null)
                        {
                            continue;
                        }

                        string _root                = _fileInfo.Root,
                               _fileName            = _fileInfo.FileName,
                               _fileExt             = _fileInfo.FileNameExt,
                               _thumbnailFileFolder = Path.Combine(dayFolder, "Thumb");

                        if (!Directory.Exists(_thumbnailFileFolder))
                        {
                            Directory.CreateDirectory(_thumbnailFileFolder);
                        }

                        foreach (var size in group.ThumbnailSizes)
                        {
                            if (size.Timming != ThumbnailTimming.Lazy)
                            {
                                continue;
                            }

                            string _thumbnailFilePath = string.Format("{0}\\thumb\\{1}_{2}_{3}{4}",
                                                                      _root, _fileName, size.Width, size.Height, _fileExt);

                            if (File.Exists(_thumbnailFilePath) && size.IsReplace)
                            {
                                File.Delete(_thumbnailFilePath);
                            }

                            if (!File.Exists(_thumbnailFilePath))
                            {
                                ThumbnailHelper.BuilderThumbnail(filePath, _thumbnailFilePath, size);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 按需生成缩略图
        /// </summary>
        private void OnDemandCreateThumbnailImage(string filePath, string thumbnailFilePath, string folder, string subFolder, string thumbnailKey)
        {
            //若缩略图不存在,则生成
            if (!File.Exists(thumbnailFilePath))
            {
                string _thumbnailFileFolder = string.Format(@"{0}\{1}\Thumb", folder, subFolder);
                _thumbnailFileFolder = Path.Combine(UploadConfigContext.UploadPath, _thumbnailFileFolder);

                if (!Directory.Exists(_thumbnailFileFolder))
                {
                    Directory.CreateDirectory(_thumbnailFileFolder);
                }

                ThumbnailHelper.BuilderThumbnail(filePath, thumbnailFilePath, UploadConfigContext.ThumbnailConfigDic[thumbnailKey]);
            }
        }