Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

upload-widget: Resize image after cropping.

Sahil Batracb9e95bbca5c
diff --git a/package.json b/package.json
index cfdd1af4f60..188508b64fa 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"clipboard": "^2.0.4",
"colord": "^2.9.3",
"compression-webpack-plugin": "^11.1.0",
+ "compressorjs": "^1.2.1",
"core-js": "^3.48.0",
"css-loader": "^7.1.1",
"css-minimizer-webpack-plugin": "^7.0.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 212f8f75375..bb00fc61dce 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -119,6 +119,9 @@ importers:
compression-webpack-plugin:
specifier: ^11.1.0
version: 11.1.0([email protected])
+ compressorjs:
+ specifier: ^1.2.1
+ version: 1.2.1
core-js:
specifier: ^3.48.0
version: 3.48.0
@@ -3684,6 +3687,9 @@ packages:
resolution: {integrity: sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==}
+ resolution: {integrity: sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg==}
+
resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==}
@@ -4048,6 +4054,9 @@ packages:
resolution: {integrity: sha512-9mAqGPHLakhCLeNyxPkK4xVo746zQ/czLH1Ky+vkitMnWfWZps8r0qXuwhwizagCRttsL4lfG4pIOvaWLpAP0w==}
engines: {node: '>= 0.8.0'}
+ resolution: {integrity: sha512-+geIjeRnPhQ+LLvvA7wxBQE5ddeLU7pJ3FsKFWirDw6veY3s9iLxAQEw7lXGHnhCJvBujEQWuNnGzZcvCvdkLQ==}
+
resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
@@ -5852,6 +5861,10 @@ packages:
resolution: {integrity: sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==}
+ engines: {node: '>=6'}
+
resolution: {integrity: sha512-wa56o2/ElJMYqjCjGkXri7it5FbebW5usLw/nPmCMs5DeZ7eziSYZhSmPRn0txqeW4LnAmQQU7FgqLpsEFKM4A==}
engines: {node: '>= 0.4'}
@@ -13568,6 +13581,8 @@ snapshots:
readable-stream: 2.3.8
safe-buffer: 5.2.1
+
@@ -13978,6 +13993,11 @@ snapshots:
transitivePeerDependencies:
- supports-color
+ dependencies:
+ blueimp-canvas-to-blob: 3.29.0
+ is-blob: 2.1.0
+
@@ -16245,6 +16265,8 @@ snapshots:
dependencies:
binary-extensions: 2.3.0
+
dependencies:
call-bound: 1.0.4
diff --git a/version.py b/version.py
index 7d61d443263..05664386cc9 100644
--- a/version.py
+++ b/version.py
@@ -50,4 +50,4 @@ API_FEATURE_LEVEL = 459
# historical commits sharing the same major version, in which case a
# minor version bump suffices.
-PROVISION_VERSION = (369, 0) # bumped 2026-02-03 to upgrade Python requirements
+PROVISION_VERSION = (369, 1) # bumped 2026-02-02 to add compressorjs
diff --git a/web/src/upload_widget.ts b/web/src/upload_widget.ts
index c4154bf69b7..26c59ab859e 100644
--- a/web/src/upload_widget.ts
+++ b/web/src/upload_widget.ts
@@ -1,10 +1,12 @@
import Uppy from "@uppy/core";
import type {Body, Meta} from "@uppy/core";
import ImageEditor from "@uppy/image-editor";
+import Compressor from "compressorjs";
import assert from "minimalistic-assert";
import render_image_editor_modal from "../templates/image_editor_modal.hbs";
+import * as blueslip from "./blueslip.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import * as util from "./util.ts";
@@ -145,6 +147,18 @@ export function build_widget(
};
}
+function ensure_file(resized_img: File | Blob, original_file: File): File {
+ // The resized image may be returned as either a Blob or a File.
+ // It usually returns a Blob, but can return a File in some cases
+ // (for example, when resizing is skipped or cannot be performed),
+ // so we convert it to a File since upload_function expects one.
+ if (resized_img instanceof File) {
+ return resized_img;
+ }
+
+ return new File([resized_img], original_file.name, {type: resized_img.type});
+}
+
function set_up_uppy_widget(property_name: "realm_icon" | "realm_logo" | "user_avatar"): void {
uppy_widget = new Uppy<Meta, Body>({
restrictions: {
@@ -209,16 +223,54 @@ function open_uppy_editor(
const uppy_file = uppy_widget.getFile(uppy_file_id);
uppy_widget.getPlugin<ImageEditor<Meta, Body>>("ImageEditor")!.selectFile(uppy_file);
+ let resizing_dimension_opts = {};
+ // The resizing dimensions should be kept in sync with the client-side
+ // resizing code in zerver/lib/thumbnail.py.
+ if (property_name === "user_avatar") {
+ resizing_dimension_opts = {
+ maxHeight: 500,
+ maxWidth: 500,
+ };
+ } else if (property_name === "realm_icon") {
+ resizing_dimension_opts = {
+ maxHeight: 100,
+ maxWidth: 100,
+ };
+ } else {
+ resizing_dimension_opts = {
+ maxHeight: 100,
+ maxWidth: 800,
+ };
+ }
+
uppy_widget.once("file-editor:complete", (file) => {
assert(file.data instanceof File);
+ let is_night = null;
+ let for_realm_icon = true;
if (property_name === "realm_logo") {
const $realm_logo_section = $upload_button.closest(".image_upload_widget");
- const is_night =
- $realm_logo_section.attr("id") === "realm-night-logo-upload-widget";
- upload_function(file.data, is_night, false);
- } else {
- upload_function(file.data, null, true);
+ is_night = $realm_logo_section.attr("id") === "realm-night-logo-upload-widget";
+ for_realm_icon = false;
}
+ new Compressor(file.data, {
+ // We do not set mimeType here because the source file is
+ // already a PNG. Uppy converts images to PNG, and the
+ // default value of "auto" for mimeType preserves the
+ // original image’s mime type.
+ ...resizing_dimension_opts,
+ success(result) {
+ assert(file.data instanceof File);
+ const resized_img = ensure_file(result, file.data);
+ upload_function(resized_img, is_night, for_realm_icon);
+ },
+ error(error) {
+ blueslip.warn(String(error));
+ // If there is some error during resizing, we just
+ // try to upload the image without resizing.
+ assert(file.data instanceof File);
+ upload_function(file.data, is_night, for_realm_icon);
+ },
+ });
});
},
on_hidden() {
diff --git a/zerver/lib/thumbnail.py b/zerver/lib/thumbnail.py
index e4e69d3032b..fbf43dc6b4a 100644
--- a/zerver/lib/thumbnail.py
+++ b/zerver/lib/thumbnail.py
@@ -204,6 +204,8 @@ def resize_avatar(image_data: bytes, size: int = DEFAULT_AVATAR_SIZE) -> bytes:
# This will scale up, if necessary, and will scale the smallest
# dimension to fit. That is, a 1x1000 image will end up with the
# one middle pixel enlarged to fill the full square.
+ # The resizing dimensions should be kept in sync with the client-side
+ # resizing code in web/upload_widget.ts.
with libvips_check_image(image_data):
return pyvips.Image.thumbnail_buffer(
image_data,
@@ -223,6 +225,8 @@ def resize_logo(image_data: bytes) -> bytes:
# pixels; it does not add any padding to make it exactly that
# size. A 1000x10 pixel image will end up as 800x8; a 10x10 will
# end up 10x10.
+ # The resizing dimensions should be kept in sync with the client-side
+ # resizing code in web/upload_widget.ts.
with libvips_check_image(image_data):
return pyvips.Image.thumbnail_buffer(
image_data,