Added controller method to ImagesController and client-side method to mediahelper to retrieve URL for processed image.

This commit is contained in:
Andy Butland
2020-03-24 12:19:04 +01:00
parent c16965319a
commit e66c92d032
2 changed files with 88 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
* @name umbraco.services.mediaHelper
* @description A helper object used for dealing with media items
**/
function mediaHelper(umbRequestHelper, $log) {
function mediaHelper(umbRequestHelper, $http, $log) {
//container of fileresolvers
var _mediaFileResolvers = {};
@@ -304,11 +304,6 @@ function mediaHelper(umbRequestHelper, $log) {
return imagePath;
}
// Check if file is a svg
if (this.getFileExtension(imagePath) === "svg") {
return imagePath;
}
// If the path is not an image we cannot get a thumb
if (!this.detectIfImageByExtension(imagePath)) {
return null;
@@ -399,6 +394,52 @@ function mediaHelper(umbRequestHelper, $log) {
var lowered = filePath.toLowerCase();
var ext = lowered.substr(lowered.lastIndexOf(".") + 1);
return ext;
},
/**
* @ngdoc function
* @name umbraco.services.mediaHelper#getProcessedImageUrl
* @methodOf umbraco.services.mediaHelper
* @function
*
* @description
* Returns image URL with configured crop and other processing parameters.
*
* @param {string} imagePath Raw image path
* @param {object} options Object describing image generation parameters:
* {
* animationProcessMode: <string>
* focalPoint: {
* left: <int>
* top: <int>
* },
* height: <int>
* mode: <string>
* upscale: <boolean>
* width: <int>
* }
*/
getProcessedImageUrl: function (imagePath, options) {
if (!options) {
return imagePath;
}
return umbRequestHelper.resourcePromise(
$http.get(
umbRequestHelper.getApiUrl(
"imagesApiBaseUrl",
"GetProcessedImageUrl",
{
imagePath,
animationProcessMode: options.animationProcessMode,
focalPoint: options.focalPoint,
height: options.height,
mode: options.mode,
upscale: options.upscale || false,
width: options.width
})),
"Failed to retrieve processed image URL for image: " + imagePath);
}
};

View File

@@ -84,5 +84,46 @@ namespace Umbraco.Web.Editors
return response;
}
/// <summary>
/// Gets a processed image for the image at the given path
/// </summary>
/// <param name="imagePath"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="focalPointLeft"></param>
/// <param name="focalPointTop"></param>
/// <param name="animationProcessMode"></param>
/// <param name="mode"></param>
/// <param name="upscale"></param>
/// <returns></returns>
/// <remarks>
/// If there is no media, image property or image file is found then this will return not found.
/// </remarks>
public string GetProcessedImageUrl(string imagePath,
int? width = null,
int? height = null,
int? focalPointLeft = null,
int? focalPointTop = null,
string animationProcessMode = "first",
string mode = "crop",
bool upscale = false,
string cacheBusterValue = "")
{
var options = new ImageUrlGenerationOptions(imagePath)
{
AnimationProcessMode = "first",
CacheBusterValue = cacheBusterValue,
Height = height,
ImageCropMode = "max",
UpScale = false,
Width = width,
};
if (focalPointLeft.HasValue && focalPointTop.HasValue)
{
options.FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition(focalPointTop.Value, focalPointLeft.Value);
}
return _imageUrlGenerator.GetImageUrl(options);
}
}
}