从图像URL转换时,如何调整base64的图像宽度大小?

发布于 2025-01-18 03:23:18 字数 779 浏览 1 评论 0原文

我有一个脚本将图像URL转换为base64。但是我不知道如何调整图像宽度和高度大小,以尊重图像的纵横比,然后再将其转换为base64。

<script>
const getBase64FromUrl = async (url) => {
  const data = await fetch(url);
  const blob = await data.blob();
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob); 
    reader.onloadend = () => {
      const base64data = reader.result;   
      resolve(base64data);
    }
  });
}
getBase64FromUrl('https://lh3.googleusercontent.com/i7cTyGnCwLIJhT1t2YpLW-zHt8ZKalgQiqfrYnZQl975-ygD_0mOXaYZMzekfKW_ydHRutDbNzeqpWoLkFR4Yx2Z2bgNj2XskKJrfw8').then(console.log)
</script>

我从 https://stackoverflow.com/a/64929732/18642473

I have a script with me to convert image url to base64. But i don't know how to resize the image width and that of height respecting the aspect ratio of the image before converting it to base64.

<script>
const getBase64FromUrl = async (url) => {
  const data = await fetch(url);
  const blob = await data.blob();
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob); 
    reader.onloadend = () => {
      const base64data = reader.result;   
      resolve(base64data);
    }
  });
}
getBase64FromUrl('https://lh3.googleusercontent.com/i7cTyGnCwLIJhT1t2YpLW-zHt8ZKalgQiqfrYnZQl975-ygD_0mOXaYZMzekfKW_ydHRutDbNzeqpWoLkFR4Yx2Z2bgNj2XskKJrfw8').then(console.log)
</script>

I got this from https://stackoverflow.com/a/64929732/18642473

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-01-25 03:23:18

这是我在上传之前要压缩图像的方法:

 const img_convert = (img_data) => {
    const imgArr = img_data
    const reader = new FileReader()
    reader.readAsDataURL(img_data)
    reader.onload = (e) => {
      const imgData = e.target.result
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      const newImg = document.createElement('img')
      newImg.setAttribute('src', imgData)
      newImg.onload = () => {
        const imgSize = (imgArr.size / (1000 * 1024)).toFixed(1)
        const actualWidth = newImg.width
        const actualHeight = newImg.height

        const imgWidth = Math.floor(
          actualWidth / (imgSize < 1 ? 1 : imgSize > 5 ? 4 : imgSize)
        )
        const imgHeight = Math.floor(
          actualHeight / (imgSize < 1 ? 1 : imgSize > 5 ? 4 : imgSize)
        )
        canvas.width = imgWidth
        canvas.height = imgHeight
        ctx.drawImage(newImg, 0, 0, imgWidth, imgHeight)
        const imgUrl = canvas.toDataURL('image/webp')
        setPost_img(imgUrl)
      }
    }
  }

您可以根据需要自定义图像

This is what i did to compress an image before upload it:

 const img_convert = (img_data) => {
    const imgArr = img_data
    const reader = new FileReader()
    reader.readAsDataURL(img_data)
    reader.onload = (e) => {
      const imgData = e.target.result
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      const newImg = document.createElement('img')
      newImg.setAttribute('src', imgData)
      newImg.onload = () => {
        const imgSize = (imgArr.size / (1000 * 1024)).toFixed(1)
        const actualWidth = newImg.width
        const actualHeight = newImg.height

        const imgWidth = Math.floor(
          actualWidth / (imgSize < 1 ? 1 : imgSize > 5 ? 4 : imgSize)
        )
        const imgHeight = Math.floor(
          actualHeight / (imgSize < 1 ? 1 : imgSize > 5 ? 4 : imgSize)
        )
        canvas.width = imgWidth
        canvas.height = imgHeight
        ctx.drawImage(newImg, 0, 0, imgWidth, imgHeight)
        const imgUrl = canvas.toDataURL('image/webp')
        setPost_img(imgUrl)
      }
    }
  }

You can customize it as you wish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文