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 19:27:00 +00:00
|
|
|
public string GetCropUrl(string alias, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null)
|
2014-03-20 14:57:48 +11:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var crop = Crops.GetCrop(alias);
|
|
|
|
|
|
2014-03-24 17:58:12 +00:00
|
|
|
if (crop == null && string.IsNullOrEmpty(alias) == false)
|
|
|
|
|
{
|
2014-03-24 15:44:04 +00:00
|
|
|
return null;
|
2014-03-24 17:58:12 +00:00
|
|
|
}
|
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
|
|
|
|
2014-04-21 08:38:53 +01:00
|
|
|
var cropBaseUrl = this.GetCropBaseUrl(alias, useFocalPoint);
|
|
|
|
|
if (cropBaseUrl != null)
|
2014-03-24 15:44:04 +00:00
|
|
|
{
|
2014-04-21 08:38:53 +01:00
|
|
|
sb.Append(cropBaseUrl);
|
2014-03-24 18:37:15 +00:00
|
|
|
}
|
2014-03-20 14:57:48 +11:00
|
|
|
|
2014-03-24 18:37:15 +00:00
|
|
|
if (crop != null && useCropDimensions)
|
2014-03-24 11:43:55 +00:00
|
|
|
{
|
|
|
|
|
sb.Append("&width=").Append(crop.Width);
|
|
|
|
|
sb.Append("&height=").Append(crop.Height);
|
|
|
|
|
}
|
2014-03-24 18:37:15 +00:00
|
|
|
|
2014-03-24 19:27:00 +00:00
|
|
|
if (cacheBusterValue != null)
|
2014-03-24 11:43:55 +00:00
|
|
|
{
|
2014-03-24 19:27:00 +00:00
|
|
|
sb.Append("&rnd=").Append(cacheBusterValue);
|
2014-03-24 11:43:55 +00:00
|
|
|
}
|
2014-03-24 18:37:15 +00:00
|
|
|
|
2014-03-20 14:57:48 +11:00
|
|
|
return sb.ToString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasFocalPoint()
|
|
|
|
|
{
|
2014-03-24 17:58:12 +00:00
|
|
|
return FocalPoint != null && FocalPoint.Top != 0.5m && FocalPoint.Top != 0.5m;
|
2014-03-20 14:57:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|