mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-25 15:15:23 +03:00
Merge branch 'up/fix/aspect-ratio' into chore/all-my-stuffs
This commit is contained in:
commit
0083b1896e
6 files changed
+8207
-3149
No files matched your search
Generated
+7932
-3149
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,70 @@
|
|||||||
|
// aspect-ratio-calculator.service.test.ts
|
||||||
|
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
type AspectRatio,
|
||||||
|
calculateAspectRatio,
|
||||||
|
calculateDimensions,
|
||||||
|
simplifyRatio,
|
||||||
|
} from './aspect-ratio-calculator.service';
|
||||||
|
|
||||||
|
describe('Aspect Ratio Calculator Service', () => {
|
||||||
|
describe('calculateAspectRatio', () => {
|
||||||
|
it('calculates correct aspect ratio for 1920x1080', () => {
|
||||||
|
const result = calculateAspectRatio(1920, 1080);
|
||||||
|
expect(result).toEqual({ r1: 16, r2: 9 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calculates correct aspect ratio for 640x480', () => {
|
||||||
|
const result = calculateAspectRatio(640, 480);
|
||||||
|
expect(result).toEqual({ r1: 4, r2: 3 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles square aspect ratio', () => {
|
||||||
|
const result = calculateAspectRatio(1000, 1000);
|
||||||
|
expect(result).toEqual({ r1: 1, r2: 1 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('calculateDimensions', () => {
|
||||||
|
it('calculates correct height given width and 16:9 ratio', () => {
|
||||||
|
const ratio: AspectRatio = { r1: 16, r2: 9 };
|
||||||
|
const result = calculateDimensions(1920, ratio, true);
|
||||||
|
expect(result).toEqual({ width: 1920, height: 1080 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calculates correct width given height and 4:3 ratio', () => {
|
||||||
|
const ratio: AspectRatio = { r1: 4, r2: 3 };
|
||||||
|
const result = calculateDimensions(480, ratio, false);
|
||||||
|
expect(result).toEqual({ width: 640, height: 480 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles 1:1 ratio', () => {
|
||||||
|
const ratio: AspectRatio = { r1: 1, r2: 1 };
|
||||||
|
const result = calculateDimensions(500, ratio, true);
|
||||||
|
expect(result).toEqual({ width: 500, height: 500 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('simplifyRatio', () => {
|
||||||
|
it('simplifies 16:9 ratio', () => {
|
||||||
|
const result = simplifyRatio(16, 9);
|
||||||
|
expect(result).toEqual({ r1: 16, r2: 9 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('simplifies 1920:1080 to 16:9', () => {
|
||||||
|
const result = simplifyRatio(1920, 1080);
|
||||||
|
expect(result).toEqual({ r1: 16, r2: 9 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('simplifies 4:2 to 2:1', () => {
|
||||||
|
const result = simplifyRatio(4, 2);
|
||||||
|
expect(result).toEqual({ r1: 2, r2: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles already simplified ratios', () => {
|
||||||
|
const result = simplifyRatio(7, 5);
|
||||||
|
expect(result).toEqual({ r1: 7, r2: 5 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
// aspect-ratio-calculator.service.ts
|
||||||
|
|
||||||
|
export interface AspectRatio {
|
||||||
|
r1: number
|
||||||
|
r2: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Dimensions {
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function calculateAspectRatio(width: number, height: number): AspectRatio {
|
||||||
|
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
|
||||||
|
const divisor = gcd(width, height);
|
||||||
|
return {
|
||||||
|
r1: width / divisor,
|
||||||
|
r2: height / divisor,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function calculateDimensions(
|
||||||
|
knownDimension: number,
|
||||||
|
ratio: AspectRatio,
|
||||||
|
isWidth: boolean,
|
||||||
|
): Dimensions {
|
||||||
|
if (isWidth) {
|
||||||
|
const height = Math.round((knownDimension * ratio.r2) / ratio.r1);
|
||||||
|
return { width: knownDimension, height };
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const width = Math.round((knownDimension * ratio.r1) / ratio.r2);
|
||||||
|
return { width, height: knownDimension };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function simplifyRatio(r1: number, r2: number): AspectRatio {
|
||||||
|
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
|
||||||
|
const divisor = gcd(r1, r2);
|
||||||
|
return {
|
||||||
|
r1: r1 / divisor,
|
||||||
|
r2: r2 / divisor,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
<!-- AspectRatioCalculator.vue -->
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import { NButton, NInputNumber, NRadio, NRadioGroup, NSpace } from 'naive-ui';
|
||||||
|
import {
|
||||||
|
calculateAspectRatio,
|
||||||
|
calculateDimensions,
|
||||||
|
} from './aspect-ratio-calculator.service';
|
||||||
|
|
||||||
|
const width = ref<number | null>(null);
|
||||||
|
const height = ref<number | null>(null);
|
||||||
|
const r1 = ref<number | null>(null);
|
||||||
|
const r2 = ref<number | null>(null);
|
||||||
|
const mode = ref<'ratio' | 'dimensions'>('ratio');
|
||||||
|
const result = ref<string | null>(null);
|
||||||
|
|
||||||
|
function calculateResult() {
|
||||||
|
if (mode.value === 'ratio' && width.value && height.value) {
|
||||||
|
const ratio = calculateAspectRatio(width.value, height.value);
|
||||||
|
r1.value = ratio.r1;
|
||||||
|
r2.value = ratio.r2;
|
||||||
|
result.value = `Aspect Ratio: ${ratio.r1}:${ratio.r2}`;
|
||||||
|
}
|
||||||
|
else if (mode.value === 'dimensions' && r1.value && r2.value) {
|
||||||
|
if (width.value) {
|
||||||
|
const dimensions = calculateDimensions(width.value, { r1: r1.value, r2: r2.value }, true);
|
||||||
|
height.value = dimensions.height;
|
||||||
|
result.value = `Dimensions: ${dimensions.width}x${dimensions.height}`;
|
||||||
|
}
|
||||||
|
else if (height.value) {
|
||||||
|
const dimensions = calculateDimensions(height.value, { r1: r1.value, r2: r2.value }, false);
|
||||||
|
width.value = dimensions.width;
|
||||||
|
result.value = `Dimensions: ${dimensions.width}x${dimensions.height}`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.value = 'Please enter either width or height to calculate dimensions';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.value = 'Please fill in the required fields';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearAll() {
|
||||||
|
width.value = null;
|
||||||
|
height.value = null;
|
||||||
|
r1.value = null;
|
||||||
|
r2.value = null;
|
||||||
|
result.value = null;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<NSpace vertical :size="24">
|
||||||
|
<NRadioGroup v-model:value="mode">
|
||||||
|
<NRadio value="ratio">
|
||||||
|
Calculate Aspect Ratio
|
||||||
|
</NRadio>
|
||||||
|
<NRadio value="dimensions">
|
||||||
|
Calculate Dimensions
|
||||||
|
</NRadio>
|
||||||
|
</NRadioGroup>
|
||||||
|
|
||||||
|
<div class="input-group">
|
||||||
|
<div class="input-pair">
|
||||||
|
<label>Pixels width</label>
|
||||||
|
<NInputNumber v-model:value="width" placeholder="Pixels width" :min="1" />
|
||||||
|
</div>
|
||||||
|
<div class="input-pair">
|
||||||
|
<label>Pixels height</label>
|
||||||
|
<NInputNumber v-model:value="height" placeholder="Pixels height" :min="1" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="input-group">
|
||||||
|
<div class="input-pair">
|
||||||
|
<label>Ratio width</label>
|
||||||
|
<NInputNumber v-model:value="r1" placeholder="Ratio width" :min="1" />
|
||||||
|
</div>
|
||||||
|
<div class="separator">
|
||||||
|
:
|
||||||
|
</div>
|
||||||
|
<div class="input-pair">
|
||||||
|
<label>Ratio height</label>
|
||||||
|
<NInputNumber v-model:value="r2" placeholder="Ratio height" :min="1" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="button-container">
|
||||||
|
<NButton type="primary" @click="calculateResult">
|
||||||
|
Calculate
|
||||||
|
</NButton>
|
||||||
|
<NButton @click="clearAll">
|
||||||
|
Clear All
|
||||||
|
</NButton>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="result" class="result">
|
||||||
|
{{ result }}
|
||||||
|
</div>
|
||||||
|
</NSpace>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.input-group {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-end;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input-pair {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input-pair label {
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.separator {
|
||||||
|
align-self: flex-end;
|
||||||
|
margin-bottom: 7px;
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-container {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-container .n-button {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.result {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.n-input-number) {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { AspectRatio } from '@vicons/tabler';
|
||||||
|
import { defineTool } from '../tool';
|
||||||
|
|
||||||
|
export const tool = defineTool({
|
||||||
|
name: 'Aspect Ratio Calculator',
|
||||||
|
path: '/aspect-ratio-calculator',
|
||||||
|
description: 'Use this ratio calculator to check the dimensions when resizing images.',
|
||||||
|
keywords: ['aspect', 'ratio', 'calculator'],
|
||||||
|
component: () => import('./aspect-ratio-calculator.vue'),
|
||||||
|
icon: AspectRatio,
|
||||||
|
createdAt: new Date('2024-08-14'),
|
||||||
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { tool as base64FileConverter } from './base64-file-converter';
|
import { tool as base64FileConverter } from './base64-file-converter';
|
||||||
import { tool as base64StringConverter } from './base64-string-converter';
|
import { tool as base64StringConverter } from './base64-string-converter';
|
||||||
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
||||||
|
import { tool as aspectRatioCalculator } from './aspect-ratio-calculator';
|
||||||
import { tool as extractTextFromHtml } from './extract-text-from-html';
|
import { tool as extractTextFromHtml } from './extract-text-from-html';
|
||||||
import { tool as propertiesToYaml } from './properties-to-yaml';
|
import { tool as propertiesToYaml } from './properties-to-yaml';
|
||||||
import { tool as jsonToJava } from './json-to-java';
|
import { tool as jsonToJava } from './json-to-java';
|
||||||
@@ -386,6 +387,7 @@ export const toolsByCategory: ToolCategory[] = [
|
|||||||
removeExif,
|
removeExif,
|
||||||
imageToAsciiArt,
|
imageToAsciiArt,
|
||||||
imageExifReader,
|
imageExifReader,
|
||||||
|
aspectRatioCalculator,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user