From feb70c8f52879b5bac3ad6b99d61384cda7b9489 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Tue, 27 Apr 2021 13:23:22 +0300 Subject: [PATCH 1/2] OSS Pixelate option (#622) --- docs/generating_the_url.md | 2 +- process.go | 7 ++++++ processing_options.go | 17 +++++++++++++ vips.c | 49 ++++++++++++++++++++++++++++++++++++++ vips.go | 12 ++++++++++ vips.h | 2 ++ 6 files changed, 88 insertions(+), 1 deletion(-) diff --git a/docs/generating_the_url.md b/docs/generating_the_url.md index 197e23f8..aecfd76d 100644 --- a/docs/generating_the_url.md +++ b/docs/generating_the_url.md @@ -350,7 +350,7 @@ As an approximate guideline, use 0.5 sigma for 4 pixels/mm (display resolution), Default: disabled -### Pixelatepro +### Pixelate ``` pixelate:%size diff --git a/process.go b/process.go index 2cc38707..0f7a6f41 100644 --- a/process.go +++ b/process.go @@ -571,6 +571,13 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces } } + if po.Pixelate > 1 { + pixels := minInt(po.Pixelate, minInt(img.Width(), img.Height())) + if err = img.Pixelate(pixels); err != nil { + return err + } + } + if err = copyMemoryAndCheckTimeout(ctx, img); err != nil { return err } diff --git a/processing_options.go b/processing_options.go index 995d5207..5e926c27 100644 --- a/processing_options.go +++ b/processing_options.go @@ -149,6 +149,7 @@ type processingOptions struct { Background rgbColor Blur float32 Sharpen float32 + Pixelate int StripMetadata bool StripColorProfile bool AutoRotate bool @@ -807,6 +808,20 @@ func applySharpenOption(po *processingOptions, args []string) error { return nil } +func applyPixelateOption(po *processingOptions, args []string) error { + if len(args) > 1 { + return fmt.Errorf("Invalid pixelate arguments: %v", args) + } + + if p, err := strconv.Atoi(args[0]); err == nil && p >= 0 { + po.Pixelate = p + } else { + return fmt.Errorf("Invalid pixelate: %s", args[0]) + } + + return nil +} + func applyPresetOption(po *processingOptions, args []string) error { for _, preset := range args { if p, ok := conf.Presets[preset]; ok { @@ -1014,6 +1029,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er return applyBlurOption(po, args) case "sharpen", "sh": return applySharpenOption(po, args) + case "pixelate", "pix": + return applyPixelateOption(po, args) case "watermark", "wm": return applyWatermarkOption(po, args) case "strip_metadata", "sm": diff --git a/vips.c b/vips.c index 4ddad480..5efd7e98 100644 --- a/vips.c +++ b/vips.c @@ -262,6 +262,55 @@ vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double wscale, doub return 0; } +int +vips_pixelate(VipsImage *in, VipsImage **out, int pixels) { + VipsImage *base = vips_image_new(); + VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3); + + int w, h, tw, th; + + w = in->Xsize; + h = in->Ysize; + + tw = (int)((double)(w + pixels - 1) / pixels) * pixels; + th = (int)((double)(h + pixels - 1) / pixels) * pixels; + + if (tw > w || th > h) { + if (vips_embed(in, &t[0], 0, 0, tw, th, "extend", VIPS_EXTEND_COPY, NULL)) { + clear_image(&base); + return 1; + } + } else { + if (vips_copy(in, &t[0], NULL)) { + clear_image(&base); + return 1; + } + } + + if ( + vips_shrink(t[0], &t[1], pixels, pixels, NULL) || + vips_zoom(t[1], &t[2], pixels, pixels, NULL) + ) { + clear_image(&base); + return 1; + } + + if (tw > w || th > h) { + if (vips_extract_area(t[2], out, 0, 0, w, h, NULL)) { + clear_image(&base); + return 1; + } + } else { + if (vips_copy(t[2], out, NULL)) { + clear_image(&base); + return 1; + } + } + + clear_image(&base); + return 0; +} + int vips_icc_is_srgb_iec61966(VipsImage *in) { const void *data; diff --git a/vips.go b/vips.go index b814834d..ab581b16 100644 --- a/vips.go +++ b/vips.go @@ -421,6 +421,18 @@ func (img *vipsImage) Resize(wscale, hscale float64, hasAlpa bool) error { return nil } +func (img *vipsImage) Pixelate(pixels int) error { + var tmp *C.VipsImage + + if C.vips_pixelate(img.VipsImage, &tmp, C.int(pixels)) != 0 { + return vipsError() + } + + C.swap_and_clear(&img.VipsImage, tmp) + + return nil +} + func (img *vipsImage) Orientation() C.int { return C.vips_get_orientation(img.VipsImage) } diff --git a/vips.h b/vips.h index 522508cf..a945c19a 100644 --- a/vips.h +++ b/vips.h @@ -58,6 +58,8 @@ int vips_rad2float_go(VipsImage *in, VipsImage **out); int vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale); int vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double wscale, double hscale); +int vips_pixelate(VipsImage *in, VipsImage **out, int pixels); + int vips_icc_is_srgb_iec61966(VipsImage *in); int vips_has_embedded_icc(VipsImage *in); int vips_icc_import_go(VipsImage *in, VipsImage **out); From 9465d04afba220487d1702099f817fc169d8f785 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Mon, 17 May 2021 18:46:28 +0600 Subject: [PATCH 2/2] Rm forgotten doc --- docs/generating_the_url_basic.md | 106 ------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 docs/generating_the_url_basic.md diff --git a/docs/generating_the_url_basic.md b/docs/generating_the_url_basic.md deleted file mode 100644 index da7722c3..00000000 --- a/docs/generating_the_url_basic.md +++ /dev/null @@ -1,106 +0,0 @@ -# Generating the URL (Basic) - -This guide describes the simple URL format that is easy to use but doesn't support the whole range of imgproxy features. This URL format is mostly supported for backwards compatibility with imgproxy 1.x. Please read our [Generating the URL (Advanced)](generating_the_url_advanced.md) guide to learn about the advanced URL format. - -## Format definition - -The basic URL should contain the signature, resize parameters, and source URL, like this: - -``` -/%signature/%resizing_type/%width/%height/%gravity/%enlarge/plain/%source_url@%extension -/%signature/%resizing_type/%width/%height/%gravity/%enlarge/%encoded_source_url.%extension -``` - -Check out the [example](#example) at the end of this guide. - -### Signature - -Signature protects your URL from being modified by an attacker. It is highly recommended to sign imgproxy URLs in a production environment. - -Once you set up your [URL signature](configuration.md#url-signature), check out the [Signing the URL](signing_the_url.md) guide to learn about how to sign your URLs. Otherwise, use any string here. - -### Resizing types - -imgproxy supports the following resizing types: - -* `fit`: resizes the image while keeping aspect ratio to fit given size; -* `fill`: resizes the image while keeping aspect ratio to fill given size and cropping projecting parts; -* `auto`: if both source and resulting dimensions have the same orientation (portrait or landscape), imgproxy will use `fill`. Otherwise, it will use `fit`. - -### Width and height - -Width and height parameters define the size of the resulting image in pixels. Depending on the resizing type applied, the dimensions may differ from the requested ones. - -### Gravity - -When imgproxy needs to cut some parts of the image, it is guided by the gravity. The following values are supported: - -* `no`: north (top edge); -* `so`: south (bottom edge); -* `ea`: east (right edge); -* `we`: west (left edge); -* `noea`: north-east (top-right corner); -* `nowe`: north-west (top-left corner); -* `soea`: south-east (bottom-right corner); -* `sowe`: south-west (bottom-left corner); -* `ce`: center; -* `sm`: smart. `libvips` detects the most "interesting" section of the image and considers it as the center of the resulting image; -* `fp:%x:%y` - focus point. `x` and `y` are floating point numbers between 0 and 1 that describe the coordinates of the center of the resulting image. Treat 0 and 1 as right/left for `x` and top/bottom for `y`. - -### Enlarge - -When set to `1`, `t` or `true`, imgproxy will enlarge the image if it is smaller than the given size. - -### Source URL - -There are two ways to specify source url: - -#### Plain - -The source URL can be provided as is, prepended by `/plain/` part: - -``` -/plain/http://example.com/images/curiosity.jpg -``` - -**📝Note:** If the source URL contains query string or `@`, you need to escape it. - -When using plain source URL, you can specify the [extension](#extension) after `@`: - -``` -/plain/http://example.com/images/curiosity.jpg@png -``` - -#### Base64 encoded - -The source URL can be encoded with URL-safe Base64. The encoded URL can be split with `/` for your needs: - -``` -/aHR0cDovL2V4YW1w/bGUuY29tL2ltYWdl/cy9jdXJpb3NpdHku/anBn -``` - -When using encoded source URL, you can specify the [extension](#extension) after `.`: - -``` -/aHR0cDovL2V4YW1w/bGUuY29tL2ltYWdl/cy9jdXJpb3NpdHku/anBn.png -``` - -### Extension - -Extension specifies the format of the resulting image. Read about image formats support [here](image_formats_support.md). - -The extension part can be omitted. In this case, imgproxy will use source image format as resulting one. If source image format is not supported as resulting, imgproxy will use `jpg`. You also can [enable WebP support detection](configuration.md#webp-support-detection) to use it as default resulting format when possible. - -## Example - -Signed imgproxy URL that resizes `http://example.com/images/curiosity.jpg` to fill `300x400` area with smart gravity without enlarging, and converts the image to `png`: - -``` -http://imgproxy.example.com/AfrOrF3gWeDA6VOlDG4TzxMv39O7MXnF4CXpKUwGqRM/fill/300/400/sm/0/plain/http://example.com/images/curiosity.jpg@png -``` - -The same URL with Base64-encoded source URL will look like this: - -``` -http://imgproxy.example.com/AfrOrF3gWeDA6VOlDG4TzxMv39O7MXnF4CXpKUwGqRM/fill/300/400/sm/0/aHR0cDovL2V4YW1w/bGUuY29tL2ltYWdl/cy9jdXJpb3NpdHku/anBn.png -```