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

postcss-image-dimensions

v1.0.2

Published

PostCSS plugin for image height, image ratio and image color in CSS

Downloads

3

Readme

PostCSS Image Dimensions

PostCSS plugin for using image width, height, ratio and color data in your CSS.

Features

  • Image width and height
  • Image size ratio (width:height and height:width)
  • Average color
  • Caching

Note: All width, height and ratio helpers include a 2x resolution version. For example, image-width() becomes image-width-2x(). The assumption is that the 2x image is the original image found and the 1x image is half of that.

Usage

Install package as a dependency.

yarn add postcss-image-dimensions

Add to your PostCSS build chain.

const postcssImageDimensions = require('postcss-image-dimensions');

postcss([
	postcssImageDimensions({
		globPattern: ['./src/images/**/*', './other/images/**/*'], // String or Array of strings
		cachePath: './.cache'
	})
]);

Use available helper methods in your CSS.

/* Input */
.example-image {
    width: image-width('src/images/example.jpg');
}

/* Output */
.example-image {
    width: 200px;
}

CSS helper methods

Important: All image paths used with the CSS helpers must be absolute from the root of your project.

  • image-width() - Width in pixels.
  • image-width-2x() - Width in pixels.
  • image-width-ratio() - Ratio of width as a percentage (to 4 decimal places).
  • image-width-ratio-2x() - Ratio of width as a percentage (to 4 decimal places).
  • image-height() - Height in pixels.
  • image-height-2x() - Height in pixels.
  • image-height-ratio() - Ratio of height as a percentage (to 4 decimal places).
  • image-height-ratio-2x() - Ratio of height as a percentage (to 4 decimal places).
  • image-color() - The average color of the image as a hexadecimal string or transparent if image is transparent.

Examples

All examples below assume the following image has been used.

alt text

| Example Input | Example Output | - | - | | image-width('test/juice.jpg') | 125px | | image-width-2x('test/juice.jpg') | 250px | | image-width-ratio('test/juice.jpg') | 117.9245% | | image-width-ratio-2x('test/juice.jpg') | 118.4834% | | image-height('test/juice.jpg') | 106px | | image-height-2x('test/juice.jpg') | 211px | | image-height-ratio('test/juice.jpg') | 84.8% | | image-height-ratio-2x('test/juice.jpg') | 84.4% | | image-color('test/juice.jpg') | #cbbc8e |

API

postcssImageDimensions([options])

options.globPattern

  • Type: String | Array
  • Default value: './src/images/**/*'

Override where the plugin should look for images. Paths need to be provided as glob patterns. See globby for supported patterns.

options.cachePath

  • Type: String
  • Default value: './.cache'

The plugin creates a cache of all image data so it doesn't have to keep processing images each time.

Todo

  • Write tests
  • Add more accurate average color