Skip to content

Commit

Permalink
添加图片放缩处理。
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdiy committed Nov 11, 2018
1 parent 48bb97a commit e7f1664
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
54 changes: 54 additions & 0 deletions core/Img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package core

import (
"goserver/utils"
"github.com/gin-gonic/gin"
"strings"
"goserver/webs/upload"
"strconv"
"io/ioutil"
"github.com/cihub/seelog"
)

type ImgResize struct {
dir string
size []string
}

func (ir *ImgResize) DoResize(context *gin.Context) {
url := context.Query("url")
if len(url) > 1 {
dp := strings.LastIndex(url, ".")
pp := strings.LastIndex(url, "_")
if dp > pp && pp > 0 {
w := url[pp+1 : dp]
for _, ss := range ir.size {
if w == ss {
target := ir.dir + url
source := ir.dir + url[0:pp] + url[dp:]
if strings.Index(w, "x") < 0 {
tw, twe := strconv.Atoi(w)
if twe == nil {
upload.ImgResize(source, target, tw)
bs, err := ioutil.ReadFile(target)
if err == nil {
context.Header("Content-Type", "image/"+url[dp+1:])
context.Writer.Write(bs)
} else {
seelog.Error("target file error:", target)
}
return
}
}
}
}
seelog.Error("生成的宽度不支持,", w)
}
}
}

func (app *Module) ImgResize(ele *utils.Element) {
ir := &ImgResize{dir: ele.MustAttr("Dir"),
size: strings.Split(ele.MustAttr("Size"), ",")}
getGin(ele).GET(ele.MustAttr("Url"), ir.DoResize)
}
2 changes: 2 additions & 0 deletions core/Web.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (app *Module) Sp(ele *utils.Element) {
put(ele, sp)
}


//文件上传
/**
TmpDir,MainWidth,ImgWidth(多个用,分隔) 可选
Expand All @@ -43,6 +44,7 @@ TmpDir,MainWidth,ImgWidth(多个用,分隔) 可选
<Upload SpRef="Sp" WebRef="Web" TmpDir="./upload/temp/" DirUpload="./upload/" ImgWidth="800" Sp="Upload" MainWidth="800" UrlPrefix="/upload"/>
*/

func (app *Module) Upload(ele *utils.Element) {
sp, spExt := ele.AttrValue("SpRef")
var nameFun func(c *webs.Param, tmpFileName string) (string, error)
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

### 项目介绍
goserver 把存储过程映射成JSON接口. 其它功能
### goserver 功能介绍
* 存储过程映射成JSON接口
* 认证码
* 定时任务(执行SQL,爬虫)
* 模版
Expand Down

0 comments on commit e7f1664

Please sign in to comment.