Skip to content

Commit

Permalink
feat: Add fix major axis length (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
정현 committed Mar 31, 2024
2 parents 3d74a15 + f134f2e commit 0c34296
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
29 changes: 29 additions & 0 deletions web/src/components/FixMajorAxisLengthInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ListInput } from 'konsta/react';
import { useEffect, useState } from 'react';

if (localStorage.getItem('fixMajorAxisLength') === null) {
localStorage.setItem('fixMajorAxisLength', '0');
}

const FixMajorAxisLengthInput = () => {
const [fixMajorAxisLength, setFixMajorAxisLength] = useState(Number(localStorage.getItem('fixMajorAxisLength')));

useEffect(() => {
localStorage.setItem('fixMajorAxisLength', fixMajorAxisLength.toString());
}, [fixMajorAxisLength]);

return (
<ListInput
label
title="Fix Major Axis Length (px, 0 to disable)"
type="number"
placeholder="e.g. 1920"
value={fixMajorAxisLength}
onChange={(e) => {
setFixMajorAxisLength(e.target.value);
}}
/>
);
};

export default FixMajorAxisLengthInput;
16 changes: 14 additions & 2 deletions web/src/domain/photo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ class Photo {

public toBlob(): Promise<Blob> {
const isWebp = localStorage.getItem('exportToWebp') === 'yes';
const fixMajorAxisLength = Number(localStorage.getItem('fixMajorAxisLength'));
const tempCanvas = document.createElement('canvas');
tempCanvas.width = fixMajorAxisLength || this.image.width;
tempCanvas.height = (this.image.height / this.image.width) * tempCanvas.width;
const tempContext = tempCanvas.getContext('2d')!;
tempContext.drawImage(this.image, 0, 0, tempCanvas.width, tempCanvas.height);
return new Promise<Blob>((resolve) => {
this._canvas.toBlob(
tempCanvas.toBlob(
(blob) => {
resolve(blob!);
},
Expand All @@ -44,7 +50,13 @@ class Photo {

public toDataURL(): string {
const isWebp = localStorage.getItem('exportToWebp') === 'yes';
return this._canvas.toDataURL(isWebp ? 'image/webp' : 'image/jpeg', Number(localStorage.getItem('quality')) / 100);
const fixMajorAxisLength = Number(localStorage.getItem('fixMajorAxisLength'));
const tempCanvas = document.createElement('canvas');
tempCanvas.width = fixMajorAxisLength || this.image.width;
tempCanvas.height = (this.image.height / this.image.width) * tempCanvas.width;
const tempContext = tempCanvas.getContext('2d')!;
tempContext.drawImage(this.image, 0, 0, tempCanvas.width, tempCanvas.height);
return tempCanvas.toDataURL(isWebp ? 'image/webp' : 'image/jpeg', Number(localStorage.getItem('quality')) / 100);
}

public toMetadata() {
Expand Down
2 changes: 2 additions & 0 deletions web/src/pages/SettingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import OverrideCameraMakerInput from '../components/OverrideCameraMakerInput';
import OverrideCameraModelInput from '../components/OverrideCameraModelInput';
import OverrideLensModelInput from '../components/OverrideLensModelInput';
import QualityRangeSlider from '../components/QualityRangeSlider';
import FixMajorAxisLengthInput from '../components/FixMajorAxisLengthInput';

const SettingPage = () => {
return (
Expand All @@ -21,6 +22,7 @@ const SettingPage = () => {
<List strong inset>
<ListItem label title="Export to Webp" after={<ExportToWebpToggle />} />
<QualityRangeSlider />
<FixMajorAxisLengthInput />
</List>

<BlockTitle>EXIF Metadata Visibility</BlockTitle>
Expand Down

0 comments on commit 0c34296

Please sign in to comment.