2014-03-20 14:57:48 +11:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models
|
|
|
|
|
{
|
|
|
|
|
[DataContract(Name="imageCropDataSet")]
|
|
|
|
|
public class ImageCropDataSet : IHtmlString
|
|
|
|
|
{
|
|
|
|
|
[DataMember(Name="src")]
|
|
|
|
|
public string Src { get; set;}
|
|
|
|
|
|
|
|
|
|
[DataMember(Name = "focalPoint")]
|
|
|
|
|
public ImageCropFocalPoint FocalPoint { get; set; }
|
|
|
|
|
|
|
|
|
|
[DataMember(Name = "crops")]
|
|
|
|
|
public IEnumerable<ImageCropData> Crops { get; set; }
|
|
|
|
|
|
|
|
|
|
|
2014-03-24 15:44:04 +00:00
|
|
|
public string GetCropUrl(string alias, bool addCropDimensions = true, bool useFocalPoint = false, bool cacheBuster = true)
|
2014-03-20 14:57:48 +11:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var crop = Crops.GetCrop(alias);
|
|
|
|
|
|
2014-03-24 15:44:04 +00:00
|
|
|
if(crop == null && !string.IsNullOrEmpty(alias))
|
|
|
|
|
return null;
|
2014-03-20 14:57:48 +11:00
|
|
|
|
2014-03-24 11:43:55 +00:00
|
|
|
var sb = new StringBuilder();
|
2014-03-24 15:44:04 +00:00
|
|
|
|
|
|
|
|
if (useFocalPoint && HasFocalPoint() || (crop != null && crop.Coordinates == null && HasFocalPoint()) || (string.IsNullOrEmpty(alias) && HasFocalPoint()))
|
|
|
|
|
{
|
|
|
|
|
sb.Append("?center=" + FocalPoint.Top.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + FocalPoint.Left.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
|
|
|
sb.Append("&mode=crop");
|
|
|
|
|
}
|
|
|
|
|
else if (crop != null && crop.Coordinates != null)
|
2014-03-20 14:57:48 +11:00
|
|
|
{
|
|
|
|
|
sb.Append("?crop=");
|
2014-03-24 11:43:55 +00:00
|
|
|
sb.Append(crop.Coordinates.X1.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
|
|
|
|
|
sb.Append(crop.Coordinates.Y1.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
|
|
|
|
|
sb.Append(crop.Coordinates.X2.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
|
|
|
|
|
sb.Append(crop.Coordinates.Y2.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
2014-03-20 14:57:48 +11:00
|
|
|
sb.Append("&cropmode=percentage");
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-03-24 15:44:04 +00:00
|
|
|
{
|
|
|
|
|
sb.Append("?anchor=center");
|
|
|
|
|
sb.Append("&mode=crop");
|
|
|
|
|
}
|
2014-03-20 14:57:48 +11:00
|
|
|
|
2014-03-24 15:44:04 +00:00
|
|
|
if (crop!= null && addCropDimensions)
|
2014-03-24 11:43:55 +00:00
|
|
|
{
|
|
|
|
|
sb.Append("&width=").Append(crop.Width);
|
|
|
|
|
sb.Append("&height=").Append(crop.Height);
|
|
|
|
|
}
|
2014-03-24 15:44:04 +00:00
|
|
|
if (cacheBuster)
|
2014-03-24 11:43:55 +00:00
|
|
|
{
|
|
|
|
|
sb.Append("&rnd=").Append(DateTime.Now.Ticks);
|
|
|
|
|
}
|
2014-03-20 14:57:48 +11:00
|
|
|
return sb.ToString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasFocalPoint()
|
|
|
|
|
{
|
|
|
|
|
return (FocalPoint != null && FocalPoint.Top != 0.5m && FocalPoint.Top != 0.5m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasCrop(string alias)
|
|
|
|
|
{
|
|
|
|
|
return Crops.Any(x => x.Alias == alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasImage()
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(Src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ToHtmlString()
|
|
|
|
|
{
|
|
|
|
|
return this.Src;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|