Oh MyUtils

宽高比计算器 - 图片和视频尺寸计算 在线

计算图片和视频的宽高比。用常用预设(16:9、4:3、1:1)保持比例缩放和响应式尺寸计算。

常见问题

什么是宽高比?

宽高比是图像、视频或屏幕的宽度与高度之间的比例关系。它用冒号分隔的两个数字表示(例如 16:9),其中第一个数字代表宽度,第二个数字代表高度。常见的宽高比包括 16:9(宽屏高清)、4:3(传统电视)和 1:1(正方形)。理解宽高比对于在不产生变形的情况下调整图像大小,以及确保内容在不同屏幕和平台上正确显示至关重要。

如何使用这个宽高比计算器?

在输入框中输入图像或视频的宽度和高度。工具会立即计算并显示简化的宽高比(例如 16:9)、小数比率和方向。要快速设置尺寸,请点击比例预设按钮(16:9、4:3、1:1 等)或使用 YouTube、Instagram、TikTok 等平台预设。切换到调整大小选项卡,可以将现有尺寸按比例缩放到目标大小。

我的数据安全吗?会有数据发送到服务器吗?

所有计算均使用 JavaScript 在您的浏览器中 100% 客户端执行。不会将任何尺寸数据、图像或其他任何信息传输到任何服务器。该工具一旦加载即可完全离线运行,因此可以安全地用于处理机密项目规格和专有媒体尺寸。

16:9 和 1.78:1 有什么区别?

它们以不同的格式表示相同的宽高比。"16:9" 是简化的整数比,而 "1.78:1"(更精确地说是 1.7778:1)是将 16 除以 9 得到的小数表示。整数格式(16:9)常用于消费类媒体和显示器规格,而小数格式在电影和专业视频制作中使用更多(例如,变形宽银幕的 2.35:1)。

如果已知宽度和宽高比,如何计算高度?

使用公式:高度 = 宽度 ×(比例高度 / 比例宽度)。例如,对于 16:9 的比例,宽度为 1920 时:高度 = 1920 ×(9/16)= 1080。此计算器自动完成此过程——只需在选择 16:9 预设后输入 1920 作为宽度,高度 1080 就会自动计算出来。

YouTube 视频应该使用什么宽高比?

YouTube 建议标准视频使用 16:9(宽屏),常见分辨率为 1920×1080(1080p)、2560×1440(1440p)和 3840×2160(4K)。YouTube Shorts 使用 9:16(竖屏),分辨率为 1080×1920。如果您的视频不符合 16:9,平台会自动添加黑边,因此使用正确的比例可以避免出现黑色条纹。

GCD(最大公约数)简化是如何工作的?

该工具通过找到宽度和高度的最大公约数,然后将两者都除以该值来简化比例。例如,1920×1080:GCD(1920, 1080) = 120,因此 1920/120 = 16,1080/120 = 9,得到比例 16:9。某些尺寸可能会产生不常见的简化比例(例如,1366×768 简化为 683:384)——在这种情况下,工具还会显示最接近的标准比例。

代码示例

// Aspect Ratio Calculator in JavaScript

// Calculate GCD using Euclidean algorithm
function gcd(a, b) {
  a = Math.abs(Math.round(a));
  b = Math.abs(Math.round(b));
  while (b) {
    [a, b] = [b, a % b];
  }
  return a;
}

// Calculate aspect ratio from width and height
function calculateAspectRatio(width, height) {
  if (width <= 0 || height <= 0) {
    throw new Error('Width and height must be positive numbers');
  }

  const divisor = gcd(width, height);
  return {
    ratioWidth: width / divisor,
    ratioHeight: height / divisor,
    decimal: +(width / height).toFixed(4),
    label: `${width / divisor}:${height / divisor}`,
  };
}

// Calculate missing dimension
function calculateMissingDimension(ratioW, ratioH, knownDimension, type) {
  if (type === 'width') {
    return Math.round((knownDimension * ratioW) / ratioH);
  } else {
    return Math.round((knownDimension * ratioH) / ratioW);
  }
}

// Scale dimensions proportionally
function scaleDimensions(originalW, originalH, targetW, targetH) {
  if (targetW) {
    const factor = targetW / originalW;
    return { width: targetW, height: Math.round(originalH * factor), factor };
  }
  if (targetH) {
    const factor = targetH / originalH;
    return { width: Math.round(originalW * factor), height: targetH, factor };
  }
}

// Example usage
const ratio = calculateAspectRatio(1920, 1080);
console.log(ratio);
// { ratioWidth: 16, ratioHeight: 9, decimal: 1.7778, label: "16:9" }

const height = calculateMissingDimension(16, 9, 2560, 'height');
console.log(`2560 x ${height}`);
// "2560 x 1440"

const scaled = scaleDimensions(1920, 1080, 1280, null);
console.log(`${scaled.width} x ${scaled.height} (${(scaled.factor * 100).toFixed(1)}%)`);
// "1280 x 720 (66.7%)"

相关工具