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

167 lines
5.4 KiB
C#
Raw Normal View History

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.Models;
using Umbraco.Web.PropertyEditors;
namespace Umbraco.Web
{
public static class ImageCropperTemplateExtensions
{
2014-02-17 16:15:39 +01:00
//this only takes the crop json into account
2014-02-24 15:33:47 +01:00
public static string GetCropUrl(this IPublishedContent mediaItem, string propertyAlias, string cropAlias)
2014-02-17 16:15:39 +01:00
{
2014-02-19 17:08:55 +11:00
mediaItem.HasProperty(propertyAlias);
2014-02-17 16:15:39 +01:00
var property = mediaItem.GetPropertyValue<string>(propertyAlias);
if (string.IsNullOrEmpty(property))
return string.Empty;
2014-02-19 12:16:58 +01:00
if (property.DetectIsJson())
2014-02-17 16:15:39 +01:00
{
var cropDataSet = property.SerializeToCropDataSet();
2014-02-19 12:16:58 +01:00
return cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias);
2014-02-17 16:15:39 +01:00
}
else
{
2014-02-19 12:16:58 +01:00
return property;
2014-02-17 16:15:39 +01:00
}
}
2014-02-24 15:33:47 +01:00
public static string GetCropUrl(
2014-02-17 16:15:39 +01:00
this IPublishedContent mediaItem,
int? width = null,
int? height = null,
int? quality = null,
Mode? mode = null,
Anchor? anchor = null,
2014-03-18 15:20:48 +00:00
string propertyAlias = null,
string cropAlias = null,
string furtherOptions = null)
2014-02-17 16:15:39 +01:00
{
string imageCropperValue = null;
string mediaItemUrl = null;
2014-03-18 15:20:48 +00:00
if (mediaItem.HasPropertyAndValueAndCrop(propertyAlias, cropAlias))
2014-02-17 16:15:39 +01:00
{
2014-03-18 15:20:48 +00:00
imageCropperValue = mediaItem.GetPropertyValue<string>(propertyAlias);
2014-02-17 16:15:39 +01:00
}
//this probably shouldn't be needed but it is currently as mediaItem.Url is populated with full crop JSON
mediaItemUrl = mediaItem.Url.DetectIsJson() ? mediaItem.Url.SerializeToCropDataSet().Src : mediaItem.Url;
return mediaItem != null ? GetCropUrl(mediaItemUrl, width, height, quality, mode, anchor, imageCropperValue, cropAlias, furtherOptions) : string.Empty;
2014-02-17 16:15:39 +01:00
}
2014-02-24 15:33:47 +01:00
public static string GetCropUrl(
2014-02-17 16:15:39 +01:00
this string imageUrl,
int? width = null,
int? height = null,
int? quality = null,
Mode? mode = null,
Anchor? anchor = null,
string imageCropperValue = null,
string cropAlias = null,
string furtherOptions = null)
2014-02-17 16:15:39 +01:00
{
if (!string.IsNullOrEmpty(imageUrl))
{
var imageResizerUrl = new StringBuilder();
2014-02-19 12:16:58 +01:00
if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson())
2014-02-17 16:15:39 +01:00
{
2014-03-18 15:20:48 +00:00
var cropDataSet = imageCropperValue.SerializeToCropDataSet();
2014-03-19 09:52:53 +00:00
imageResizerUrl.Append(cropDataSet.Src);
var crop = cropDataSet.Crops.FirstOrDefault(x => cropAlias != null && x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
2014-03-19 09:52:53 +00:00
if (crop != null && crop.Coordinates != null)
{
imageResizerUrl.Append("?crop=");
imageResizerUrl.Append(crop.Coordinates.X1).Append(",");
imageResizerUrl.Append(crop.Coordinates.Y1).Append(",");
imageResizerUrl.Append(crop.Coordinates.X2).Append(",");
imageResizerUrl.Append(crop.Coordinates.Y2);
imageResizerUrl.Append("&cropmode=percentage");
}
else
{
if (cropDataSet.HasFocalPoint())
{
imageResizerUrl.Append("?center=" + cropDataSet.FocalPoint.Top + "," + cropDataSet.FocalPoint.Left);
imageResizerUrl.Append("&mode=crop");
}
else
{
imageResizerUrl.Append("?anchor=center");
imageResizerUrl.Append("&mode=crop");
}
}
2014-02-17 16:15:39 +01:00
}
else
{
2014-03-19 09:52:53 +00:00
imageResizerUrl.Append(imageUrl);
2014-02-17 16:15:39 +01:00
if (mode == null)
{
mode = Mode.Pad;
}
imageResizerUrl.Append("?mode=" + mode.ToString().ToLower());
if (anchor != null)
{
imageResizerUrl.Append("&anchor=" + anchor.ToString().ToLower());
}
}
if (quality != null)
{
imageResizerUrl.Append("&quality=" + quality);
}
if (width != null)
{
imageResizerUrl.Append("&width=" + width);
}
if (height != null)
{
imageResizerUrl.Append("&height=" + height);
}
if (furtherOptions != null)
{
imageResizerUrl.Append(furtherOptions);
}
return imageResizerUrl.ToString();
}
return string.Empty;
}
public enum Mode
{
Crop,
Max,
Strech,
Pad
}
public enum Anchor
{
Center,
Top,
Right,
Bottom,
Left
}
}
}