From e66c92d032ed0de41fc548e425526b48d498e7c6 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 24 Mar 2020 12:19:04 +0100 Subject: [PATCH] Added controller method to ImagesController and client-side method to mediahelper to retrieve URL for processed image. --- .../common/services/mediahelper.service.js | 53 ++++++++++++++++--- src/Umbraco.Web/Editors/ImagesController.cs | 41 ++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js index 2271f891ce..953ddf46d9 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js @@ -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: + * focalPoint: { + * left: + * top: + * }, + * height: + * mode: + * upscale: + * width: + * } + */ + 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); } }; diff --git a/src/Umbraco.Web/Editors/ImagesController.cs b/src/Umbraco.Web/Editors/ImagesController.cs index 515b5e9c8c..960b589b6d 100644 --- a/src/Umbraco.Web/Editors/ImagesController.cs +++ b/src/Umbraco.Web/Editors/ImagesController.cs @@ -84,5 +84,46 @@ namespace Umbraco.Web.Editors return response; } + /// + /// Gets a processed image for the image at the given path + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// If there is no media, image property or image file is found then this will return not found. + /// + 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); + } } }