支持任意运行在浏览器的 JavaScript
语言
图片 url
转 base64
、base64
转 blob
、彩色图片转灰色图片
urlToBase64
图片 url
转 base64
查看代码
vue
<script setup lang="ts">
import { ref } from "vue";
import { urlToBase64 } from "@pureadmin/utils";
let base64Url = ref();
let base64OnLineUrl = ref();
function urlToBase() {
urlToBase64("/avatar.jpg", "image/jpeg").then(res => {
base64Url.value = res;
});
}
function urlOnLineToBase() {
urlToBase64(
"https://avatars.githubusercontent.com/u/106637267?s=200&v=4"
).then(res => {
base64OnLineUrl.value = res;
});
}
</script>
<template>
<naive-theme>
<div>
<n-button @click="urlToBase"> 本地图片转base64 </n-button>
<img
:src="base64Url"
alt="base64Url"
width="60"
height="60"
v-show="base64Url"
class="mt-2"
/>
</div>
<div class="mt-2">
<n-button @click="urlOnLineToBase" class="mt-2">
在线图片url转base64
</n-button>
<img
:src="base64OnLineUrl"
alt="base64OnLineUrl"
width="60"
height="60"
v-show="base64OnLineUrl"
class="mt-2"
/>
</div>
</naive-theme>
</template>
接收三个参数,第一个参数 url
,第二个参数 mineType
,第三个参数 encoderOptions
,返回值类型 Promise<string>
参数属性 | 说明 | 类型 |
---|---|---|
url | 图片 url | string |
mineType | 图片格式,默认为 image/png | string |
encoderOptions | 0 到 1 之间的取值,主要用来选定图片的质量,默认值是 0.92 ,超出范围也会选择默认值 | number |
dataURLtoBlob
base64
转 blob
查看代码
vue
<script setup lang="ts">
import { ref } from "vue";
import { dataURLtoBlob } from "@pureadmin/utils";
let blob = ref();
let base64 =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALEAAAAwCAYAAABexZu4AAAAAXNSR0IArs4c6QAAC5pJREFUeF7tnQ9wFNUdx7+/vQQsiOb2orHyp0JuQ/1TpFVbS+2I7RS1aqdlKu2oU2WqQvbQFmtrO7QFpp22U0ZhgNuQInXa6rTFKo5oi3XGPzNqiwIWBDTZS2Boi8aQDQxYId7tr/M2CZNc7u69vdtLLrI7k2GG/b3fe+/3Pvu737597/cIRVw1ydaZ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Y6UdB97T7eKeMv1MPZZJ474jdw/d+hQ0xN4AJ+05IIiE4v5ytI0iiAMC+TjAawESx8GJh2LI5R9izBNHVzBhCRgfLxLmQQBLPfHASsTB09URnktEwltKcxlLGriFiZ+PUOS5QlmF+hazi4Ma8x7G6K3nICRUE26LdkXXvPkJqqoSJ6beBHEojsqVBXHMsp9k4AZZUdkG21zlVXXL9sr1JYsUefamyNo54P4eAM8yIg91m9N2eZlFP8i8kxPiHAvCCs4CVWOyc4fxHxHaRXqO30HgbwKketj9QbC7Xqsa25x9MLuSJ87ugAB6rOZewRSZxuBpBExjwlkERAUj3p/3M8QdAL3DQAeBO5h4ezrjPnt00fldPoxaNtGalftqImN7ZgPa5Sz6AHh9AZAB0AJGi0gsThG84HfRU9kaXYRiXYSFvXPAE8E8EUTnEjCRAbHTvb03D3Bmnwt64nCi4V9FVFF0EW+5JTDL+5UXfy4me/+e5EfbC3L/kuHjzx0xZ4ht/EOu/wNFbO2YhmAeMQAAAABJRU5ErkJggg==";
function baseToBlob() {
blob.value = window.URL.createObjectURL(dataURLtoBlob(base64));
console.log(dataURLtoBlob(base64));
}
</script>
<template>
<naive-theme>
<n-space className="flex items-center mt-2">
<n-tooltip placement="bottom-start" trigger="hover">
<template #trigger>
<n-button @click="baseToBlob"> base64转blob </n-button>
</template>
打开控制台然后点击此按钮,观察打印的Blob吧😛
</n-tooltip>
<img
:src="blob"
alt="base64OnLineUrl"
width="60"
height="60"
v-show="blob"
/>
</n-space>
</naive-theme>
</template>
接收一个参数 base64Buf
,返回值类型 Blob
参数属性 | 说明 | 类型 |
---|---|---|
base64Buf | base64 | string |
convertImageToGray
彩色图片转灰色图片
查看代码
vue
<script setup lang="ts">
import { ref } from "vue";
import { convertImageToGray } from "@pureadmin/utils";
let vueSvg = ref();
let reactPng = ref();
let vueStr = "https://xiaoxian521.github.io/hyperlink/svg/vue.svg";
let reactStr = "https://xiaoxian521.github.io/hyperlink/img/react.png";
convertImageToGray(vueStr)
.then(grayscaleImageSrc => {
vueSvg.value = grayscaleImageSrc;
})
.catch(error => {
console.error(error);
});
convertImageToGray(reactStr)
.then(grayscaleImageSrc => {
reactPng.value = grayscaleImageSrc;
})
.catch(error => {
console.error(error);
});
</script>
<template>
<naive-theme>
原彩色图片
<div class="flex">
<img :src="vueStr" alt="vueStr" />
<img :src="reactStr" alt="reactStr" />
</div>
转换后的灰色图片
<div class="flex">
<img :src="vueSvg" alt="vueSvg" />
<img :src="reactPng" alt="reactPng" />
</div>
</naive-theme>
</template>
接收两个参数,第一个参数 url
,第二个参数 options
,返回值类型 Promise<string>
参数属性 | 说明 | 类型 |
---|---|---|
url | 彩色图片 url | string |
options | 转灰色图片相关配置,拥有四个属性,具体看下面的 options 详情 | grayOpt |
在RGB
颜色模型中,灰色由红、绿、蓝三种颜色通道混合而成。值越小,灰色越深;值越大,灰色越浅,这里的值就是指下面的red
、green
、blue
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
red | RGB 颜色模型中的红色 | number | 0.3 |
green | RGB 颜色模型中的绿色 | number | 0.59 |
blue | RGB 颜色模型中的蓝色 | number | 0.11 |
scale | 使用canvas 缩放图像比例,默认1 不缩放保持原始比例,建议范围0.2 < scale < 2 | number | 1 |
ts
interface grayOpt {
/** `RGB`颜色模型中的红色,默认`0.3` */
red?: number;
/** `RGB`颜色模型中的绿色,默认`0.59` */
green?: number;
/** `RGB`颜色模型中的蓝色,默认`0.11` */
blue?: number;
/** 使用`canvas`缩放图像比例,默认`1`不缩放保持原始比例,建议范围`0.2 < scale < 2` */
scale?: number;
}
提示信息
在线图片地址需要支持跨域,服务器配置下允许跨域即可