Resize the given BufferedImage.

@param originalImage
the original image
@param formatName
the format name
@param targetWidth
the target width
@param targetHeight
the target height
@return the byte[]
709
public static byte[] resize(final BufferedImage originalImage, final String formatName,
		final int targetWidth, final int targetHeight)
	{
		return resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, formatName,
			targetWidth, targetHeight);
	}
1009546
public static BufferedImage randomBufferedImage(final int width, final int height,
		final int imageType)
	{
		final BufferedImage img = new BufferedImage(width, height, imageType);
		for (int y = 0; y < height; y++)
		{
			for (int x = 0; x < width; x++)
			{
				img.setRGB(x, y, RandomExtensions.newRandomPixel());
			}
		}
		return img;
	}
1185685
def _heighten(self, resource):
        
        original_width, original_height = resource.size

        target_height = self.value
        target_width = int((float(target_height) / original_height) * original_width)

        resource = resource.resize((target_width, target_height), Image.ANTIALIAS)

        return resource
185024
protected function getEntropyOffsets(\Imagick $original, $targetWidth, $targetHeight)
    {
        $measureImage = clone($original);
        // Enhance edges
        $measureImage->edgeimage(1);
        // Turn image into a grayscale
        $measureImage->modulateImage(100, 0, 100);
        // Turn everything darker than this to pitch black
        $measureImage->blackThresholdImage("#070707");
        // Get the calculated offset for cropping
        return $this->getOffsetFromEntropy($measureImage, $targetWidth, $targetHeight);
    }
1036102
public function resize($width, $height)
    {
        $this->mode = 'resize';
        $this->targetSize = [$width, $height];
        $this->arguments = [];

        return $this->process();
    }
849443
protected function doResize($bg, $target_width, $target_height, $new_width, $new_height)
    {
        $width = $this->width();
        $height = $this->height();
        $n = imagecreatetruecolor($target_width, $target_height);

        if ($bg != 'transparent') {
            imagefill($n, 0, 0, ImageColor::gdAllocate($this->resource, $bg));
        } else {
            imagealphablending($n, false);
            $color = ImageColor::gdAllocate($this->resource, 'transparent');

            imagefill($n, 0, 0, $color);
            imagesavealpha($n, true);
        }

        imagecopyresampled($n, $this->resource, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height);
        imagedestroy($this->resource);

        $this->resource = $n;

        return $this;
    }
97389
private function verifySize($originalWidth, $originalHeight, $targetWidth, $targetHeight)
    {
        if ($originalWidth < $targetWidth && $originalHeight < $targetHeight) {
            return array(
                'width' => $originalWidth,
                'height' => $originalHeight,
            );
        }

        return array(
            'width' => $targetWidth,
            'height' => $targetHeight,
        );
    }
1062253
public static byte[] writeImageToBytes(BufferedImage image,
			String formatName) throws IOException {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		ImageIO.write(image, formatName, stream);
		stream.flush();
		byte[] bytes = stream.toByteArray();
		stream.close();
		return bytes;
	}
373452
def resize(image, target_size, **kwargs):
    """"""

    if isinstance(target_size, int):
        target_size = (target_size, target_size)

    if not isinstance(target_size, (list, tuple, np.ndarray)):
        message = (
            "`target_size` should be a single number (width) or a list"
            "/tuple/ndarray (width, height), not {}.".format(type(target_size))
        )
        raise ValueError(message)

    rank = len(image.shape)
    assert 3 <= rank <= 4

    original_size = image.shape[-3:-1]

    if original_size == target_size:
        return image  # noop return because ndimage.zoom doesn't check itself

    # TODO: maybe allow -1 in target_size to signify aspect-ratio preserving resize?
    ratios = [t / o for t, o in zip(target_size, original_size)]
    zoom = [1] * rank
    zoom[-3:-1] = ratios

    roughly_resized = ndimage.zoom(image, zoom, **kwargs)
    return roughly_resized[..., : target_size[0], : target_size[1], :]
1181232
protected function getTargetDimensions($width, $height, $originalImagePath)
    {
        $originalDimensions = [$width, $height];

        try {
            $dimensions = getimagesize($originalImagePath);
            if (!$dimensions) {
                return $originalDimensions;
            }

            if ($dimensions[0] > $width || $dimensions[1] > $height) {
                return $originalDimensions;
            }

            return $dimensions;
        }
        catch (Exception $ex) {
            return $originalDimensions;
        }
    }
283390
protected function calculateTargetSize($params, $sourceWidth, $sourceHeight): array
    {
        $fit = $params['fit']; // clamp, clip, crop, facearea, fill, fillmax, max, min, and scale. 
        $ratio = $sourceWidth / $sourceHeight;

        $w = $params['w'] ?? null;
        $h = $params['h'] ?? null;

        switch ($fit) {
            case 'clip':
            case 'fill':
            case 'crop':
            case 'clamp':
            case 'scale':
                if ($w) {
                    return [$w, round($w / $ratio)];
                }
                if ($h) {
                    return [round($h * $ratio), $h];
                }
                break;
            case 'min':
            case 'max':
                if ($w) {
                    $useWidth = min($w, $sourceWidth);

                    return [$useWidth, round($useWidth / $ratio)];
                }
                if ($h) {
                    $useHeigth = min($h, $sourceHeight);

                    return [round($useHeigth * $ratio), $useHeigth];
                }
                break;
        }

        return [$w ?: 0, $h ?: 0];
    }
887738
protected function calculateDimensions($originalWidth, $originalHeight)
	{
		// check if target size is set and valid
		if (empty($this->targetWidth) || ($this->targetWidth <= 0)) {
			throw new ImageProcessorException('Target width is not set or is invalid');
		}
		if (empty($this->targetHeight) || ($this->targetHeight <= 0)) {
			throw new ImageProcessorException('Target height is not set or is invalid');
		}

		$newDimensions = $this->getExpectedSize(false);
		
		$dimensions = array();
		
		// set default dimensions for image-to-image copy
		$dimensions['sourceLeft'] = null;
		$dimensions['sourceTop'] = null;
		$dimensions['sourceWidth'] = null;
		$dimensions['sourceHeight'] = null;
		
		$dimensions['destWidth'] = max(round($newDimensions['width']), 1);
		$dimensions['destHeight'] = max(round($newDimensions['height']), 1);

		// get ratios 
		$wRatio = max($originalWidth / $newDimensions['width'], 1);
		$hRatio = max($originalHeight / $newDimensions['height'], 1);

		if ( ! $this->cropMode) {
			$wRatio = $hRatio = max($wRatio, $hRatio);
		} else {
			$wRatio = $hRatio = min($wRatio, $hRatio);
		}

		// set source dimensions to center (with target aspect ratio)
		$sourceHeight = $newDimensions['height'] * $hRatio;
		$dimensions['sourceTop'] =
				round(($originalHeight - $sourceHeight) / 2);
		$dimensions['sourceHeight'] = round($sourceHeight);

		$sourceWidth = $newDimensions['width'] * $wRatio;
		$dimensions['sourceLeft'] =
				round(($originalWidth - $sourceWidth) / 2);
		$dimensions['sourceWidth'] = round($sourceWidth);

		return $dimensions;
	}
1212687
private function resize($newWidth, $newHeight, $srcImg, $filename)
    {
        $oldX = imagesx($srcImg);
        $oldY = imagesy($srcImg);

        if ($oldX > $oldY) {
            $thumbWidth = $newWidth;
            $thumbHeight = $oldY * ($newHeight / $oldX);
        } else {
            if ($oldX <= $oldY) {
                $thumbWidth = $oldX * ($newWidth / $oldY);
                $thumbHeight = $newHeight;
            }
        }

        //white background
        $dstImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
        $bg = imagecolorallocate($dstImg, 255, 255, 255);
        imagefill($dstImg, 0, 0, $bg);

        //resizing
        imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
        $srcImg = imagepng($dstImg, $filename);

        //free memory
        imagedestroy($dstImg);
    }
404140
public static byte[] scaleImage(byte[] imageBytes, String outputFormat,
                                    Integer targetSize) throws Exception {

        InputStream is = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] imageInBytes = null;
        BufferedImage image = null;
        BufferedImage resizedImage = null;

        try {
            is = new ByteArrayInputStream(imageBytes);
            image = ImageIO.read(is);
            resizedImage = Scalr.resize(image, targetSize);
            ImageIO.write(resizedImage, outputFormat, baos);
            imageInBytes = baos.toByteArray();
        } catch (Exception e) {
            throw new Exception("Error on resizing an image: " + e.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(baos);
            if (image != null) {
                image.flush();
            }
            if (resizedImage != null) {
                resizedImage.flush();
            }
        }
        return imageInBytes;
    }
816689
function getTargetSize(size, buffer) {
  size *= 1000000; // Converting to MB to B.

  // 10% is default.
  buffer = 1 + (buffer ? buffer / 100 : 0.1);

  return Math.round(size * buffer);
}
1117013
public CmsJspImageBean createWidthVariation(String widthStr) {

        CmsJspImageBean result = null;

        try {

            double baseRatio;
            if ((getOriginalScaler().getFocalPoint() != null)
                && checkCropRegionContainsFocalPoint(getScaler(), getOriginalScaler().getFocalPoint())) {
                // We use scaling mode 8 if there is a focal point, and in this case,
                // the correct aspect ratio is width x height, not cropWidth x cropHeight
                // even if cropping is set
                baseRatio = (double)getScaler().getWidth() / (double)getScaler().getHeight();
            } else if (getScaler().isCropping()) {
                // use the image crop with/height for aspect ratio calculation
                baseRatio = (double)getScaler().getCropWidth() / (double)getScaler().getCropHeight();
            } else {
                // use the image original pixel width/height for aspect ratio calculation
                baseRatio = (double)getScaler().getWidth() / (double)getScaler().getHeight();
            }

            // height is not set, calculate it with the given width and the aspect ratio
            int targetWidth = Integer.valueOf(widthStr).intValue();
            int targetHeight = (int)Math.round(targetWidth / baseRatio);

            CmsImageScaler targetScaler = createVariation(
                getWidth(),
                getHeight(),
                getBaseScaler(),
                targetWidth,
                targetHeight,
                getQuality());

            if (targetScaler != null) {
                result = createVariation(targetScaler);
            }

        } catch (NumberFormatException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn(String.format("Illegal width format: '%s' not usable for image scaling", widthStr));
            }
        }

        return result;
    }
564007
public static BufferedImage compression(BufferedImage src, int widthScale, int heightScale) {
        int oldWidth = src.getWidth();
        int oldHeight = src.getHeight();
        int width = oldWidth / widthScale + ((oldWidth % widthScale) > 0 ? 1 : 0);
        int height = oldHeight / heightScale + ((oldHeight % heightScale) > 0 ? 1 : 0);

        BufferedImage dest = new BufferedImage(width, height, src.getType());

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgb = src.getRGB(x * widthScale, y * heightScale);
                dest.setRGB(x, y, rgb);
            }
        }

        return dest;
    }