Browse Source

Format parameter removed from Save function

disintegration 12 năm trước cách đây
mục cha
commit
49f546fde8
2 tập tin đã thay đổi với 21 bổ sung17 xóa
  1. 7 5
      README.md
  2. 14 12
      imaging.go

+ 7 - 5
README.md

@@ -8,6 +8,8 @@ manipulation functions provided by the package return a new image of
 
 ###Recent changes
 
+- Format parameter removed from `Save` function. Now the format is determined
+from the filename extension, `jpg` (or `jpeg`) and `png` are supported.
 - All the image manipulation functions now return `*image.NRGBA` instead of
 `draw.Image`.
 - `Copy()` function renamed to `Clone()`. 
@@ -31,12 +33,12 @@ package main
 import (
     "github.com/disintegration/imaging"
     "image"
-    "image/color"  
+    "image/color"
 )
 
 func main() {
-    src, _ := imaging.Open("1.png") // load an image from file (returns image.Image interface)
-    var dst image.Image
+    src, _ := imaging.Open("src.png") // load an image from file (returns image.Image interface)
+    var dst *image.NRGBA
     
     dst = imaging.New(800, 600, color.NRGBA(255, 0, 0, 255)) // create a new 800x600px image filled with red color
     dst = imaging.Clone(src) // make a copy of the image
@@ -60,6 +62,6 @@ func main() {
     dst = imaging.Paste(dst, src, image.Pt(50, 50)) // paste the src image to the dst image at the given position
     dst = imaging.PasteCenter(dst, src) // paste the src image to the center of the dst image
 
-    imaging.Save(dst, "2.jpg", "jpeg") // save the image to file using jpeg format
+    imaging.Save(dst, "dst.jpg") // save the image to file using jpeg format
 }
-```
+```

+ 14 - 12
imaging.go

@@ -14,6 +14,7 @@ import (
 	"image/png"
 	"math"
 	"os"
+	"path/filepath"
 	"strings"
 )
 
@@ -26,29 +27,31 @@ func Open(filename string) (img image.Image, err error) {
 	defer file.Close()
 
 	img, _, err = image.Decode(file)
-	if err != nil {
-		return
-	}
 	return
 }
 
-// Save saves the image to file with the specified filename. Format parameter can be "jpeg" or "png".
-func Save(img image.Image, filename string, format string) (err error) {
+// Save saves the image to file with the specified filename. 
+// The format is determined from the filename extension, "jpg" (or "jpeg") and "png" are supported.
+func Save(img image.Image, filename string) (err error) {
+	format := strings.ToLower(filepath.Ext(filename))
+	if format != "jpg" && format != "jpeg" && format != "png" {
+		err = fmt.Errorf("unknown image format: %s", format)
+		return
+	}
+
 	file, err := os.Create(filename)
 	if err != nil {
 		return
 	}
 	defer file.Close()
 
-	formatLower := strings.ToLower(format)
-	switch formatLower {
-	case "jpeg", "jpg":
+	switch format {
+	case "jpg", "jpeg":
 		err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95})
 	case "png":
 		err = png.Encode(file, img)
-	default:
-		err = fmt.Errorf("unknown image format: %s", format)
 	}
+
 	return
 }
 
@@ -155,7 +158,6 @@ func toNRGBA(src image.Image, clone bool) *image.NRGBA {
 					dst.Pix[i+1] = uint8(uint16(src0.Pix[j+1]) * 0xff / uint16(a))
 					dst.Pix[i+2] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
 				}
-
 			}
 		}
 
@@ -182,7 +184,6 @@ func toNRGBA(src image.Image, clone bool) *image.NRGBA {
 					dst.Pix[i+1] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
 					dst.Pix[i+2] = uint8(uint16(src0.Pix[j+4]) * 0xff / uint16(a))
 				}
-
 			}
 		}
 
@@ -641,6 +642,7 @@ func Resize(img image.Image, width, height int) *image.NRGBA {
 			dst.Pix[i+3] = uint8(a + 0.5)
 		}
 	}
+
 	return dst
 }