Add support for thumbs and crops from standard datatypes.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using System.Configuration.Provider;
|
||||
using System.Collections.Specialized;
|
||||
using System.Configuration.Provider;
|
||||
|
||||
namespace Umbraco.Core.Media
|
||||
{
|
||||
public abstract class ImageUrlProviderBase : ProviderBase
|
||||
{
|
||||
public abstract string GetImageUrlFromMedia(int mediaId);
|
||||
public abstract string GetImageUrlFromFileName(string specifiedSrc);
|
||||
public abstract string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters);
|
||||
public abstract string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Configuration;
|
||||
using System.Globalization;
|
||||
using System.Web;
|
||||
@@ -23,11 +24,15 @@ namespace Umbraco.Web.Media
|
||||
Initialize("ImageUrl");
|
||||
}
|
||||
|
||||
public static string GetImageUrl(string specifiedSrc, string field, string provider, int? nodeId = null)
|
||||
public static string GetImageUrl(string specifiedSrc, string field, string provider, string parameters, int? nodeId = null)
|
||||
{
|
||||
string url;
|
||||
ImageUrlProviderBase p = GetProvider(provider);
|
||||
|
||||
NameValueCollection parsedParameters = string.IsNullOrEmpty(parameters)?
|
||||
new NameValueCollection() :
|
||||
HttpUtility.ParseQueryString(parameters);
|
||||
|
||||
string fieldValue = string.Empty;
|
||||
if (!string.IsNullOrEmpty(field))
|
||||
{
|
||||
@@ -56,18 +61,18 @@ namespace Umbraco.Web.Media
|
||||
if (int.TryParse(fieldValue, out mediaId))
|
||||
{
|
||||
//Fetch media
|
||||
url = p.GetImageUrlFromMedia(mediaId);
|
||||
url = p.GetImageUrlFromMedia(mediaId, parsedParameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
//assume file path
|
||||
url = p.GetImageUrlFromFileName(fieldValue);
|
||||
url = p.GetImageUrlFromFileName(fieldValue, parsedParameters);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
url = p.GetImageUrlFromFileName(specifiedSrc);
|
||||
url = p.GetImageUrlFromFileName(specifiedSrc, parsedParameters);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Xml.XPath;
|
||||
using System.Collections.Specialized;
|
||||
using System.Xml.XPath;
|
||||
using Umbraco.Core.Media;
|
||||
using umbraco;
|
||||
|
||||
@@ -12,20 +13,63 @@ namespace Umbraco.Web.Media.ImageUrlProviders
|
||||
base.Initialize(name, config);
|
||||
}
|
||||
|
||||
public override string GetImageUrlFromMedia(int mediaId)
|
||||
public override string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters)
|
||||
{
|
||||
string url = string.Empty;
|
||||
var nodeIterator = library.GetMedia(mediaId, false);
|
||||
if (nodeIterator.Current != null)
|
||||
{
|
||||
var filename = getProperty(nodeIterator, "umbracoFile");
|
||||
var fileExtension = getProperty(nodeIterator, "umbracoExtension");
|
||||
url = filename;
|
||||
string withThumb = addThumbInfo(filename, parameters);
|
||||
url = addCropInfo(withThumb, parameters);
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public override string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters)
|
||||
{
|
||||
string withThumb = addThumbInfo(specifiedSrc, parameters);
|
||||
return addCropInfo(withThumb, parameters);
|
||||
}
|
||||
|
||||
private string addThumbInfo(string filename, NameValueCollection parameters)
|
||||
{
|
||||
string thumb = string.Empty;
|
||||
if (!string.IsNullOrEmpty(parameters["thumb"]))
|
||||
{
|
||||
thumb = parameters["thumb"];
|
||||
}
|
||||
|
||||
if(!string.IsNullOrEmpty(thumb))
|
||||
{
|
||||
int lastIndexOf = filename.LastIndexOf('.');
|
||||
string name = filename.Substring(0, lastIndexOf);
|
||||
string extension = filename.Substring(lastIndexOf, filename.Length - lastIndexOf);
|
||||
return string.Format("{0}_thumb_{1}{2}", name, thumb, extension);
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
private string addCropInfo(string filename, NameValueCollection parameters)
|
||||
{
|
||||
string crop = string.Empty;
|
||||
if (!string.IsNullOrEmpty(parameters["crop"]))
|
||||
{
|
||||
crop = parameters["crop"];
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(crop))
|
||||
{
|
||||
int lastIndexOf = filename.LastIndexOf('.');
|
||||
string name = filename.Substring(0, lastIndexOf);
|
||||
string extension = filename.Substring(lastIndexOf, filename.Length - lastIndexOf);
|
||||
return string.Format("{0}_{1}{2}", name, crop, extension);
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
|
||||
private static string getProperty(XPathNodeIterator nodeIterator, string fileProp)
|
||||
{
|
||||
string xpath = UmbracoSettings.UseLegacyXmlSchema
|
||||
@@ -34,10 +78,5 @@ namespace Umbraco.Web.Media.ImageUrlProviders
|
||||
var file = nodeIterator.Current.SelectSingleNode(xpath).InnerXml;
|
||||
return file;
|
||||
}
|
||||
|
||||
public override string GetImageUrlFromFileName(string specifiedSrc)
|
||||
{
|
||||
return specifiedSrc + "?wheee=1234";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ namespace umbraco.presentation.templateControls
|
||||
public string NodeId { get; set; }
|
||||
public string Field { get; set; }
|
||||
public string Provider { get; set; }
|
||||
public string Parameters { get; set; }
|
||||
|
||||
protected override void Render(HtmlTextWriter writer)
|
||||
{
|
||||
@@ -17,7 +18,7 @@ namespace umbraco.presentation.templateControls
|
||||
bool hasid = int.TryParse(NodeId, out id);
|
||||
int? nodeId = hasid ? id : (int?) null;
|
||||
|
||||
Src = ImageUrl.GetImageUrl(Src, Field, Provider, nodeId);
|
||||
Src = ImageUrl.GetImageUrl(Src, Field, Provider, Parameters, nodeId);
|
||||
base.Render(writer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user