本文整理汇总了Golang中github.com/beego/wetalk/modules/models.Image.Created方法的典型用法代码示例。如果您正苦于以下问题:Golang Image.Created方法的具体用法?Golang Image.Created怎么用?Golang Image.Created使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/beego/wetalk/modules/models.Image
的用法示例。
在下文中一共展示了Image.Created方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}