123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- func (p *Poster) Create2(json PosterJson) (string, error) {
- cachePath := fmt.Sprintf("%s/cache", util.Getwd())
- image := NewImage(util.Getwd())
- image.SetCache(cachePath)
- if json.Use == "bgUrl" {
- json.Background = ""
- }
- if json.Use == "bgc" {
- json.BackgroundUrl = ""
- }
- poster, err := image.Create(json.BackgroundUrl, json.Background, *NewSize(json.Width, json.Height))
- if err != nil {
- return "", err
- }
- defer poster.Destroy()
- itemLen := len(json.Items)
- destChan := make(chan *PosterMW, itemLen)
- defer close(destChan)
- for index, item := range json.Items {
- go func(item Items, index int) {
- var drawErr error
- var drawMW *gmagick.MagickWand
- var updated Items
- if item.Type == "text" {
- drawMW, updated, drawErr = p.Text(item)
- }
- if item.Type == "image" {
- drawMW, drawErr = image.Resize(item.Value, item.Width, item.Height, item.Angle)
- }
- if item.Type == "qrcode" {
- drawMW, updated, drawErr = p.QRCode(item)
- }
- destChan <- &PosterMW{Index: index, Image: drawMW, Item: updated, Err: drawErr}
- }(item, index)
- }
- var response = make([]*PosterMW, itemLen)
- for i := 0; i < len(json.Items); i++ {
- res := <-destChan
- response[res.Index] = res
- }
- for i := 0; i < len(response); i++ {
- draw := response[i].Image
- errDraw := response[i].Err
- if errDraw != nil {
- continue
- }
- if errComposite := poster.CompositeImage(draw, gmagick.COMPOSITE_OP_OVER, response[i].Item.X, response[i].Item.Y); errComposite != nil {
- fmt.Println("poster.CompositeImage text err : ", errComposite.Error())
- continue
- }
- }
- destFile := image.CacheFile("preview-poster", poster.GetImageFormat())
- err = poster.WriteImage(destFile)
- if err != nil {
- return "", err
- }
- return destFile, nil
- }
- func (p *Poster) Create1(json PosterJson) (string, error) {
- cachePath := fmt.Sprintf("%s/cache", util.Getwd())
- image := NewImage(util.Getwd())
- image.SetCache(cachePath)
- if json.Use == "bgUrl" {
- json.Background = ""
- }
- if json.Use == "bgc" {
- json.BackgroundUrl = ""
- }
- poster, err := image.Create(json.BackgroundUrl, json.Background, *NewSize(json.Width, json.Height))
- if err != nil {
- return "", err
- }
- defer poster.Destroy()
- for _, item := range json.Items {
- if item.Type == "text" {
- drawText, _, errTxt := p.Text(item)
- if errTxt != nil {
- fmt.Println("drawText err: ", errTxt.Error())
- continue
- }
- if errComposite := poster.CompositeImage(drawText, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
- fmt.Println("poster.CompositeImage text err : ", errComposite.Error())
- continue
- }
- }
- if item.Type == "image" {
- imageMW, errImage := image.Resize(item.Value, item.Width, item.Height, item.Angle)
- if errImage != nil {
- continue
- }
- if errComposite := poster.CompositeImage(imageMW, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
- fmt.Println("poster.CompositeImage image err : ", errComposite.Error())
- continue
- }
- }
- if item.Type == "qrcode" {
- qrWM, _, errQr := p.QRCode(item)
- if errQr != nil {
- fmt.Println("Qr err", errQr.Error())
- continue
- }
- if errComposite := poster.CompositeImage(qrWM, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
- fmt.Println("poster.CompositeImage qr err : ", errComposite.Error())
- continue
- }
- }
- }
- //util.RandomStr(10);
- destFile := image.CacheFile("preview-poster", poster.GetImageFormat())
- err = poster.WriteImage(destFile)
- if err != nil {
- return "", err
- }
- return destFile, nil
- }
|