{"size": 2655, "ext": "js", "max_stars_repo_path": "src/downscaleImage.js", "max_stars_repo_name": "benwinding/quill-image-compress", "max_stars_repo_stars_event_min_datetime": "2019-11-12T14:46:38.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-28T16:00:19.000Z", "max_issues_repo_path": "src/downscaleImage.js", "max_issues_repo_name": "benwinding/quill-image-compress", "max_issues_repo_issues_event_min_datetime": "2019-08-08T00:05:31.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-16T08:19:06.000Z", "max_forks_repo_path": "src/downscaleImage.js", "max_forks_repo_name": "benwinding/quill-image-compress", "max_forks_repo_forks_event_min_datetime": "2019-11-08T04:36:16.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-22T15:11:00.000Z"}
{"max_stars_count": 64, "max_issues_count": 27, "max_forks_count": 19, "avg_line_length": 27.0918367347, "max_line_length": 88, "alphanum_fraction": 0.6655367232}
// Take an image URL, downscale it to the given width, and return a new image URL.
export async function downscaleImage(
  dataUrl,
  maxWidth,
  maxHeight,
  imageType,
  keepImageTypes,
  ignoreImageTypes,
  imageQuality,
  logger,
) {
  "use strict";
  // Input image values
  const inputImageType = dataUrl.split(';')[0].split(':')[1];

  // Provide default values
  imageType = imageType || "image/jpeg";
  imageQuality = imageQuality || 0.7;

  // Create a temporary image so that we can compute the height of the downscaled image.
  const image = new Image();
  image.src = dataUrl;
  await new Promise((resolve) => {
    image.onload = () => {
      resolve();
    };
  });
  const [newWidth, newHeight] = getDimensions(
    image.width,
    image.height,
    maxWidth,
    maxHeight
  );

  // Create a temporary canvas to draw the downscaled image on.
  const canvas = document.createElement("canvas");
  canvas.width = newWidth;
  canvas.height = newHeight;

  const ctx = canvas.getContext("2d");
  
  // If the type is an jpeg, draw a white background first.
  if (imageType === "image/jpeg") {
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, image.width, image.height);
  }

  // If the type is included in the ignore list, return the original
  if (ignoreImageTypes.includes(inputImageType)) {
    return dataUrl;
  }

  // If the type is included in keep type list, fix the image type
  if (keepImageTypes.includes(inputImageType)) {
    imageType = inputImageType;
  }

  // Draw the downscaled image on the canvas and return the new data URL.
  ctx.drawImage(image, 0, 0, newWidth, newHeight);
  const newDataUrl = canvas.toDataURL(imageType, imageQuality);
  logger.log("downscaling image...", {
    args: {
      dataUrl,
      maxWidth,
      maxHeight,
      imageType,
      ignoreImageTypes,
      keepImageTypes,
      imageQuality,
    },
    newHeight,
    newWidth,
  });
  return newDataUrl;
}

function getDimensions(inputWidth, inputHeight, maxWidth, maxHeight) {
  if (inputWidth <= maxWidth && inputHeight <= maxHeight) {
    return [inputWidth, inputHeight];
  }
  if (inputWidth > maxWidth) {
    const newWidth = maxWidth;
    const newHeight = Math.floor((inputHeight / inputWidth) * newWidth);

    if (newHeight > maxHeight) {
      const newHeight = maxHeight;
      const newWidth = Math.floor((inputWidth / inputHeight) * newHeight);
      return [newWidth, newHeight];
    } else {
      return [newWidth, newHeight];
    }
  }
  if (inputHeight > maxHeight) {
    const newHeight = maxHeight;
    const newWidth = Math.floor((inputWidth / inputHeight) * newHeight);
    return [newWidth, newHeight];
  }
}