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

@houkai/imagehandler

v1.0.1

Published

Use sharp and gifsicle to manipulate images

Downloads

2

Readme

支持 jpg, jpeg, png, webp, tiff, gif, svg 样式的图片裁剪、缩放等操作


内置处理引擎


图片处理支持类型

  • resize: 缩放

    • width: 宽度
    • height: 高度
    • fit: 图片缩放规则 default: cover
      • cover: 智能裁剪
        • 智能裁剪 (个人理解)
          1. width 对比 (old_width/new_width) 和 height 对比 (old_height/new_height) 比较
          1. 比例大的需要先裁剪一定长度(z)
          1. 比如 width 比例较大: (old_width - z)/new_width = old_height/new_height
          1. z = old_width - (old_height/new_height) * new_width
      • fill: 忽略比例, 强行缩放
      • inside: 忽略给定短边, 按照长边比例进行缩放
      • outside: 忽略给定长边, 按照短边比例进行缩放
      • contain: 图片缩放和 inside 相同, 差别在于是内嵌在给定长宽内
    • limit: <bool> 当超出原有大小时, 是否禁止缩放; default: false
    • position: 智能裁剪时保留哪个部位; center, top, bottom, left, right
  • crop: 裁剪

    • left: x 坐标
    • top: y 坐标
    • width: 宽度
    • height: 高度
  • rotate: 翻转

    • rotate: 度数
  • quality:

    • quality: number
  • proportion:

    • proportion: number (放大倍数)
  • format:

    • format: 'png', 'jpg' ...
  • circle: 裁圆; gif 需先 format png

    • desc: 目前只做了圆处理, 后期会加入花式处理
    • x: x 坐标
    • y: y 坐标
    • r: 半径

暴露函数

imageHandler (input, rule, forceSharp)

  • input: 仅持支 buffer, 后期优化
  • rule:
    [
      { method: 'format', opts: { format: 'png' } },
      { method: 'resize', opts: { width: 100, height: 100 } }
    ]
  • style: 内置的几种样式, 优先级高于 rule
  • forceSharp: 如 format、circle 等功能对 gif 图片而言就需要强制使用 sharp 引擎处理; 因为另一个没写 😂
  • return: buffer

toRule (ruleStr)

  • param: ruleStr, 字符串形式处理规则
  • returns: ruleArr, 数组形式规则

为方便使用, 提供了另一套字符串规则, 使用缩写代替

上文中提到, 支持 resize crop circle rotate format proportion quality 七种操作方式, 其中 resize crop circle 三种操作, 需要批量参数修正, 所以制定如下简易规则:

  • resize:
    • 示例: resize,w_100,h_100,f_inside,l_1,p_center
    • width: w
    • height: h
    • limit: l
    • fit: f
    • position: p
  • crop:
    • 示例: crop,x_100,y_100,w_100,h_100
    • left: x
    • top: y
    • width: w
    • heigth: h
  • circle
    • 示例: circle,x_100,y_100,r_100
  • rotate
    • 示例: rotate,10
  • quality
    • 示例: quality,90
  • proportion
    • 示例: proportion,2
  • format
    • 示例: format,png

单样式字符串, 比如 'format,png' 交由 toRule 函数处理, 返回规则数组, 然后交由 imageHandler 函数处理即可 多样式字符串, 样式之间以 : 间隔, resize,w_100:format,png