/// <summary> /// 上传Logo /// </summary> /// <param name="associateId"></param> /// <param name="stream"></param> /// <returns>上传文件的相对路径(包含文件名)</returns> public string UploadLogo(object associateId, Stream stream) { string relativeFileName = string.Empty; if (stream != null) { ILogoSettingsManager logoSettingsManager = DIContainer.Resolve <ILogoSettingsManager>(); LogoSettings logoSettings = logoSettingsManager.Get(); //检查是否需要缩放原图 Image image = Image.FromStream(stream); if (image.Height > this.TenantLogoSettings.MaxHeight || image.Width > this.TenantLogoSettings.MaxWidth) { stream = ImageProcessor.Resize(stream, this.TenantLogoSettings.MaxWidth, this.TenantLogoSettings.MaxHeight, logoSettings.ResizeMethod); } string relativePath = GetLogoRelativePath(associateId); string fileName = GetLogoFileName(associateId); relativeFileName = relativePath + "\\" + fileName; StoreProvider.AddOrUpdateFile(relativePath, fileName, stream); stream.Dispose(); //根据不同租户类型的设置生成不同尺寸的图片,用于图片直连访问 if (this.TenantLogoSettings.ImageSizeTypes != null && this.TenantLogoSettings.ImageSizeTypes.Count > 0) { foreach (var imageSizeType in this.TenantLogoSettings.ImageSizeTypes.Values) { string sizedFileName = StoreProvider.GetSizeImageName(fileName, imageSizeType.Key, imageSizeType.Value); StoreProvider.DeleteFile(relativePath, sizedFileName); IStoreFile file = StoreProvider.GetResizedImage(relativePath, fileName, imageSizeType.Key, imageSizeType.Value); } } } return(relativeFileName); }
/// <summary> /// 创建附件 /// </summary> /// <param name="attachment">附件</param> /// <param name="contentStream">文件流</param> public void Create(T attachment, Stream contentStream) { if (contentStream == null) { return; } if (attachment.MediaType == MediaType.Image) { //检查是否需要缩放原图 if (TenantAttachmentSettings.MaxImageWidth > 0 || TenantAttachmentSettings.MaxImageHeight > 0) { int maxWidth = TenantAttachmentSettings.MaxImageWidth > 0 ? TenantAttachmentSettings.MaxImageWidth : attachment.Width; int maxHeight = TenantAttachmentSettings.MaxImageHeight > 0 ? TenantAttachmentSettings.MaxImageHeight : attachment.Height; if (attachment.Width > maxWidth || attachment.Height > maxHeight) { Stream resizedStream = ImageProcessor.Resize(contentStream, maxWidth, maxHeight, ResizeMethod.KeepAspectRatio); if (resizedStream != contentStream) { contentStream.Dispose(); } contentStream = resizedStream; } } IAttachmentSettingsManager attachmentSettingsManager = DIContainer.Resolve <IAttachmentSettingsManager>(); AttachmentSettings attachmentSettings = attachmentSettingsManager.Get(); Image image = Image.FromStream(contentStream); bool isGif = ImageProcessor.IsGIFAnimation(image); //检查是否需要打水印 if (!isGif && TenantAttachmentSettings.EnableWatermark && attachmentSettings.WatermarkSettings.WatermarkType != WatermarkType.None && image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight) { ImageProcessor imageProcessor = new ImageProcessor(); if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Text) { TextWatermarkFilter watermarkFilter = new TextWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkText, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity); imageProcessor.Filters.Add(watermarkFilter); } else if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Image) { ImageWatermarkFilter watermarkFilter = new ImageWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkImagePhysicalPath, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity); imageProcessor.Filters.Add(watermarkFilter); } //如果需要添加水印,则除水印图片以外,还需要保留原图 StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.GetOriginalFileName(), contentStream); //给原始尺寸的图添加水印 using (Stream watermarkImageStream = imageProcessor.Process(contentStream)) { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, watermarkImageStream); } //根据设置生成不同尺寸的图片,并添加水印 if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0) { foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes) { Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod); image = Image.FromStream(resizedStream); if (image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight) { using (Stream watermarkImageStream = imageProcessor.Process(resizedStream)) { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), watermarkImageStream); } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream); } if (resizedStream != contentStream) { resizedStream.Dispose(); } } } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream); if (!isGif) { //根据设置生成不同尺寸的图片 if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0) { foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes) { Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod); StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream); if (resizedStream != contentStream) { resizedStream.Dispose(); } } } } } } else { StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream); } if (contentStream != null) { contentStream.Dispose(); } EventBus <T> .Instance().OnBefore(attachment, new CommonEventArgs(EventOperationType.Instance().Create())); attachmentRepository.Insert(attachment); EventBus <T> .Instance().OnAfter(attachment, new CommonEventArgs(EventOperationType.Instance().Create())); }