一,安装所需的外部程序
1,用apt-get安装ImageMagick
root@ku:/data/liuhongdi/digv23# apt-get install imagemagick
2,查看安装是否成功:
-
root@ku:/data/liuhongdi/digv23# convert --version
-
Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
-
Copyright: © 1999-2019 ImageMagick Studio LLC
-
License: https://imagemagick.org/script/license.php
-
Features: Cipher DPC Modules OpenMP
-
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib
3,查看convert的默认安装路径:
-
root@ku:/data/liuhongdi/digv23# whereis convert
-
convert: /usr/bin/convert /usr/share/man/man1/convert.1.gz
说明:刘宏缔的go森林是一个专注golang的博客,
地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/digv24
2,功能:演示go调用shell命令生成缩略图
3,项目结构: 如图:
三,配置文件说明
1,config/config.yaml
-
Server:
-
RunMode: debug
-
HttpPort: 8000
-
ReadTimeout: 60
-
WriteTimeout: 60
-
Log:
-
LogFilePath: /data/gologs/logs
-
LogInfoFileName: info
-
LogWarnFileName: warn
-
LogFileExt: log
-
AccessLog:
-
LogFilePath: /data/gologs/logs
-
LogFileName: access
-
LogFileExt: log
-
Static:
-
StaticDir: /data/liuhongdi/digv24/static
-
ArticleImage:
-
UploadDir: /data/liuhongdi/digv24/static/ware/orig
-
TmbDir: /data/liuhongdi/digv24/static/ware/tmb
-
ImageHost: http://127.0.0.1:8000
-
说明:
StaticDir:静态文件的保存目录
UploadDir:文章配图的原始图上传后保存目录
TmbDir: 文章配图的缩略图保存目录
ImageHost:访问文章配图url的host
四,go代码说明
1,controller/imageController.go
-
package controller
-
-
import (
-
"fmt"
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv24/global"
-
"github.com/liuhongdi/digv24/pkg/image"
-
"github.com/liuhongdi/digv24/pkg/result"
-
"github.com/liuhongdi/digv24/pkg/validCheck"
-
"github.com/liuhongdi/digv24/request"
-
"strconv"
-
)
-
-
type ImageController struct{}
-
-
func NewImageController() ImageController {
-
return ImageController{}
-
}
-
//上传单张图片
-
func (a *ImageController) UploadOne(c *gin.Context) {
-
resultRes := result.NewResult(c)
-
param := request.ArticleRequest{ID: validCheck.StrTo(c.Param("id")).MustUInt64()}
-
valid, errs := validCheck.BindAndValid(c, ¶m)
-
if !valid {
-
resultRes.Error(400,errs.Error())
-
return
-
}
-
-
//save image
-
f, err := c.FormFile("f1s")
-
//错误处理
-
if err != nil {
-
fmt.Println(err.Error())
-
resultRes.Error(1,"图片上传失败")
-
} else {
-
//将文件保存至本项目根目录中
-
idstr:=strconv.FormatUint(param.ID, 10)
-
destImage := global.ArticleImageSetting.UploadDir+"/"+idstr+".jpg"
-
err := c.SaveUploadedFile(f, destImage)
-
if (err != nil){
-
fmt.Println("save err:")
-
fmt.Println(err)
-
resultRes.Error(1,"图片保存失败")
-
} else {
-
//make tmb
-
orig:= destImage
-
dest := global.ArticleImageSetting.TmbDir+"/"+idstr+".jpg"
-
err := image.ConvertByLong(orig,dest,300)
-
if (err != nil){
-
fmt.Println(err)
-
}
-
origUrl := global.ArticleImageSetting.ImageHost+"/static/ware/orig/"+idstr+".jpg"
-
tmbUrl := global.ArticleImageSetting.ImageHost+"/static/ware/tmb/"+idstr+".jpg"
-
resultRes.Success(gin.H{"origurl":origUrl,"tmburl":tmbUrl})
-
}
-
}
-
return
-
}
2,pkg/image/convert.go
-
package image
-
-
import (
-
"bytes"
-
"errors"
-
"fmt"
-
"os/exec"
-
"strconv"
-
)
-
-
//按指定的图片的长边,生成缩略图
-
func ConvertByLong(orig,dest string,long int) error {
-
longstr := strconv.Itoa(long)
-
cmd := "/usr/bin/convert -size "+longstr+"x"+longstr+" -resize "+longstr+"x"+longstr+" +profile '*' "+orig+" "+dest+" 2>&1"
-
out,err := execShell(cmd)
-
fmt.Println(out)
-
fmt.Println(err)
-
if (err != nil) {
-
return err
-
}
-
if (out != "") {
-
return errors.New(out)
-
}
-
return nil
-
}
-
-
//执行shell命令
-
func execShell(s string) (string, error){
-
//函数返回一个*Cmd,用于使用给出的参数执行name指定的程序
-
cmd := exec.Command("/bin/bash", "-c", s)
-
var out bytes.Buffer
-
cmd.Stdout = &out
-
err := cmd.Run()
-
//checkErr(err)
-
return out.String(), err
-
}
3,static/upload.html
-
-
<html lang="zh-CN">
-
<head>
-
<title>上传文件示例</title>
-
</head>
-
<body>
-
单文件上传:<br/>
-
<form action="/image/uploadone" method="post" enctype="multipart/form-data">
-
<input type="text" name="id" id="id" placeholder="请输入id" /> <br/>
-
<input type="file" name="f1s" /><br/>
-
<input type="submit" value="上传">
-
</form>
-
-
</body>
-
</html>
4,其他相关代码可访问github查看
五,测试效果
1,打开上传页面,访问:
http://127.0.0.1:8000/static/upload.html
返回如图:
上传一张图片后返回:
点击tmburl,可以看到缩放为长边300的图片:
六,查看库的版本
-
module github.com/liuhongdi/digv24
-
-
go 1.15
-
-
require (
-
github.com/gin-gonic/gin v1.6.3
-
github.com/go-playground/universal-translator v0.17.0
-
github.com/go-playground/validator/v10 v10.2.0
-
github.com/jinzhu/gorm v1.9.16
-
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f
-
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
-
github.com/magiconair/properties v1.8.4 // indirect
-
github.com/mitchellh/mapstructure v1.3.3 // indirect
-
github.com/pelletier/go-toml v1.8.1 // indirect
-
github.com/pkg/errors v0.9.1 // indirect
-
github.com/spf13/afero v1.4.1 // indirect
-
github.com/spf13/cast v1.3.1 // indirect
-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
-
github.com/spf13/pflag v1.0.5 // indirect
-
github.com/spf13/viper v1.7.1
-
go.uber.org/multierr v1.6.0 // indirect
-
go.uber.org/zap v1.16.0
-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
-
golang.org/x/text v0.3.4 // indirect
-
gopkg.in/ini.v1 v1.62.0 // indirect
-
gopkg.in/yaml.v2 v2.3.0 // indirect
-
)