image-resizing
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -24,6 +24,28 @@ "use strict"; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function createHandler(options) { | ||
var _parseBucket = (0, _parse.parseBucket)(options.sourceBucket), | ||
_parseBucket2 = _slicedToArray(_parseBucket, 2), | ||
sourceBucketName = _parseBucket2[0], | ||
sourcePrefix = _parseBucket2[1]; | ||
var _parseBucket3 = (0, _parse.parseBucket)(options.cacheBucket), | ||
_parseBucket4 = _slicedToArray(_parseBucket3, 2), | ||
cacheBucketName = _parseBucket4[0], | ||
cachePrefix = _parseBucket4[1]; | ||
var storage = new _storage.Storage(options.storage); | ||
var sourceBucket = storage.bucket(options.sourceBucket); | ||
var cacheBucket = storage.bucket(options.cacheBucket); | ||
var sourceBucket = storage.bucket(sourceBucketName); | ||
var cacheBucket = storage.bucket(cacheBucketName); | ||
var cacheControl = "public, max-age=31560000, immutable"; | ||
@@ -44,3 +66,3 @@ var cacheControlInitial = "public, max-age=31560000, s-maxage=0, immutable"; | ||
var sourceFile = target ? cacheBucket.file(target) : sourceBucket.file(source); | ||
var sourceFile = target ? cacheBucket.file("".concat(cachePrefix).concat(target)) : sourceBucket.file("".concat(sourcePrefix).concat(source)); | ||
sourceFile.createReadStream({ | ||
@@ -56,3 +78,3 @@ decompress: false | ||
this.end(); | ||
sourceBucket.file(source).createReadStream().on("error", _utils.noop).on("response", function (x) { | ||
sourceBucket.file("".concat(sourcePrefix).concat(source)).createReadStream().on("error", _utils.noop).on("response", function (x) { | ||
if (x.statusCode === 200) { | ||
@@ -64,3 +86,3 @@ res.set("Cache-Control", cacheControlInitial); | ||
} else { | ||
var targetFile = cacheBucket.file(target).createWriteStream({ | ||
var targetFile = cacheBucket.file("".concat(cachePrefix).concat(target)).createWriteStream({ | ||
contentType: x.headers["content-type"] | ||
@@ -67,0 +89,0 @@ }).on("error", function (err) { |
@@ -6,1 +6,11 @@ /** | ||
export declare function parseUrlPath(path: string, params: Readonly<Params>): ParsedOutput; | ||
/** | ||
* Extracts folder name (prefix) from the bucket name. | ||
* | ||
* @example | ||
* parseBucket("gs://s.example.com") => ["gs://s.example.com", ""] | ||
* parseBucket("gs://s.example.com/") => ["gs://s.example.com", ""] | ||
* parseBucket("gs://s.example.com/uploads") => ["gs://s.example.com", "uploads/"] | ||
* parseBucket("gs://s.example.com/uploads/") => ["gs://s.example.com", "uploads/"] | ||
*/ | ||
export declare function parseBucket(value: string): [bucket: string, prefix: string]; |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.parseUrlPath = parseUrlPath; | ||
exports.parseBucket = parseBucket; | ||
@@ -105,2 +106,28 @@ var _path = require("path"); | ||
} | ||
/** | ||
* Extracts folder name (prefix) from the bucket name. | ||
* | ||
* @example | ||
* parseBucket("gs://s.example.com") => ["gs://s.example.com", ""] | ||
* parseBucket("gs://s.example.com/") => ["gs://s.example.com", ""] | ||
* parseBucket("gs://s.example.com/uploads") => ["gs://s.example.com", "uploads/"] | ||
* parseBucket("gs://s.example.com/uploads/") => ["gs://s.example.com", "uploads/"] | ||
*/ | ||
function parseBucket(value) { | ||
var protocol = function (i) { | ||
return i === -1 ? "gs:" : value.substring(0, i + 1); | ||
}(value.indexOf("://")); | ||
if (protocol !== "gs:") { | ||
throw new Error("Only Google Storage buckets are supported at the moment."); | ||
} | ||
return function (i) { | ||
return i === -1 ? [value, ""] : [value.substring(0, i), function (x) { | ||
return x && (x.endsWith("/") ? x : "".concat(x, "/")); | ||
}(value.substring(i + 1))]; | ||
}(value.indexOf("/", protocol ? protocol.length + 2 : 0)); | ||
} | ||
//# sourceMappingURL=parse.js.map |
{ | ||
"name": "image-resizing", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Node.js backend (middleware) for image manipulation needs (transform, resize, optimize).", | ||
@@ -22,3 +22,2 @@ "keywords": [ | ||
"license": "MIT", | ||
"homepage": "https://github.com/kriasoft/image-resizing/blob/main/image-resizing", | ||
"bugs": "https://github.com/kriasoft/image-resizing/issues", | ||
@@ -25,0 +24,0 @@ "repository": "github:kriasoft/image-resizing", |
@@ -25,2 +25,3 @@ # Cloud Image Resizing · [![npm package][npm-badge]][npm] | ||
sourceBucket: "s.example.com", | ||
// Where the transformed images needs to be stored. | ||
@@ -34,8 +35,62 @@ // E.g. gs://c.example.com/image__w_80,h_60.jpg | ||
``` | ||
https://example.com/image.jpg - original image | ||
https://example.com/w_80,h_60,c_fill/image.jpg - resized image (80x60) | ||
https://example.com/x_10,y_10,w_80,h_60,c_crop/image.jpg - cropped image (80x60 at 10,10 offset) | ||
``` | ||
## Resizing and cropping images | ||
You can resize and crop images in order to match the graphic design of your web | ||
site or mobile application. Whether images are uploaded in your server-side code | ||
or by your users, the original hi-res images are stored in the cloud for further | ||
processing and management. You can then dynamically create multiple resized, | ||
cropped and manipulated images on-the-fly and deliver them via dynamic URLs. | ||
To change the size of a image, use the `width` and `height` parameters (`w` and | ||
`h` in URLs) to assign new values. You can resize the image by using both the | ||
width and height parameters or with only one of them: the other dimension is | ||
automatically updated to maintain the aspect ratio. | ||
Examples of resizing the uploaded jpg image named `sample`: | ||
1. Resizing the height to 200 pixels, maintaining the aspect ratio: | ||
<p align="center"> | ||
<img src="https://i.kriasoft.com/h_200/sample.jpg" /><br> | ||
<code><a href="https://i.kriasoft.com/h_200/sample.jpg">https://i.kriasoft.com/h_200/sample.jpg</a></code> | ||
</p> | ||
2. Resizing to a width of 200 pixels and a height of 100 pixels: | ||
<p align="center"> | ||
<img src="https://i.kriasoft.com/w_200,h_100/sample.jpg" /><br> | ||
<code><a href="https://i.kriasoft.com/w_200,h_100/sample.jpg">https://i.kriasoft.com/w_200,h_100/sample.jpg</a></code> | ||
</p> | ||
## Fixed coordinates cropping | ||
You can specify a region of the original image to crop by giving the `x` and `y` | ||
coordinates of the top left corner of the region together with the `width` and | ||
`height` of the region. You can also use percentage based numbers instead of the | ||
exact coordinates for `x`, `y`, `w` and `h` (e.g., 0.5 for 50%) . Use this | ||
method when you know beforehand what the correct absolute cropping coordinates | ||
are, as in when your users manually select the region to crop out of the | ||
original image. | ||
For example, the following image shows many white sheep and one brown sheep. | ||
<p align="center"> | ||
<img src="https://i.kriasoft.com/brown_sheep.jpg" /><br> | ||
<code><a href="https://i.kriasoft.com/brown_sheep.jpg">https://i.kriasoft.com/brown_sheep.jpg</a></code> | ||
</p> | ||
To manipulate the picture so that only the brown sheep is visible, the image is cropped to a 300x200 region starting at the coordinate x = 355 and y = 410: | ||
<p align="center"> | ||
<img src="https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/brown_sheep.jpg" /><br> | ||
<code><a href="https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/brown_sheep.jpg">https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/brown_sheep.jpg</a></code> | ||
</p> | ||
The image can be further manipulated with chained transformations. For example, the 300x200 cropped version above, also scaled down to 150x100: | ||
<p align="center"> | ||
<img src="https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/w_150,h_100,c_scale/brown_sheep.jpg" /><br> | ||
<code><a href="https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/w_150,h_100,c_scale/brown_sheep.jpg">https://i.kriasoft.com/x_355,y_410,w_300,h_200,c_crop/w_150,h_100,c_scale/brown_sheep.jpg</a></code> | ||
</p> | ||
## References | ||
@@ -54,4 +109,5 @@ | ||
Copyright © 2020-present Kriasoft. This source code is licensed under the MIT license found in the | ||
[LICENSE](https://github.com/kriasoft/image-resizing/blob/main/LICENSE) file. | ||
Copyright © 2020-present Kriasoft. This source code is licensed under the MIT | ||
license found in the [LICENSE](https://github.com/kriasoft/image-resizing/blob/main/LICENSE) | ||
file. Sample images and transformation options are borrowed from Cloudinary. | ||
@@ -58,0 +114,0 @@ --- |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45698
387
115