本文整理汇总了Golang中github.com/beego/wetalk/modules/models.Image类的典型用法代码示例。如果您正苦于以下问题:Golang Image类的具体用法?Golang Image怎么用?Golang Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ImageFilter
func ImageFilter(ctx *context.Context) {
token := path.Base(ctx.Request.RequestURI)
// split token and file ext
var filePath string
if i := strings.IndexRune(token, '.'); i == -1 {
return
} else {
filePath = token[i+1:]
token = token[:i]
}
// decode token to file path
var image models.Image
if err := image.DecodeToken(token); err != nil {
beego.Info(err)
return
}
// file real path
filePath = attachment.GenImagePath(&image) + filePath
// if x-send on then set header and http status
// fall back use proxy serve file
if setting.ImageXSend {
ext := filepath.Ext(filePath)
ctx.Output.ContentType(ext)
ctx.Output.Header(setting.ImageXSendHeader, "/"+filePath)
ctx.Output.SetStatus(200)
} else {
// direct serve file use go
http.ServeFile(ctx.ResponseWriter, ctx.Request, filePath)
}
}
示例2: GenImageFilePath
func GenImageFilePath(img *models.Image, width int) string {
var size string
if width == 0 {
size = "full"
} else {
size = utils.ToStr(width)
}
return GenImagePath(img) + size + img.GetExt()
}
示例3: QiniuImageFilter
func QiniuImageFilter(ctx *context.Context) {
var imageName = path.Base(ctx.Request.RequestURI)
var imageKey string
var imageSize string
if i := strings.IndexRune(imageName, '.'); i == -1 {
return
} else {
imageSize = imageName[i+1:]
if j := strings.IndexRune(imageSize, '.'); j == -1 {
imageSize = "full"
} else {
imageSize = imageSize[:j]
}
imageKey = imageName[:i]
}
var image = models.Image{
Token: imageKey,
}
err := image.Read("Token")
if err != nil {
return
}
var imageWidth = image.Width
var imageHeight = image.Height
var zoomRatio = setting.ImageSizeMiddle / imageWidth
if imageWidth > setting.ImageSizeMiddle {
imageWidth = setting.ImageSizeMiddle
}
imageHeight *= zoomRatio
var imageUrl = utils.GetQiniuPublicDownloadUrl(setting.QiniuPostDomain, imageKey)
var zoomImageUrl = utils.GetQiniuZoomViewUrl(imageUrl, imageWidth, imageHeight)
resp, err := http.Get(zoomImageUrl)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
ctx.ResponseWriter.Write(body)
}
示例4: Post
func (this *UploadRouter) Post() {
result := map[string]interface{}{
"success": false,
}
defer func() {
this.Data["json"] = &result
this.ServeJson()
}()
// check permition
if !this.User.IsActive {
return
}
// get file object
file, handler, err := this.Ctx.Request.FormFile("image")
if err != nil {
return
}
defer file.Close()
t := time.Now()
image := models.Image{}
image.User = &this.User
// get mime type
mime := handler.Header.Get("Content-Type")
// save and resize image
if err := attachment.SaveImage(&image, file, mime, handler.Filename, t); err != nil {
beego.Error(err)
return
}
result["link"] = image.LinkMiddle()
result["success"] = true
}
示例5: SaveImageToQiniu
func SaveImageToQiniu(m *models.Image, r goio.ReadSeeker, mime string, filename string, created time.Time, bucketName string) error {
var ext string
// check image mime type
switch mime {
case "image/jpeg":
ext = ".jpg"
case "image/png":
ext = ".png"
case "image/gif":
ext = ".gif"
default:
ext = filepath.Ext(filename)
switch ext {
case ".jpg", ".png", ".gif":
default:
return fmt.Errorf("unsupport image format `%s`", filename)
}
}
// decode image
var img image.Image
var err error
switch ext {
case ".jpg":
m.Ext = 1
img, err = jpeg.Decode(r)
case ".png":
m.Ext = 2
img, err = png.Decode(r)
case ".gif":
m.Ext = 3
img, err = gif.Decode(r)
}
if err != nil {
return err
}
m.Width = img.Bounds().Dx()
m.Height = img.Bounds().Dy()
m.Created = created
//save to database
if err := m.Insert(); err != nil || m.Id <= 0 {
return err
}
m.Token = m.GetToken()
if err := m.Update(); err != nil {
return err
}
var key = m.Token
//reset reader pointer
if _, err := r.Seek(0, 0); err != nil {
return err
}
//save to qiniu
var uptoken = utils.GetQiniuUptoken(bucketName)
var putRet io.PutRet
var putExtra = &io.PutExtra{
MimeType: mime,
}
err = io.Put(nil, &putRet, uptoken, key, r, putExtra)
if err != nil {
return err
}
return nil
}
示例6: SaveImage
func SaveImage(m *models.Image, r io.ReadSeeker, mime string, filename string, created time.Time) error {
var ext string
// test image mime type
switch mime {
case "image/jpeg":
ext = ".jpg"
case "image/png":
ext = ".png"
case "image/gif":
ext = ".gif"
default:
ext = filepath.Ext(filename)
switch ext {
case ".jpg", ".png", ".gif":
default:
return fmt.Errorf("unsupport image format `%s`", filename)
}
}
// decode image
var img image.Image
var err error
switch ext {
case ".jpg":
m.Ext = 1
img, err = jpeg.Decode(r)
case ".png":
m.Ext = 2
img, err = png.Decode(r)
case ".gif":
m.Ext = 3
img, err = gif.Decode(r)
}
if err != nil {
return err
}
m.Width = img.Bounds().Dx()
m.Height = img.Bounds().Dy()
m.Created = created
if err := m.Insert(); err != nil || m.Id <= 0 {
return err
}
m.Token = m.GetToken()
if err := m.Update(); err != nil {
return err
}
path := GenImagePath(m)
os.MkdirAll(path, 0755)
fullPath := GenImageFilePath(m, 0)
if _, err := r.Seek(0, 0); err != nil {
return err
}
var file *os.File
if f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644); err != nil {
return err
} else {
file = f
}
if _, err := io.Copy(file, r); err != nil {
os.RemoveAll(fullPath)
return err
}
if ext != ".gif" {
if m.Width > setting.ImageSizeSmall {
if err := ImageResize(m, img, setting.ImageSizeSmall); err != nil {
os.RemoveAll(fullPath)
return err
}
}
if m.Width > setting.ImageSizeMiddle {
if err := ImageResize(m, img, setting.ImageSizeMiddle); err != nil {
os.RemoveAll(fullPath)
return err
}
}
}
return nil
}