GolangNote

Golang笔记

golang image 智能合并图片

Permalink

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

Go: 智能合并图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 转摘请注明来源

Related articles

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Golang 把cookie 字符串解析为cookie 结构

在做爬虫时有时候会遇到需要带已登录的 cookie 请求,这个时候最简单的方法是在浏览器登录后,在开发者面板找到cookie 字符串,然后拷贝粘贴。这就面临一个问题需要把cookie 字符串解析成Go 语言 cookie 结构体。...

Write a Comment to "golang image 智能合并图片"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.18 Processed in 1ms