Repository: xenova/transformers.js
File: src/utils/image.js (JavaScript)

Code before:
    /**
     * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing.
     * @param {number} width The width of the new image. `null` or `-1` will preserve the aspect ratio.
     * @param {number} height The height of the new image. `null` or `-1` will preserve the aspect ratio.
     * @param {Object} options Additional options for resizing.
     * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
     * @returns {Promise<RawImage>} `this` to support chaining.
     */
    async resize(width, height, {
        resample = 2,
    } = {}) {

        // Ensure resample method is a string
        let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample;

        // Calculate width / height to maintain aspect ratio, in the event that
        // the user passed a null value in.
        // This allows users to pass in something like `resize(320, null)` to
        // resize to 320 width, but maintain aspect ratio.
        if (isNullishDimension(width) && isNullishDimension(height)) {
            return this;
        } else if (isNullishDimension(width)) {
            width = (height / this.height) * this.width;
        } else if (isNullishDimension(height)) {
            height = (width / this.width) * this.height;
        }

        if (BROWSER_ENV) {
            // TODO use `resample` in browser environment

            // Store number of channels before resizing
            const numChannels = this.channels;

            // Create canvas object for this image
            const canvas = this.toCanvas();

            // Actually perform resizing using the canvas API
            const ctx = createCanvasFunction(width, height).getContext('2d');

            // Draw image to context, resizing in the process
            ctx.drawImage(canvas, 0, 0, width, height);

            // Create image from the resized data
            const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4);

            // Convert back so that image has the same number of channels as before
            return resizedImage.convert(numChannels);

        } else {
            // Create sharp image from raw data, and resize
            let img = this.toSharp();

Review comment (suggestion):
```suggestion
        const nullish_width = isNullishDimension(width);
        const nullish_height = isNullishDimension(height);
        if (nullish_width && nullish_height) {
            return this;
        } else if (nullish_width) {
            width = (height / this.height) * this.width;
        } else if (nullish_height) {
            height = (width / this.width) * this.height;
        }
```

Code after:
    /**
     * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing.
     * @param {number} width The width of the new image. `null` or `-1` will preserve the aspect ratio.
     * @param {number} height The height of the new image. `null` or `-1` will preserve the aspect ratio.
     * @param {Object} options Additional options for resizing.
     * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
     * @returns {Promise<RawImage>} `this` to support chaining.
     */
    async resize(width, height, {
        resample = 2,
    } = {}) {

        // Ensure resample method is a string
        let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample;

        // Calculate width / height to maintain aspect ratio, in the event that
        // the user passed a null value in.
        // This allows users to pass in something like `resize(320, null)` to
        // resize to 320 width, but maintain aspect ratio.
        const nullish_width = isNullishDimension(width);
        const nullish_height = isNullishDimension(height);
        if (nullish_width && nullish_height) {
            return this;
        } else if (nullish_width) {
            width = (height / this.height) * this.width;
        } else if (nullish_height) {
            height = (width / this.width) * this.height;
        }

        if (BROWSER_ENV) {
            // TODO use `resample` in browser environment

            // Store number of channels before resizing
            const numChannels = this.channels;

            // Create canvas object for this image
            const canvas = this.toCanvas();

            // Actually perform resizing using the canvas API
            const ctx = createCanvasFunction(width, height).getContext('2d');

            // Draw image to context, resizing in the process
            ctx.drawImage(canvas, 0, 0, width, height);

            // Create image from the resized data
            const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4);

            // Convert back so that image has the same number of channels as before
            return resizedImage.convert(numChannels);

        } else {