Please bookmark this page to avoid losing your image tool!

Image Resizer By Specific Dimensions

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, newWidth = 300, newHeight = 200) {
  // Ensure newWidth and newHeight are numbers
  const targetWidth = Number(newWidth);
  const targetHeight = Number(newHeight);

  // Validate inputs
  if (isNaN(targetWidth) || isNaN(targetHeight) || targetWidth <= 0 || targetHeight <= 0) {
    console.error("Invalid newWidth or newHeight. Please provide positive numbers.");
    // Create a small canvas with an error message
    const errorCanvas = document.createElement('canvas');
    errorCanvas.width = 200;
    errorCanvas.height = 50;
    const ctx = errorCanvas.getContext('2d');
    ctx.fillStyle = 'red';
    ctx.font = '12px Arial';
    ctx.fillText('Error: Invalid dimensions.', 10, 20);
    ctx.fillText(`W: ${newWidth}, H: ${newHeight}`, 10, 40);
    return errorCanvas;
  }

  // Create a new canvas element
  const canvas = document.createElement('canvas');

  // Set the canvas dimensions to the new width and height
  canvas.width = targetWidth;
  canvas.height = targetHeight;

  // Get the 2D rendering context of the canvas
  const ctx = canvas.getContext('2d');

  // Draw the original image onto the canvas, scaling it to the new dimensions
  // The drawImage method can take an image, and destination x, y, width, and height
  ctx.drawImage(originalImg, 0, 0, targetWidth, targetHeight);

  // Return the canvas with the resized image
  return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

Image Resizer By Specific Dimensions is a tool that allows users to resize images by specifying exact dimensions for width and height. This can be useful in various scenarios, such as preparing images for online use, adjusting images for specific display requirements, or optimizing images for email attachments. Simply input the desired width and height, and the tool will create a resized image, maintaining the aspect ratio where necessary. This tool is essential for web developers, graphic designers, and anyone needing to prepare images for different formats.

Leave a Reply

Your email address will not be published. Required fields are marked *