@cloudinary/url-gen
Version:
You are invited to influence our new SDK [Click here to view github discussion](https://github.com/cloudinary/js-url-gen/discussions/602) =========================
368 lines (363 loc) • 15.3 kB
JavaScript
;
var ResizePadAction = require('./ResizePadAction-70b204bd.cjs');
var ResizeSimpleAction = require('./ResizeSimpleAction-0ef78caa.cjs');
var ResizeScaleAction = require('./ResizeScaleAction-905903b0.cjs');
var ThumbnailAction = require('./ThumbnailAction-f1759347.cjs');
var ResizeCropAction = require('./ResizeCropAction-000b56fc.cjs');
var ResizeFillAction = require('./ResizeFillAction-7a1602bf.cjs');
var ResizeLimitFitAction = require('./ResizeLimitFitAction-22d3f29a.cjs');
var ResizeLimitFillAction = require('./ResizeLimitFillAction-c8c935fe.cjs');
var ResizeLimitPadAction = require('./ResizeLimitPadAction-669adf81.cjs');
var ResizeMinimumPadAction = require('./ResizeMinimumPadAction-5757b7f1.cjs');
var ResizeAdvancedAction = require('./ResizeAdvancedAction-dd8733aa.cjs');
var Qualifier = require('./Qualifier-6633a22f.cjs');
/**
* @description Tries to prevent a "bad crop" by first attempting to use the auto cropping mode, but adding some padding if the algorithm determines that more of the original image needs to be included in the final image.
* @extends Actions.Resize.autoPad
* @memberOf Actions.Resize
* @see Visit {@link Actions.Resize| Resize} for examples
*/
class ResizeAutoPadAction extends ResizeSimpleAction.ResizeSimpleAction {
constructor(cropType, cropWidth, cropHeight) {
super(cropType, cropWidth, cropHeight);
this.addQualifier(new Qualifier.Qualifier('g', 'auto'));
}
/**
* @description Sets the background.
* @param {Qualifiers.Background} backgroundQualifier Defines the background color to use instead of
* transparent background areas or when resizing with padding.
*/
background(backgroundQualifier) {
this._actionModel.background = ResizePadAction.createBackgroundModel(backgroundQualifier);
return this.addQualifier(backgroundQualifier);
}
gravity(gravity) {
// Only `auto` gravity is allowed (and required) for `c_auto_pad` and it's already added automatically in the constructor.
// Although, this method is needed to exist because it is being shown in autogenerated SDK code snippet.
return this;
}
static fromJson(actionModel) {
const result = super.fromJson.apply(this, [actionModel]);
actionModel.background && result.background(ResizePadAction.createBackgroundFromModel(actionModel.background));
return result;
}
}
/**
* @description Determines how to crop, scale, and/or zoom the delivered asset according to the requested dimensions.
* @memberOf Actions
* @namespace Resize
* @see Learn more about Gravity and Focus {@link Qualifiers.Gravity| here }
* @example
* <caption> <h4>Scaling an image</h4> </caption>
* import {Cloudinary} from "@cloudinary/url-gen";
* import {scale, fit, pad, crop} from '@cloudinary/url-gen/actions/resize';
*
* const yourCldInstance = new Cloudinary({cloud:{cloudName:'demo'}});
* const image = yourCldInstance.image('woman');
*
* image.resize( scale(100, 100) );
* // All resize actions have a similar interface.
* // image.resize( fit(100, 100)) );
* // image.resize( pad(100, 100)) );
* // image.resize( crop(100, 100)) );
* // However, Some actions have additional arguments exposed as builder methods.
* // See the documentation for each method for more information
*
*
* // Alternative syntax, using builder methods
* image.resize(
* scale()
* .width(100)
* .height(100)
* );
* image.toString()
*
* @example
* <caption> <h4>Cropping with automatic focus(Gravity)</h4> </caption>
* import {Cloudinary} from "@cloudinary/url-gen";
*
* const yourCldInstance = new Cloudinary({cloud:{cloudName:'demo'}});
* const image = yourCldInstance.image('woman');
*
* import {scale} from '@cloudinary/url-gen/actions/resize';
* import {autoGravity} from '@cloudinary/url-gen/qualifiers/gravity';
*
* image.resize( crop(100, 100).gravity(autoGravity()) );
*
* // Alternative syntax, using builder methods
* image.resize(
* scale()
* .width(100)
* .height(100)
* .gravity(autoGravity())
* );
* image.toString()
*/
/**
* @summary action
* @description
* Changes the size of the image exactly to the given width and height without necessarily retaining the original aspect ratio:<br/>
* all original image parts are visible but might be stretched or shrunk.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ScaleAction}
*/
function scale(width, height) {
return new ResizeScaleAction.ResizeScaleAction('scale', width, height);
}
/**
* @summary action
* @description
* Scales your image based on automatically calculated areas of interest within each specific photo.
*
* For details, see the Imagga Crop and Scale {@link https://cloudinary.com/documentation/imagga_crop_and_scale_addon#smartly_scale_images|add-on documentation}.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeSimpleAction}
*/
function imaggaScale(width, height) {
return new ResizeSimpleAction.ResizeSimpleAction('imagga_scale', width, height);
}
/**
* @summary action
* @description
* Crops your image based on automatically calculated areas of interest within each specific photo.
*
* For details, see the Imagga Crop and Scale {@link https://cloudinary.com/documentation/imagga_crop_and_scale_addon#smartly_crop_images|add-on documentation}.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeSimpleAction}
*/
function imaggaCrop(width, height) {
return new ResizeSimpleAction.ResizeSimpleAction('imagga_crop', width, height);
}
/**
* @summary action
* @description Extracts a region of the given width and height out of the original image.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeCropAction}
*/
function crop(width, height) {
return new ResizeCropAction.ResizeCropAction('crop', width, height);
}
/**
* @summary action
* @description
* Creates an image with the exact given width and height without distorting the image.<br/>
* This option first scales up or down as much as needed to at least fill both of the given dimensions.<br/><br/>
* If the requested aspect ratio is different than the original, cropping will occur on the dimension that exceeds the requested size after scaling.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeFillAction}
*/
function fill(width, height) {
return new ResizeFillAction.ResizeFillAction('fill', width, height);
}
/**
* @summary action
* @description
* The image is resized so that it takes up as much space as possible within a bounding box defined by the given width and height parameters.</br>
* The original aspect ratio is retained and all of the original image is visible.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeSimpleAction}
*/
function fit(width, height) {
return new ResizeSimpleAction.ResizeSimpleAction('fit', width, height);
}
/**
* @summary action
* @description
* Resizes the asset to fill the given width and height while retaining the original aspect ratio.
*
* If the proportions of the original asset do not match the given width and height, padding is added to the asset
* to reach the required size.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizePadAction}
*/
function pad(width, height) {
return new ResizePadAction.ResizePadAction('pad', width, height);
}
/**
* @summary action
* @description
* Creates an asset with the exact given width and height without distorting the asset, but only if the original
* asset is larger than the specified resolution limits.
*
* The asset is scaled down to fill the given width and height without distorting the asset, and then the dimension
* that exceeds the request is cropped. If the original dimensions are both smaller than the requested size, it is
* not resized at all.
*
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeLimitFillAction}
*/
function limitFill(width, height) {
return new ResizeLimitFillAction.ResizeLimitFillAction('lfill', width, height);
}
/**
* @summary action
* @description
* Resizes the asset so that it takes up as much space as possible within a bounding box defined by the given
* width and height parameters, but only if the original asset is larger than the given limit (width and height).
*
* The asset is scaled down, the original aspect ratio is retained and all of the original asset is visible.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeSimpleAction}
*/
function limitFit(width, height) {
return new ResizeLimitFitAction.ResizeLimitFitAction('limit', width, height);
}
/**
* @summary action
* @description
* Resizes the asset to fill the given width and height while retaining the original aspect ratio, but only if the
* original asset is smaller than the given minimum (width and height).
*
* The asset is scaled up. If the proportions of the original asset do not match the given width and height,
* padding is added to the asset to reach the required size.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizePadAction}
*/
function minimumPad(width, height) {
return new ResizeMinimumPadAction.ResizeMinimumPadAction('mpad', width, height);
}
/**
* @summary action
* @description
* Resizes the asset so that it takes up as much space as possible within a bounding box defined by the given
* width and height parameters, but only if the original asset is smaller than the given minimum (width and height).
*
* The asset is scaled up, the original aspect ratio is retained and all of the original asset is visible.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeSimpleAction}
*/
function minimumFit(width, height) {
return new ResizeSimpleAction.ResizeSimpleAction('mfit', width, height);
}
/**
* @summary action
* @memberOf Actions.Resize
* @description
* Tries to prevent a "bad crop" by first attempting to use the fill mode, but adding padding if it is determined
* that more of the original image needs to be included in the final image.
*
* Especially useful if the aspect ratio of the delivered image is considerably different from the original's
* aspect ratio.
*
* Only supported in conjunction with Automatic cropping.
*
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizePadAction}
*/
function fillPad(width, height) {
return new ResizePadAction.ResizePadAction('fill_pad', width, height);
}
/**
* @summary action
* @description
* The thumb cropping mode is specifically used for creating image thumbnails from either face or custom coordinates,</br>
* and must always be accompanied by the gravity parameter set to one of the face detection or custom values.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ThumbResizeAction}
*/
function thumbnail(width, height) {
return new ThumbnailAction.ThumbResizeAction('thumb', width, height);
}
/**
* @summary action
* @description
* Automatically determines the best crop based on the gravity and specified dimensions.
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ThumbResizeAction}
*/
function auto(width, height) {
return new ResizeAdvancedAction.ResizeAdvancedAction('auto', width, height);
}
/**
* @summary action
* @description
* Resizes the asset to fill the given width and height while retaining the original aspect ratio, but only if the
* original asset is larger than the given limit (width and height).
*
* The asset is scaled down. If the proportions of the original asset do not match the given width and height,
* padding is added to the asset to reach the required size.
*
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizePadAction}
*/
function limitPad(width, height) {
return new ResizeLimitPadAction.ResizeLimitPadAction('lpad', width, height);
}
/**
* @summary action
* @description
* Tries to prevent a "bad crop" by first attempting to use the auto cropping mode, but adding some padding
* if the algorithm determines that more of the original image needs to be included in the final image.
*
* @memberOf Actions.Resize
* @param {number|string} width The required width of a transformed asset.
* @param {number|string} height The required height of a transformed asset.
* @return {Actions.Resize.ResizeAutoPadAction}
*/
function autoPad(width, height) {
return new ResizeAutoPadAction('auto_pad', width, height);
}
const Resize = {
imaggaScale,
imaggaCrop,
crop,
fill,
scale,
minimumPad,
fit,
pad,
limitFit,
thumbnail,
limitFill,
minimumFit,
limitPad,
fillPad,
auto,
autoPad
};
exports.Resize = Resize;
exports.auto = auto;
exports.autoPad = autoPad;
exports.crop = crop;
exports.fill = fill;
exports.fillPad = fillPad;
exports.fit = fit;
exports.imaggaCrop = imaggaCrop;
exports.imaggaScale = imaggaScale;
exports.limitFill = limitFill;
exports.limitFit = limitFit;
exports.limitPad = limitPad;
exports.minimumFit = minimumFit;
exports.minimumPad = minimumPad;
exports.pad = pad;
exports.scale = scale;
exports.thumbnail = thumbnail;