dstew32424 2019-06-01 22:30
浏览 36
已采纳

发送前调整图像大小

I want to have a route with gin that will send an image (jpeg) as the response but instead of sending the original image that's saved to disk I would like to resize it first (to a thumbnail size).

So far I can send images using c.File(filepath string) but that doesn't allow me to resize the image. Is there any way to do that without having to create a new file on the disk?

  • 写回答

1条回答 默认 最新

  • doucai9270 2019-06-02 00:32
    关注

    Yes, you can just resize it using built in package "image", i just built it for you, with different resize options and bytes / pure image output, pick what you want. I didnt use this particular package in the past, so any correction / recommendation for this pice of code is welcome. code:

    package main
    
    import (
        "bytes"
        "image"
        "image/color"
        "image/draw"
        "image/jpeg"
        "io/ioutil"
        "log"
        "math"
        "os"
    )
    
    func main() {
        f, err := os.Open("resources/image.jpg")
        if err != nil {
            log.Fatal(err)
        }
    
        //encoding message is discarded, because OP wanted only jpg, else use encoding in resize function
        img, _, err := image.Decode(f)
        if err != nil {
            log.Fatal(err)
        }
    
        //this is the resized image
        resImg := resize(img, 20, 20)
    
        //this is the resized image []bytes
        imgBytes := imgToBytes(resImg)
    
        //optional written to file
        err = ioutil.WriteFile("resources/test.jpg", imgBytes, 0777)
        if err != nil {
            log.Fatal(err)
        }
    }
    
    func resize(img image.Image, length int, width int) image.Image {
        //truncate pixel size
        minX := img.Bounds().Min.X
        minY := img.Bounds().Min.Y
        maxX := img.Bounds().Max.X
        maxY := img.Bounds().Max.Y
        for (maxX-minX)%length != 0 {
            maxX--
        }
        for (maxY-minY)%width!= 0 {
            maxY--
        }
        scaleX := (maxX - minX) / length
        scaleY := (maxY - minY) / width
    
        imgRect := image.Rect(0, 0, length, width)
        resImg := image.NewRGBA(imgRect)
        draw.Draw(resImg, resImg.Bounds(), &image.Uniform{C: color.White}, image.ZP, draw.Src)
        for y := 0; y < width; y += 1 {
            for x := 0; x < length; x += 1 {
                averageColor := getAverageColor(img, minX+x*scaleX, minX+(x+1)*scaleX, minY+y*scaleY, minY+(y+1)*scaleY)
                resImg.Set(x, y, averageColor)
            }
        }
        return resImg
    }
    
    func getAverageColor(img image.Image, minX int, maxX int, minY int, maxY int) color.Color {
        var averageRed float64
        var averageGreen float64
        var averageBlue float64
        var averageAlpha float64
        scale := 1.0 / float64((maxX-minX)*(maxY-minY))
    
        for i := minX; i < maxX; i++ {
            for k := minY; k < maxY; k++ {
                r, g, b, a := img.At(i, k).RGBA()
                averageRed += float64(r) * scale
                averageGreen += float64(g) * scale
                averageBlue += float64(b) * scale
                averageAlpha += float64(a) * scale
            }
        }
    
        averageRed = math.Sqrt(averageRed)
        averageGreen = math.Sqrt(averageGreen)
        averageBlue = math.Sqrt(averageBlue)
        averageAlpha = math.Sqrt(averageAlpha)
    
        averageColor := color.RGBA{
            R: uint8(averageRed),
            G: uint8(averageGreen),
            B: uint8(averageBlue),
            A: uint8(averageAlpha)}
    
        return averageColor
    }
    
    func imgToBytes(img image.Image) []byte {
        var opt jpeg.Options
        opt.Quality = 80
    
        buff := bytes.NewBuffer(nil)
        err := jpeg.Encode(buff, img, &opt)
        if err != nil {
            log.Fatal(err)
        }
    
        return buff.Bytes()
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度