Golang笔记

golang image 智能合并图片

golang image 智能合并图片,自动识别图片类型,合并后缩放。

package util

import (
	"fmt"
	"image"
	"image/draw"
	"image/gif"
	"image/jpeg"
	"image/png"
	"net/http"
	"os"

	"github.com/nfnt/resize"
)

func CheckImageType2(filePath string) (string, error) {
	file, err := os.Open(filePath)

	if err != nil {
		return "", err
	}
	defer file.Close()
	buff := make([]byte, 512) // why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType
	_, err = file.Read(buff)

	if err != nil {
		return "", err
	}

	filetype := http.DetectContentType(buff)

	fmt.Println(filetype)

	switch filetype {
	case "image/jpeg", "image/jpg":
		fmt.Println(filetype)
		return "jpg", nil

	case "image/gif":
		fmt.Println(filetype)

	case "image/png":
		fmt.Println(filetype)

	case "application/pdf": // not image, but application !
		fmt.Println(filetype)
	default:
		fmt.Println("unknown file type uploaded")
	}
	return "aa", nil
}

func GetImageObj(filePath string) (img image.Image, err error) {
	f1Src, err := os.Open(filePath)

	if err != nil {
		return nil, err
	}
	defer f1Src.Close()

	buff := make([]byte, 512) // why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType
	_, err = f1Src.Read(buff)

	if err != nil {
		return nil, err
	}

	filetype := http.DetectContentType(buff)

	fmt.Println(filetype)

	fSrc, err := os.Open(filePath)
	defer fSrc.Close()

	switch filetype {
	case "image/jpeg", "image/jpg":
		img, err = jpeg.Decode(fSrc)
		if err != nil {
			fmt.Println("jpeg error")
			return nil, err
		}

	case "image/gif":
		img, err = gif.Decode(fSrc)
		if err != nil {
			return nil, err
		}

	case "image/png":
		img, err = png.Decode(fSrc)
		if err != nil {
			return nil, err
		}
	default:
		return nil, err
	}
	return img, nil
}

func MergeImage(worksDir string, file1 string, file2 string, newName string) (string, error) {

	src, err := GetImageObj(worksDir + file1)
	if err != nil {
		return "", err
	}
	srcB := src.Bounds().Max

	src1, err := GetImageObj(worksDir + file2)
	if err != nil {
		return "", err
	}
	src1B := src.Bounds().Max

	newWidth := srcB.X + src1B.X
	newHeight := srcB.Y
	if src1B.Y > newHeight {
		newHeight = src1B.Y
	}

	des := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight)) // 底板

	draw.Draw(des, des.Bounds(), src, src.Bounds().Min, draw.Over)                      //首先将一个图片信息存入jpg
	draw.Draw(des, image.Rect(srcB.X, 0, newWidth, src1B.Y), src1, image.ZP, draw.Over) //将另外一张图片信息存入jpg

	fSave, err := os.Create(worksDir + newName)
	if err != nil {
		return "", err
	}

	defer fSave.Close()

	var opt jpeg.Options
	opt.Quality = 80

	newImage := resize.Resize(1024, 0, des, resize.Lanczos3)

	err = jpeg.Encode(fSave, newImage, &opt) // put quality to 80%
	if err != nil {
		return "", err
	}
	return newName, nil
}

本文网址: https://golangnote.com/topic/149.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles