best fit option

This commit is contained in:
Milan Nikolic
2015-11-05 05:06:45 +01:00
parent bbcaf0f9dd
commit 97aae5e0c7
2 changed files with 9 additions and 2 deletions

View File

@@ -54,12 +54,13 @@ Using
--width=0 Image width --width=0 Image width
--height=0 Image height --height=0 Image height
--fit Best fit for required width and height
--quality=75 JPEG image quality --quality=75 JPEG image quality
--filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
--png Encode images to PNG instead of JPEG --png Encode images to PNG instead of JPEG
--bmp Encode images to 4-Bit BMP (16 colors) instead of JPEG --bmp Encode images to 4-Bit BMP (16 colors) instead of JPEG
--gif Encode images to GIF instead of JPEG --gif Encode images to GIF instead of JPEG
--rgb Convert images that have RGB colorspace (use --no-rgb if you only want to process grayscale images) --rgb Convert images that have RGB colorspace (use --no-rgb if you only want to convert grayscale images)
--nonimage Leave non image files in archive (use --no-nonimage to remove non image files from archive) --nonimage Leave non image files in archive (use --no-nonimage to remove non image files from archive)
--grayscale Convert images to grayscale (monochromatic) --grayscale Convert images to grayscale (monochromatic)
--rotate=0 Rotate images, valid values are 0, 90, 180, 270 --rotate=0 Rotate images, valid values are 0, 90, 180, 270

View File

@@ -89,6 +89,7 @@ type options struct {
Quality int // JPEG image quality Quality int // JPEG image quality
Width int // image width Width int // image width
Height int // image height Height int // image height
Fit bool // Best fit for required width and height
Filter int // 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos Filter int // 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
RGB bool // convert images that have RGB colorspace RGB bool // convert images that have RGB colorspace
NonImage bool // Leave non image files in archive NonImage bool // Leave non image files in archive
@@ -160,8 +161,12 @@ func transformImage(img image.Image) image.Image {
var i image.Image = img var i image.Image = img
if opts.Width > 0 || opts.Height > 0 { if opts.Width > 0 || opts.Height > 0 {
if opts.Fit {
i = imaging.Fit(i, opts.Width, opts.Height, filters[opts.Filter])
} else {
i = imaging.Resize(i, opts.Width, opts.Height, filters[opts.Filter]) i = imaging.Resize(i, opts.Width, opts.Height, filters[opts.Filter])
} }
}
if opts.Rotate > 0 { if opts.Rotate > 0 {
switch opts.Rotate { switch opts.Rotate {
@@ -896,6 +901,7 @@ func parseFlags() {
convert.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments) convert.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments)
convert.Flag("width", "Image width").Default(strconv.Itoa(0)).IntVar(&opts.Width) convert.Flag("width", "Image width").Default(strconv.Itoa(0)).IntVar(&opts.Width)
convert.Flag("height", "Image height").Default(strconv.Itoa(0)).IntVar(&opts.Height) convert.Flag("height", "Image height").Default(strconv.Itoa(0)).IntVar(&opts.Height)
convert.Flag("fit", "Best fit for required width and height").BoolVar(&opts.Fit)
convert.Flag("quality", "JPEG image quality").Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) convert.Flag("quality", "JPEG image quality").Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality)
convert.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Default(strconv.Itoa(Linear)).IntVar(&opts.Filter) convert.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Default(strconv.Itoa(Linear)).IntVar(&opts.Filter)
convert.Flag("png", "Encode images to PNG instead of JPEG").BoolVar(&opts.ToPNG) convert.Flag("png", "Encode images to PNG instead of JPEG").BoolVar(&opts.ToPNG)