2014-04-21 08:38:53 +01:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2014-02-17 16:15:39 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2014-02-19 12:16:58 +01:00
|
|
|
|
|
|
|
|
|
|
using Umbraco.Core;
|
2014-02-17 16:15:39 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Web.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class ImageCropperBaseExtensions
|
|
|
|
|
|
{
|
2014-03-20 14:17:00 +11:00
|
|
|
|
|
2014-02-17 16:15:39 +01:00
|
|
|
|
internal static ImageCropData GetImageCrop(this string json, string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ic = new ImageCropData();
|
2014-02-19 12:16:58 +01:00
|
|
|
|
if (json.DetectIsJson())
|
2014-02-17 16:15:39 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var imageCropperSettings = JsonConvert.DeserializeObject<List<ImageCropData>>(json);
|
2014-03-18 19:47:18 +01:00
|
|
|
|
ic = imageCropperSettings.GetCrop(id);
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
2014-03-20 14:17:00 +11:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Error(typeof(ImageCropperBaseExtensions), "Could not parse the json string: " + json, ex);
|
|
|
|
|
|
}
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
return ic;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static ImageCropDataSet SerializeToCropDataSet(this string json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2014-03-20 14:17:00 +11:00
|
|
|
|
catch (Exception ex)
|
2014-02-17 16:15:39 +01:00
|
|
|
|
{
|
2014-03-20 14:17:00 +11:00
|
|
|
|
LogHelper.Error(typeof(ImageCropperBaseExtensions), "Could not parse the json string: " + json, ex);
|
2014-02-17 16:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageCrops;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-18 19:47:18 +01:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-20 14:17:00 +11:00
|
|
|
|
internal static ImageCropData GetCrop(this IEnumerable<ImageCropData> dataset, string cropAlias)
|
|
|
|
|
|
{
|
2014-03-18 19:47:18 +01:00
|
|
|
|
if (dataset == null || !dataset.Any())
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(cropAlias))
|
|
|
|
|
|
return dataset.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
return dataset.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
|
|
|
|
|
|
}
|
2014-04-21 08:38:53 +01:00
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
}
|
2015-02-27 12:15:28 +00:00
|
|
|
|
else if (crop != null && crop.Coordinates != null && preferFocalPoint == false)
|
2014-04-21 08:38:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|