DocSpace-buildtools/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs

161 lines
6.5 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
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
{
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
{
return SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(x, y, width, height), userId);
2019-06-07 08:59:07 +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
{
return SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(point, size), userId);
2019-06-07 08:59:07 +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;
foreach (var thumbnail in thumbnailsData.ThumbnailList())
2019-06-07 08:59:07 +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);
2020-10-12 19:39:23 +00:00
settingsManager.SaveForUser(thumbnailSettings, userId);
2019-06-07 08:59:07 +00:00
return thumbnailsData.ThumbnailList();
2019-06-07 08:59:07 +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-02-04 17:59:58 +00:00
var width = x + thumbnailSettings.Size.Width > mainImg.Width ? mainImg.Width : thumbnailSettings.Size.Width;
var height = y + thumbnailSettings.Size.Height > mainImg.Height ? mainImg.Height : thumbnailSettings.Size.Height;
var rect = new Rectangle(x,
y,
2022-02-04 17:59:58 +00:00
width,
height);
Image destRound = mainImg.Clone(x => x.Crop(rect).Resize(new ResizeOptions
{
Size = size,
Mode = ResizeMode.Stretch
2022-02-04 17:59:58 +00:00
}));
return destRound;
2019-06-07 08:59:07 +00:00
}
}
public class ThumbnailItem
{
public Size Size { get; set; }
public string ImgUrl { get; set; }
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
}
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
}
public string MainImgUrl()
2019-06-07 08:59:07 +00:00
{
return UserPhotoManager.GetPhotoAbsoluteWebPath(UserId);
2019-06-07 08:59:07 +00:00
}
public List<ThumbnailItem> ThumbnailList()
2019-08-08 09:26:58 +00:00
{
return new List<ThumbnailItem>
{
new ThumbnailItem
{
Size = UserPhotoManager.RetinaFotoSize,
ImgUrl = UserPhotoManager.GetRetinaPhotoURL(UserId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.MaxFotoSize,
ImgUrl = UserPhotoManager.GetMaxPhotoURL(UserId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.BigFotoSize,
ImgUrl = UserPhotoManager.GetBigPhotoURL(UserId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.MediumFotoSize,
ImgUrl = UserPhotoManager.GetMediumPhotoURL(UserId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.SmallFotoSize,
ImgUrl = UserPhotoManager.GetSmallPhotoURL(UserId)
2019-08-08 09:26:58 +00:00
}
};
2019-06-07 08:59:07 +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)
{
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
}
}
}