2019-06-07 08:59:07 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-10-18 11:24:05 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-11-08 12:21:51 +00:00
|
|
|
using ASC.Core.Common.Settings;
|
2019-08-08 09:26:58 +00:00
|
|
|
|
2021-10-18 11:24:05 +00:00
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
using SixLabors.ImageSharp.Formats;
|
|
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
|
2019-06-07 08:59:07 +00:00
|
|
|
namespace ASC.Web.Core.Users
|
|
|
|
{
|
2022-01-21 10:47:56 +00:00
|
|
|
public static class UserPhotoThumbnailManager
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-11-08 12:21:51 +00:00
|
|
|
public static List<ThumbnailItem> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, int x, int y, int width, int height, Guid userId)
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-11-08 12:21:51 +00:00
|
|
|
return SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(x, y, width, height), userId);
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 12:21:51 +00:00
|
|
|
public static List<ThumbnailItem> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, Point point, Size size, Guid userId)
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-11-08 12:21:51 +00:00
|
|
|
return SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(point, size), userId);
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 12:21:51 +00:00
|
|
|
public static List<ThumbnailItem> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, UserPhotoThumbnailSettings thumbnailSettings, Guid userId)
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
|
|
|
if (thumbnailSettings.Size.IsEmpty) return null;
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
var thumbnailsData = new ThumbnailsData(userId, userPhotoManager);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
|
|
|
var resultBitmaps = new List<ThumbnailItem>();
|
|
|
|
|
2021-11-25 08:23:35 +00:00
|
|
|
using var img = thumbnailsData.MainImgBitmap(out var format);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
|
|
|
if (img == null) return null;
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
foreach (var thumbnail in thumbnailsData.ThumbnailList())
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2021-10-18 11:24:05 +00:00
|
|
|
thumbnail.Image = GetImage(img, thumbnail.Size, thumbnailSettings);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
|
|
|
resultBitmaps.Add(thumbnail);
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:50:39 +00:00
|
|
|
thumbnailsData.Save(resultBitmaps);
|
2019-11-08 12:21:51 +00:00
|
|
|
|
2020-10-12 19:39:23 +00:00
|
|
|
settingsManager.SaveForUser(thumbnailSettings, userId);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
return thumbnailsData.ThumbnailList();
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 11:24:05 +00:00
|
|
|
public static Image GetImage(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings)
|
|
|
|
{
|
|
|
|
var x = thumbnailSettings.Point.X > 0 ? thumbnailSettings.Point.X : 0;
|
|
|
|
var y = thumbnailSettings.Point.Y > 0 ? thumbnailSettings.Point.Y : 0;
|
2022-03-28 08:38:50 +00:00
|
|
|
var width = x + thumbnailSettings.Size.Width > mainImg.Width ? mainImg.Width : thumbnailSettings.Size.Width;
|
2022-02-04 17:59:58 +00:00
|
|
|
var height = y + thumbnailSettings.Size.Height > mainImg.Height ? mainImg.Height : thumbnailSettings.Size.Height;
|
|
|
|
|
2021-10-18 11:24:05 +00:00
|
|
|
var rect = new Rectangle(x,
|
|
|
|
y,
|
2022-02-04 17:59:58 +00:00
|
|
|
width,
|
|
|
|
height);
|
2021-10-18 11:24:05 +00:00
|
|
|
|
2022-05-09 18:15:27 +00:00
|
|
|
mainImg.Mutate(x => x.BackgroundColor(Color.White).Crop(rect).Resize(new ResizeOptions
|
2021-10-18 11:24:05 +00:00
|
|
|
{
|
2022-05-09 18:15:27 +00:00
|
|
|
Size = size
|
2022-02-04 17:59:58 +00:00
|
|
|
}));
|
2022-05-09 15:57:07 +00:00
|
|
|
|
|
|
|
return mainImg;
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ThumbnailItem
|
|
|
|
{
|
|
|
|
public Size Size { get; set; }
|
|
|
|
public string ImgUrl { get; set; }
|
2021-10-18 11:24:05 +00:00
|
|
|
public Image Image { get; set; }
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ThumbnailsData
|
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
private Guid UserId { get; set; }
|
2020-08-12 09:58:08 +00:00
|
|
|
private UserPhotoManager UserPhotoManager { get; }
|
2019-09-09 12:56:33 +00:00
|
|
|
|
|
|
|
public ThumbnailsData(Guid userId, UserPhotoManager userPhotoManager)
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
UserId = userId;
|
|
|
|
UserPhotoManager = userPhotoManager;
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 11:24:05 +00:00
|
|
|
public Image MainImgBitmap(out IImageFormat format)
|
|
|
|
{
|
|
|
|
var img = UserPhotoManager.GetPhotoImage(UserId, out var imageFormat);
|
|
|
|
format = imageFormat;
|
|
|
|
return img;
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public string MainImgUrl()
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
return UserPhotoManager.GetPhotoAbsoluteWebPath(UserId);
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public List<ThumbnailItem> ThumbnailList()
|
2019-08-08 09:26:58 +00:00
|
|
|
{
|
|
|
|
return new List<ThumbnailItem>
|
|
|
|
{
|
|
|
|
new ThumbnailItem
|
|
|
|
{
|
|
|
|
Size = UserPhotoManager.RetinaFotoSize,
|
2019-09-12 11:34:58 +00:00
|
|
|
ImgUrl = UserPhotoManager.GetRetinaPhotoURL(UserId)
|
2019-08-08 09:26:58 +00:00
|
|
|
},
|
|
|
|
new ThumbnailItem
|
|
|
|
{
|
|
|
|
Size = UserPhotoManager.MaxFotoSize,
|
2019-09-12 11:34:58 +00:00
|
|
|
ImgUrl = UserPhotoManager.GetMaxPhotoURL(UserId)
|
2019-08-08 09:26:58 +00:00
|
|
|
},
|
|
|
|
new ThumbnailItem
|
|
|
|
{
|
|
|
|
Size = UserPhotoManager.BigFotoSize,
|
2019-09-12 11:34:58 +00:00
|
|
|
ImgUrl = UserPhotoManager.GetBigPhotoURL(UserId)
|
2019-08-08 09:26:58 +00:00
|
|
|
},
|
|
|
|
new ThumbnailItem
|
|
|
|
{
|
|
|
|
Size = UserPhotoManager.MediumFotoSize,
|
2019-09-12 11:34:58 +00:00
|
|
|
ImgUrl = UserPhotoManager.GetMediumPhotoURL(UserId)
|
2019-08-08 09:26:58 +00:00
|
|
|
},
|
|
|
|
new ThumbnailItem
|
|
|
|
{
|
|
|
|
Size = UserPhotoManager.SmallFotoSize,
|
2019-09-12 11:34:58 +00:00
|
|
|
ImgUrl = UserPhotoManager.GetSmallPhotoURL(UserId)
|
2019-08-08 09:26:58 +00:00
|
|
|
}
|
|
|
|
};
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public void Save(List<ThumbnailItem> bitmaps)
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2019-08-16 08:44:03 +00:00
|
|
|
foreach (var item in bitmaps)
|
|
|
|
{
|
2021-10-18 11:24:05 +00:00
|
|
|
using var mainImgBitmap = MainImgBitmap(out var format);
|
|
|
|
UserPhotoManager.SaveThumbnail(UserId, item.Image, format);
|
2019-08-16 08:44:03 +00:00
|
|
|
}
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|