npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bitpix

v1.0.1

Published

A package for converting and compressing images to base64 format with quality control and size limits.

Downloads

2

Readme

Introducing bitpix: Simplify Image Uploads with Seamless Base64 Encoding

Need to upload images to your web application but hate the hassle of dealing with complex URL structures or server-side image handling? Stop worrying about complicated workflows! bitpix is here to make your life easier. It converts any image to Base64 format, compresses it to ensure fast performance, and maintains quality — all with an easy-to-use function. No more worrying about image size or file types — bitpix has you covered.

Whether you're building a dynamic web app or a quick image uploader for a client, bitpix lets you focus on what matters by handling image conversion and compression in one streamlined package.

Demo

Check out the demo.

Core Function: convertImageToBase64

At the heart of bitpix is its main function, convertImageToBase64, which efficiently converts and compresses images to Base64, ensuring they are under the 5MB size limit. This function returns both the Base64 string and an error if the image exceeds the limit.

Function Signature:

convertImageToBase64(file: File): Promise<{ base64: string | null, error: string | null }>

Parameters:

  • file: The image file the user uploads (supports all common types: JPEG, PNG, GIF, etc.).

Returns:

  • base64: A Base64 encoded string that can be used in image tags (<img src="..." />), sent to a server, or stored.
  • error: If the image exceeds 5MB, an error string is returned, and the conversion is aborted.

Features:

  • All Image Formats: Accepts all image types (JPEG, PNG, GIF, etc.).
  • File Size Limit: Automatically rejects files larger than 5MB, giving you the control to handle these errors in your app.
  • Compression: Compresses the image efficiently to ensure it's within the size limits while maintaining high quality.
  • Error Handling: Ensures smooth error handling with a friendly error message when something goes wrong.

Simple JSX Example

Here’s how you can use bitpix in a simple React component to handle image uploads and display a Base64-encoded version of the image:

import React, { useState } from 'react';
import { convertImageToBase64 } from 'bitpix'; // Import the bitpix package

const ImageUploader = () => {
    const [base64Image, setBase64Image] = useState(null);
    const [error, setError] = useState(null);

    const handleImageUpload = async (event) => {
        const file = event.target.files[0];
        
        if (file) {
            const { base64, error } = await convertImageToBase64(file);

            if (error) {
                setError(error);
                setBase64Image(null);
            } else {
                setBase64Image(base64);
                setError(null);
            }
        }
    };

    return (
        <div>
            <h2>Upload an Image</h2>
            <input type="file" onChange={handleImageUpload} accept="image/*" />
            {error && <p style={{ color: 'red' }}>{error}</p>}
            {base64Image && (
                <div>
                    <h3>Base64 Encoded Image:</h3>
                    <img src={base64Image} alt="Uploaded" style={{ maxWidth: '100%' }} />
                    <textarea value={base64Image} readOnly rows="10" style={{ width: '100%' }} />
                </div>
            )}
        </div>
    );
};

export default ImageUploader;

Explanation:

  • Image Input: Users can select an image from their device using the file input.
  • Image Conversion: The selected image is converted to Base64 using the convertImageToBase64 function.
  • Display: The Base64 string is displayed both as an image preview and as plain text in a textarea.
  • Error Handling: If the image exceeds 5MB, an error message is shown.

Why Choose bitpix?

bitpix is perfect for developers who want:

  • Efficiency: Convert and compress images in a single function.
  • Compatibility: Handle all image types (JPEG, PNG, GIF, etc.).
  • Error Management: Easily manage oversized files with built-in error handling.
  • Simplicity: A clean API that integrates smoothly into any React or JavaScript project.

No more worrying about oversized image uploads or complex image handling — bitpix takes care of it all!


Contributing to bitpix

We welcome contributions from the open-source community! To contribute, follow these steps:

1. Fork the Repository

Start by forking the bitpix repository on GitHub.

git clone https://github.com/your-username/bitpix.git

2. Create a New Branch

Make a new branch for your feature or fix.

git checkout -b feature-name

3. Make Your Changes

Implement your changes and ensure your code is clean and well-structured.

4. Commit Your Changes

Write meaningful commit messages and commit your changes.

git add .
git commit -m "Add new feature"

5. Push to Your Fork

Push your changes to your forked repository.

git push origin feature-name

6. Submit a Pull Request

Submit a pull request to the original bitpix repository and explain your changes in detail.

We’ll review your changes and work together to improve bitpix!


Summary

bitpix is your go-to solution for image uploads in web applications. With its efficient compression, file size management, and simple API, bitpix takes the hassle out of image handling so you can focus on building better apps.