Преглед на файлове

上传图片水印功能

懒得勤快 преди 5 години
родител
ревизия
1d3d5eff5c

+ 71 - 0
src/Masuit.MyBlogs.Core/Common/ImageWatermarker.cs

@@ -0,0 +1,71 @@
+using Masuit.Tools;
+using System.Drawing;
+using System.Drawing.Text;
+using System.IO;
+
+namespace Masuit.MyBlogs.Core.Common
+{
+    public class ImageWatermarker
+    {
+        public bool SkipWatermarkForSmallImages { get; set; }
+
+        public int SmallImagePixelsThreshold { get; set; }
+
+        private readonly Stream _stream;
+
+        public ImageWatermarker(Stream originStream)
+        {
+            _stream = originStream;
+        }
+
+        public MemoryStream AddWatermark(string watermarkText, Color color, WatermarkPosition watermarkPosition = WatermarkPosition.BottomRight, int textPadding = 10, int fontSize = 20, Font font = null, bool textAntiAlias = true)
+        {
+            using var img = Image.FromStream(_stream);
+            if (SkipWatermarkForSmallImages && img.Height * img.Width < SmallImagePixelsThreshold)
+            {
+                return _stream.SaveAsMemoryStream();
+            }
+
+            using var graphic = Graphics.FromImage(img);
+            if (textAntiAlias)
+            {
+                graphic.TextRenderingHint = TextRenderingHint.AntiAlias;
+            }
+
+            using var brush = new SolidBrush(color);
+            using var f = font ?? new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Bold, GraphicsUnit.Pixel);
+            var textSize = graphic.MeasureString(watermarkText, f);
+            int x, y;
+
+            switch (watermarkPosition)
+            {
+                case WatermarkPosition.TopLeft:
+                    x = textPadding;
+                    y = textPadding;
+                    break;
+                case WatermarkPosition.TopRight:
+                    x = img.Width - (int)textSize.Width - textPadding;
+                    y = textPadding;
+                    break;
+                case WatermarkPosition.BottomLeft:
+                    x = textPadding;
+                    y = img.Height - (int)textSize.Height - textPadding;
+                    break;
+                case WatermarkPosition.BottomRight:
+                    x = img.Width - (int)textSize.Width - textPadding;
+                    y = img.Height - (int)textSize.Height - textPadding;
+                    break;
+                default:
+                    x = textPadding;
+                    y = textPadding;
+                    break;
+            }
+
+            graphic.DrawString(watermarkText, f, brush, new Point(x, y));
+            var ms = new MemoryStream();
+            img.Save(ms, img.RawFormat);
+            ms.Position = 0;
+            return ms;
+        }
+    }
+}

+ 12 - 1
src/Masuit.MyBlogs.Core/Common/ImagebedClient.cs

@@ -8,6 +8,7 @@ using Masuit.Tools.Systems;
 using Microsoft.Extensions.Configuration;
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Net;
@@ -50,7 +51,17 @@ namespace Masuit.MyBlogs.Core.Common
         public async Task<(string url, bool success)> UploadImage(Stream stream, string file)
         {
             file = SnowFlake.NewId + Path.GetExtension(file);
-            for (int i = 0; i < 3; i++)
+            if (!string.IsNullOrEmpty(CommonHelper.SystemSettings.GetOrAdd("Watermark", string.Empty)))
+            {
+                var watermarker = new ImageWatermarker(stream)
+                {
+                    SkipWatermarkForSmallImages = true,
+                    SmallImagePixelsThreshold = 40000
+                };
+                stream = watermarker.AddWatermark(CommonHelper.SystemSettings["Watermark"], Color.LightGray, WatermarkPosition.BottomRight, 30);
+            }
+
+            for (var i = 0; i < 3; i++)
             {
                 try
                 {

+ 10 - 0
src/Masuit.MyBlogs.Core/Common/WatermarkPosition.cs

@@ -0,0 +1,10 @@
+namespace Masuit.MyBlogs.Core.Common
+{
+    public enum WatermarkPosition
+    {
+        TopLeft,
+        TopRight,
+        BottomLeft,
+        BottomRight
+    }
+}

+ 0 - 1
src/Masuit.MyBlogs.Core/Extensions/UEditor/UploadHandler.cs

@@ -61,7 +61,6 @@ namespace Masuit.MyBlogs.Core.Extensions.UEditor
                         {
                             Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                         }
-
                         File.WriteAllBytes(localPath, stream.ToArray());
                         Result.Url = savePath;
                     }

+ 1 - 1
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -29,7 +29,7 @@
         <PackageReference Include="CacheManager.Microsoft.Extensions.Caching.Memory" Version="2.0.0-beta-1629" />
         <PackageReference Include="CacheManager.Serialization.Json" Version="2.0.0-beta-1629" />
         <PackageReference Include="CSRedisCore" Version="3.4.5" />
-        <PackageReference Include="DocxToHtml" Version="1.0.4" />
+        <PackageReference Include="DocxToHtml_ldqk" Version="1.0.4" />
         <PackageReference Include="EFSecondLevelCache.Core" Version="2.9.1" />
         <PackageReference Include="Hangfire" Version="1.7.9" />
         <PackageReference Include="Hangfire.Autofac" Version="2.3.1" />

+ 1 - 1
src/Masuit.MyBlogs.Core/Properties/launchSettings.json

@@ -18,7 +18,7 @@
     "Masuit.MyBlogs.Core": {
       "commandName": "Project",
       "environmentVariables": {
-        "ASPNETCORE_ENVIRONMENT": "prod"
+        "ASPNETCORE_ENVIRONMENT": "Development"
       },
       "applicationUrl": "http://localhost:5000"
     }

+ 6 - 0
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/system/home.html

@@ -35,6 +35,12 @@
                 <input type="text" class="form-control" ng-model="Settings.ReservedName" />
             </div>
         </div>
+        <div class="input-group">
+            <span class="input-group-addon">上传图片水印:</span>
+            <div class="fg-line">
+                <input type="text" class="form-control" ng-model="Settings.Watermark" placeholder="留空则上传图片时不添加水印"/>
+            </div>
+        </div>
         <div class="input-group">
             <span class="input-group-addon el-switch" style="text-align: left">
                 <span>闭站保护:</span>