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 🙏

© 2026 – Pkg Stats / Ryan Hefner

image-resolution-checker

v1.0.3

Published

A Vue 3 plugin for checking image resolutions with per - page settings

Readme

Image Resolution Checker

A Vue 3 plugin to check image resolution before uploading.

Installation

npm install image-resolution-checker

Usage

import { createApp } from 'vue';
import App from './App.vue';
import ImageResolutionChecker from 'image-resolution-checker';

const app = createApp(App);
app.use(ImageResolutionChecker);
app.mount('#app');

In your component:

<template>
    <input type="file" multiple @change="handleFileUpload" />
    <div v-for="(result, index) in checkResults" :key="index">
        <p>{{ result.file.name }}: {{ result.isValid ? 'Resolution meets requirements' : 'Resolution does not meet requirements' }}</p>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { checkImageResolutions } from 'image-resolution-checker';

const checkResults = ref<{ file: File; isValid: boolean }[]>([]);
// 每个页面独立设置最小宽度
const minWidth = 1200; 
// 每个页面独立设置最小高度
const minHeight = 800; 

const handleFileUpload = async (event: Event) => {
    const target = event.target as HTMLInputElement;
    const files = target.files;
    if (files) {
        checkResults.value = await checkImageResolutions(files, minWidth, minHeight);
    }
};
</script>

代码解释

  1. checkSingleImageResolution 函数:负责检查单张图片的分辨率是否满足指定的最小宽度和最小高度要求,使用 FileReader 和 Image 对象来读取和加载图片,最终返回一个 Promise 表示检查结果。
  2. checkImageResolutions 函数:可以处理多张图片的分辨率检查,它会遍历传入的 FileList,调用 checkSingleImageResolution 函数对每张图片进行检查,并将结果存储在数组中返回。
  3. ImageResolutionChecker 插件:通过 install 方法将 checkImageResolutions 函数挂载到 Vue 应用的全局属性上,方便在组件中使用。
  4. 组件使用:在组件中引入 checkImageResolutions 函数,在文件上传事件中调用该函数,并根据返回结果显示每张图片的分辨率检查情况。

代码实例(element-plus+vue3的上传组件实例)

<template>
  <el-upload
    action="#"
    :before-upload="beforeUpload"
    :on-change="handleChange"
    :auto-upload="false"
    multiple
  >
    <el-button type="primary">点击上传图片</el-button>
  </el-upload>
  <div v-if="checkResults.length > 0">
    <el-alert
      v-for="(result, index) in checkResults"
      :key="index"
      :title="`${result.file.name}: ${result.isValid ? '分辨率符合要求' : '分辨率不符合要求'}`"
      :type="result.isValid ? 'success' : 'error'"
      :closable="false"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { checkImageResolutions } from 'image-resolution-checker';

// 每个页面独立设置的最小宽度
const minWidth = 800; 
// 每个页面独立设置的最小高度
const minHeight = 600; 
const checkResults = ref<{ file: File; isValid: boolean }[]>([]);

const beforeUpload = async (file: File | File[]) => {
  const files = Array.isArray(file) ? file : [file];
  const results = await checkImageResolutions(files as FileList, minWidth, minHeight);
  checkResults.value = results;
  // 过滤出分辨率符合要求的文件
  const validFiles = results.filter(result => result.isValid).map(result => result.file);
  // 如果有符合要求的文件,允许上传;否则阻止上传
  return validFiles.length > 0;
};

const handleChange = (file: File, fileList: File[]) => {
  // 可以在这里处理文件列表变化的逻辑
};
</script>

代码解释 使用 el-upload 组件:

  • 在组件模板中使用 ElementPlus 的 el-upload 组件来实现图片上传功能。
  • beforeUpload 钩子:在 beforeUpload 钩子中调用 checkImageResolutions 函数来检查上传的图片分辨率。
  • 根据检查结果更新 checkResults 状态,并返回符合分辨率要求的文件,以决定是否允许上传。
  • 在 el-alert 组件中,通过 :type="result.isValid ? 'success' : 'error'" 动态绑定 type 属性。
    • 当 result.isValid 为 true 时,即图片分辨率符合要求,el-alert 的类型为 success,显示绿色的提示框;
    • 当 result.isValid 为 false 时,即图片分辨率不符合要求,el-alert 的类型为 error,显示红色的提示框。
    • 这样可以让用户更直观地了解每张图片的检查结果。