Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] - Image Resizer #48

Merged
merged 24 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0ebdd5f
feat(readme): add Image Resizer link to the utilities list
EduardoDePatta Aug 20, 2024
2179d02
feat(seo): create ImageResizeSEO component
EduardoDePatta Aug 20, 2024
3c3c891
feat(utils): implement resizeImage function
EduardoDePatta Aug 20, 2024
78d2de9
feat(tools): add Image Resizer to tools list
EduardoDePatta Aug 20, 2024
84acaed
feat(page): implement Image Resizer page
EduardoDePatta Aug 20, 2024
5db8ebb
feat(combobox): add optional disabled prop
EduardoDePatta Aug 20, 2024
0d4f805
feat(image-resizer): expand SEO content to include SVG format details
EduardoDePatta Aug 20, 2024
35a08e4
refactor(resize-image): use options object and mantainAspectRatio
EduardoDePatta Aug 20, 2024
44df5bc
refactor(page): include mantainAspectRatio, quality input, and improv…
EduardoDePatta Aug 20, 2024
19e9b25
feat(tailwindcss): add grow-from-center animation to tailwind config
EduardoDePatta Aug 20, 2024
44f19a7
refactor(image-to-base64): extract ImageUploadComponent from ImageToB…
EduardoDePatta Aug 20, 2024
b9a37c7
feat: create reusable ImageUploadComponent with drag-and-drop and fil…
EduardoDePatta Aug 20, 2024
fbddb85
docs(SEO): clarify SVG conversion limitations and use cases in Image …
EduardoDePatta Aug 20, 2024
821ffcb
refactor(resize-image): add support for SVG format in image resize fu…
EduardoDePatta Aug 20, 2024
da669c8
feat(image-resizer): introduce svg support
EduardoDePatta Aug 20, 2024
e0833d7
test(resize-image.utils): add comprehensive unit tests for image proc…
EduardoDePatta Aug 20, 2024
59c659d
refactor: standardize components to match project conventions
EduardoDePatta Aug 21, 2024
7343901
chore(deps-dev): add canvas to dev dependencies
EduardoDePatta Aug 21, 2024
7b9ac98
fix: resolve merge conflicts
EduardoDePatta Aug 21, 2024
ae6ed5f
style: organize inputs in 50/50 layout, add divider between sections
peckz Sep 10, 2024
51c4dc5
feat: enhance image resizer with dynamic labels and input validation
EduardoDePatta Sep 10, 2024
8c64c79
feat: add dynamic file size formatting to ImageUploadComponent
EduardoDePatta Sep 10, 2024
40d1ae3
refactor: rename variable for consistency in image processing utility
EduardoDePatta Sep 10, 2024
0ad64ce
Merge branch 'main' into feature/image-resizer
EduardoDePatta Sep 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Here is the list of all utilities:
- [Number Base Changer](https://jam.dev/utilities/number-base-changer)
- [CSS Inliner for Email](https://jam.dev/utilities/css-inliner-for-email)
- [Regex Tester](https://jam.dev/utilities/regex-tester)
- [Image Resizer](https://jam.dev/utilities/image-resizer)
- [CSS Units Converter](https://jam.dev/utilities/css-units-converter)

### Built With
Expand Down
2 changes: 2 additions & 0 deletions components/ds/ComboboxComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ComboboxProps {
data: { value: string; label: string }[];
onSelect(value: string): void;
defaultValue?: string;
disabled?: boolean;
}

export function Combobox(props: ComboboxProps) {
Expand All @@ -38,6 +39,7 @@ export function Combobox(props: ComboboxProps) {
role="combobox"
aria-expanded={open}
className="justify-between"
disabled={props.disabled}
>
{selectedItem ? selectedItem.label : "Select base..."}
<CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
Expand Down
133 changes: 133 additions & 0 deletions components/ds/ImageUploadComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"use client";
import { DragEvent, useCallback, useMemo, useRef, useState } from "react";
import UploadIcon from "@/components/icons/UploadIcon";

type Status = "idle" | "loading" | "error" | "unsupported" | "hover";

const MAX_FILE_SIZE = 4 * 1024 * 1024;
interface ImageUploadProps {
onFileSelect: (file: File) => void;
maxFileSize?: number;
}

const ImageUploadComponent = ({
onFileSelect,
maxFileSize = MAX_FILE_SIZE,
}: ImageUploadProps) => {
const [status, setStatus] = useState<Status>("idle");
const inputRef = useRef<HTMLInputElement>(null);

const formattedMaxSize = useMemo((): string => {
const sizeInMB = maxFileSize / (1024 * 1024);
return Number.isInteger(sizeInMB)
? `${sizeInMB}MB`
: `${sizeInMB.toFixed(2)}MB`;
}, [maxFileSize]);

const handleDrop = useCallback(
(event: DragEvent<HTMLDivElement>) => {
event.preventDefault();

const file = event.dataTransfer.files[0];
if (!file || !file.type.startsWith("image/")) {
setStatus("unsupported");
return;
}

if (file.size > maxFileSize) {
setStatus("error");
return;
}

setStatus("loading");
onFileSelect(file);
setStatus("idle");
},
[onFileSelect, maxFileSize]
);

const handleDragOver = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
setStatus("hover");
},
[]
);

const handleDragLeave = useCallback(() => {
setStatus("idle");
}, []);

const handleClick = () => {
inputRef.current?.click();
};

const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
if (!file.type.startsWith("image/")) {
setStatus("unsupported");
return;
}

if (file.size > maxFileSize) {
setStatus("error");
return;
}

setStatus("loading");
onFileSelect(file);
setStatus("idle");
}
};

return (
<div
onDrop={handleDrop}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onClick={handleClick}
className="flex flex-col border border-dashed border-border p-6 text-center text-muted-foreground rounded-lg min-h-40 items-center justify-center bg-muted cursor-pointer mb-2"
>
<input
ref={inputRef}
type="file"
accept="image/*"
className="hidden"
onChange={handleFileChange}
/>
<UploadIcon />
{statusComponents[status](formattedMaxSize)}
</div>
);
};

const StatusComponent = ({
title,
message,
}: {
title: string;
message?: string;
}) => (
<div>
<p>{title}</p>
<p>{message || "\u00A0"}</p>
</div>
);

const statusComponents: Record<Status, (maxSize: string) => JSX.Element> = {
idle: (maxSize) => (
<StatusComponent
title="Drag and drop your image here, or click to select"
message={`Max size ${maxSize}`}
/>
),
loading: () => <StatusComponent title="Loading..." />,
error: (maxSize) => (
<StatusComponent title="Image is too big!" message={`${maxSize} max`} />
),
unsupported: () => <StatusComponent title="Please provide a valid image" />,
hover: () => <StatusComponent title="Drop it like it's hot! 🔥" />,
};

export { ImageUploadComponent };
145 changes: 145 additions & 0 deletions components/seo/ImageResizeSEO.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import Link from "next/link";

export default function ImageResizeSEO() {
return (
<div className="content-wrapper">
<section>
<h2>Free, Open Source & Ad-free</h2>
<p>
This free image resizer is a versatile tool that allows you to resize
various image formats, including PNG, JPEG, and SVG. While PNG and
JPEG images are pixel-based and can have their dimensions adjusted,
SVGs are vector-based and can be scaled infinitely without losing
quality. However, if you convert a PNG or JPEG to SVG using this tool,
it will result in an SVG file that contains the image as Base64 data.
This means the image will still be rasterized and can lose quality
when resized. Whether you need to reduce file size, adjust dimensions,
or convert formats, our tool can help. Made with 💜 by the developers
building Jam.
</p>
</section>

<section>
<h2>How to Use the Image Resizer</h2>
<p>
Resizing images with our online tool is simple and straightforward.
Just upload your image, select the desired dimensions and format, and
download the resized image. No signup required. Here's how:
</p>
<ul>
<li>
<b>Step 1:</b> <br /> Upload your image: Choose the image file you
want to resize. Supported formats include PNG, JPEG, and SVG.
</li>
<li>
<b>Step 2:</b> <br /> Select dimensions (for PNG/JPEG): Enter the
width or height, and the tool will maintain the aspect ratio. For
SVG, the image can be scaled as needed without losing quality.
</li>
<li>
<b>Step 3:</b> <br /> Choose the format: Select between PNG, JPEG,
or SVG. While PNG and JPEG are more common, SVG is a good choice if
you want an image that can scale perfectly to any size. However, be
aware that converting a PNG or JPEG to SVG will result in an SVG
that embeds the image as Base64, which does not improve scalability
or quality.
</li>
<li>
<b>Step 4:</b> <br /> Download: Click the button to download the
resized image.
</li>
</ul>
</section>

<section>
<h2>Benefits of Using an Image Resizer</h2>
<p>
Resizing images helps in optimizing their use across different
platforms and devices. Whether you're a developer, designer, or
content creator, resizing images can greatly enhance the performance
and aesthetics of your work.
</p>
<ul>
<li>
<b>Performance:</b> <br /> Smaller image files load faster,
improving website performance and user experience.
</li>
<li>
<b>Compatibility:</b> <br /> Ensure your images fit perfectly on
different screens and devices by adjusting their dimensions.
</li>
<li>
<b>File Size Reduction:</b> <br /> Reducing the dimensions can
significantly decrease the file size without losing quality. This is
particularly effective with PNG and JPEG formats.
</li>
<li>
<b>SVG Scalability:</b> <br /> SVG images are vector-based, meaning
they can be scaled infinitely without losing quality. However,
converting a PNG or JPEG to SVG results in an SVG that contains the
image as Base64. This type of SVG will behave like a raster image
and may lose quality when resized, similar to the original PNG or
JPEG.
</li>
</ul>
</section>

<section>
<h2>More Image Tools: Easy Conversion</h2>
<p>
Explore other image conversion tools available on Jam.dev for
developers and designers. They're all free, open source, and available
in dark mode too.
</p>
<ul>
<li>
<Link href="/utilities/image-to-base64">Image to Base64</Link>:
Instantly convert images to Base64 strings. Embed images directly in
your code with ease.
</li>
<li>
<Link href="/utilities/hex-to-rgb">HEX to RGB</Link>: Easily convert
HEX to RGB and generate CSS snippets for web development.
</li>
</ul>
</section>

<section>
<h2>FAQs</h2>
<ul>
<li>
<b>How does the image resizer maintain quality?</b> <br /> Our tool
uses advanced algorithms to resize images while preserving their
original quality, ensuring sharpness and clarity. For SVGs, the tool
ensures that the vector quality is maintained at any size. However,
if you convert a PNG or JPEG to SVG, the resulting SVG will embed
the original image in Base64 format, which means it will behave like
a raster image and may lose quality when resized.
</li>
<li>
<b>What formats are supported?</b> <br /> Currently, our tool
supports PNG, JPEG, and SVG formats, which are the most commonly
used image formats on the web.
</li>
<li>
<b>Can I resize images to specific dimensions?</b> <br /> Yes, you
can specify either the width or height, and the aspect ratio will be
maintained automatically. This applies to both raster formats (PNG,
JPEG) and vector formats (SVG).
</li>
<li>
<b>Is there a limit to the image size?</b> <br /> Our tool supports
image files up to 4MB in size. This ensures quick processing and
efficient performance. While SVGs are generally lightweight and can
be resized quickly, PNG and JPEG images must also adhere to this
size limit.
</li>
<li>
<b>Is this tool free to use?</b> <br /> Yes, the image resizer is
completely free to use, without any hidden costs or limitations.
</li>
</ul>
</section>
</div>
);
}
Loading
Loading