Files
Umbraco-CMS/src/Umbraco.Web/ImageCropperBaseExtensions.cs

94 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Web.Models;
2014-02-17 16:15:39 +01:00
namespace Umbraco.Web
{
2014-02-17 16:15:39 +01:00
internal static class ImageCropperBaseExtensions
{
internal static ImageCropDataSet DeserializeToCropDataSet(this string json)
2014-02-17 16:15:39 +01:00
{
var imageCrops = new ImageCropDataSet();
2014-02-19 12:16:58 +01:00
if (json.DetectIsJson())
2014-02-17 16:15:39 +01:00
{
try
{
imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(json, new JsonSerializerSettings
{
Culture = CultureInfo.InvariantCulture,
FloatParseHandling = FloatParseHandling.Decimal
});
2014-02-17 16:15:39 +01:00
}
catch (Exception ex)
2014-02-17 16:15:39 +01:00
{
LogHelper.Error(typeof(ImageCropperBaseExtensions), "Could not parse the json string: " + json, ex);
2014-02-17 16:15:39 +01:00
}
}
return imageCrops;
}
internal static ImageCropData GetCrop(this ImageCropDataSet dataset, string cropAlias)
{
if (dataset == null || dataset.Crops == null || !dataset.Crops.Any())
return null;
return dataset.Crops.GetCrop(cropAlias);
}
internal static ImageCropData GetCrop(this IEnumerable<ImageCropData> dataset, string cropAlias)
{
var imageCropDatas = dataset.ToArray();
if (dataset == null || imageCropDatas.Any() == false)
return null;
if (string.IsNullOrEmpty(cropAlias))
return imageCropDatas.FirstOrDefault();
return imageCropDatas.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
}
internal static string GetCropBaseUrl(this ImageCropDataSet cropDataSet, string cropAlias, bool preferFocalPoint)
{
var cropUrl = new StringBuilder();
var crop = cropDataSet.GetCrop(cropAlias);
// if crop alias has been specified but not found in the Json we should return null
if (string.IsNullOrEmpty(cropAlias) == false && crop == null)
{
return null;
}
if ((preferFocalPoint && cropDataSet.HasFocalPoint()) || (crop != null && crop.Coordinates == null && cropDataSet.HasFocalPoint()) || (string.IsNullOrEmpty(cropAlias) && cropDataSet.HasFocalPoint()))
{
cropUrl.Append("?center=" + cropDataSet.FocalPoint.Top.ToString(CultureInfo.InvariantCulture) + "," + cropDataSet.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
cropUrl.Append("&mode=crop");
}
else if (crop != null && crop.Coordinates != null && preferFocalPoint == false)
{
cropUrl.Append("?crop=");
cropUrl.Append(crop.Coordinates.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.Y2.ToString(CultureInfo.InvariantCulture));
cropUrl.Append("&cropmode=percentage");
}
else
{
cropUrl.Append("?anchor=center");
cropUrl.Append("&mode=crop");
}
return cropUrl.ToString();
}
2014-02-17 16:15:39 +01:00
}
}