diff --git a/.travis.yml b/.travis.yml index f2583ee..99b56ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,12 @@ go: - 1.5 - tip +before_install: + - curl -s https://raw.githubusercontent.com/lovell/sharp/master/preinstall.sh | sudo bash - + +before_script: + - go get -u github.com/golang/lint/golint + script: - diff -u <(echo -n) <(gofmt -s -d ./) - diff -u <(echo -n) <(go vet ./) @@ -13,6 +19,3 @@ script: after_success: - goveralls -coverprofile=coverage.out -service=travis-ci - -before_install: - - curl -s https://raw.githubusercontent.com/lovell/sharp/master/preinstall.sh | sudo bash - diff --git a/file_test.go b/file_test.go index 5805e74..2144669 100644 --- a/file_test.go +++ b/file_test.go @@ -33,6 +33,6 @@ func TestWrite(t *testing.T) { err = Write("fixtures/test_write_out.jpg", buf) if err != nil { - t.Fatal("Cannot write the file: %#v", err) + t.Fatalf("Cannot write the file: %#v", err) } } diff --git a/fixtures/test.gif b/fixtures/test.gif new file mode 100644 index 0000000..7bf290a Binary files /dev/null and b/fixtures/test.gif differ diff --git a/fixtures/test.pdf b/fixtures/test.pdf new file mode 100644 index 0000000..c14cc56 Binary files /dev/null and b/fixtures/test.pdf differ diff --git a/fixtures/test.svg b/fixtures/test.svg new file mode 100644 index 0000000..679edec --- /dev/null +++ b/fixtures/test.svg @@ -0,0 +1,725 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fixtures/test_gif.jpg b/fixtures/test_gif.jpg new file mode 100644 index 0000000..9dd2def Binary files /dev/null and b/fixtures/test_gif.jpg differ diff --git a/fixtures/test_pdf.jpg b/fixtures/test_pdf.jpg new file mode 100644 index 0000000..8625743 Binary files /dev/null and b/fixtures/test_pdf.jpg differ diff --git a/fixtures/test_svg.jpg b/fixtures/test_svg.jpg new file mode 100644 index 0000000..cf88931 Binary files /dev/null and b/fixtures/test_svg.jpg differ diff --git a/image_test.go b/image_test.go index a69ddd2..54d71f9 100644 --- a/image_test.go +++ b/image_test.go @@ -20,6 +20,66 @@ func TestImageResize(t *testing.T) { Write("fixtures/test_resize_out.jpg", buf) } +func TestImageGifResize(t *testing.T) { + _, err := initImage("test.gif").Resize(300, 240) + if err == nil { + t.Errorf("GIF shouldn't be saved within VIPS.") + } +} + +func TestImagePdfResize(t *testing.T) { + _, err := initImage("test.pdf").Resize(300, 240) + if err == nil { + t.Errorf("PDF cannot be saved within VIPS.") + } +} + +func TestImageSvgResize(t *testing.T) { + _, err := initImage("test.svg").Resize(300, 240) + if err == nil { + t.Errorf("SVG cannot be saved within VIPS.") + } +} + +func TestImageGifToJpeg(t *testing.T) { + i := initImage("test.gif") + options := Options{ + Type: JPEG, + } + buf, err := i.Process(options) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + Write("fixtures/test_gif.jpg", buf) +} + +func TestImagePdfToJpeg(t *testing.T) { + i := initImage("test.pdf") + options := Options{ + Type: JPEG, + } + buf, err := i.Process(options) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + Write("fixtures/test_pdf.jpg", buf) +} + +func TestImageSvgToJpeg(t *testing.T) { + i := initImage("test.svg") + options := Options{ + Type: JPEG, + } + buf, err := i.Process(options) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + Write("fixtures/test_svg.jpg", buf) +} + func TestImageResizeAndCrop(t *testing.T) { buf, err := initImage("test.jpg").ResizeAndCrop(300, 200) if err != nil { diff --git a/metadata_test.go b/metadata_test.go index 87639fd..ceb2b2e 100644 --- a/metadata_test.go +++ b/metadata_test.go @@ -56,13 +56,13 @@ func TestMetadata(t *testing.T) { t.Fatalf("Unexpected image orientation: %d != %d", metadata.Orientation, file.orientation) } if metadata.Alpha != file.alpha { - t.Fatalf("Unexpected image alpha: %s != ", metadata.Alpha, file.alpha) + t.Fatalf("Unexpected image alpha: %t != %t", metadata.Alpha, file.alpha) } if metadata.Profile != file.profile { - t.Fatalf("Unexpected image profile: %s != %s", metadata.Profile, file.profile) + t.Fatalf("Unexpected image profile: %t != %t", metadata.Profile, file.profile) } if metadata.Space != file.space { - t.Fatalf("Unexpected image profile: %s != %s", metadata.Profile, file.profile) + t.Fatalf("Unexpected image profile: %t != %t", metadata.Profile, file.profile) } } } diff --git a/resize_test.go b/resize_test.go index b359abb..96a3a2f 100644 --- a/resize_test.go +++ b/resize_test.go @@ -58,7 +58,7 @@ func TestResizeVerticalImage(t *testing.T) { } if DetermineImageType(image) != test.format { - t.Fatal("Image format is invalid. Expected: %s", test.format) + t.Fatalf("Image format is invalid. Expected: %#v", test.format) } size, _ := Size(image) @@ -99,7 +99,7 @@ func TestResizeCustomSizes(t *testing.T) { } if DetermineImageType(image) != test.format { - t.Fatal("Image format is invalid. Expected: %s", test.format) + t.Fatalf("Image format is invalid. Expected: %#v", test.format) } size, _ := Size(image) diff --git a/type.go b/type.go index 0eba2c2..60722eb 100644 --- a/type.go +++ b/type.go @@ -14,6 +14,12 @@ const ( PNG // TIFF represents the TIFF image type. TIFF + // GIF represents the GIF image type. + GIF + // PDF represents the PDF type. + PDF + // SVG represents the SVG image type. + SVG // MAGICK represents the libmagick compatible genetic image type. MAGICK ) @@ -24,6 +30,9 @@ var ImageTypes = map[ImageType]string{ PNG: "png", WEBP: "webp", TIFF: "tiff", + GIF: "gif", + PDF: "pdf", + SVG: "svg", MAGICK: "magick", } diff --git a/type_test.go b/type_test.go index 68e6d21..e33f1f9 100644 --- a/type_test.go +++ b/type_test.go @@ -15,6 +15,9 @@ func TestDeterminateImageType(t *testing.T) { {"test.jpg", JPEG}, {"test.png", PNG}, {"test.webp", WEBP}, + {"test.gif", GIF}, + {"test.pdf", PDF}, + {"test.svg", SVG}, } for _, file := range files { @@ -36,6 +39,9 @@ func TestDeterminateImageTypeName(t *testing.T) { {"test.jpg", "jpeg"}, {"test.png", "png"}, {"test.webp", "webp"}, + {"test.gif", "gif"}, + {"test.pdf", "pdf"}, + {"test.svg", "svg"}, } for _, file := range files { @@ -53,7 +59,7 @@ func TestIsTypeSupported(t *testing.T) { types := []struct { name ImageType }{ - {JPEG}, {PNG}, {WEBP}, + {JPEG}, {PNG}, {WEBP}, {GIF}, {PDF}, } for _, n := range types { @@ -71,7 +77,8 @@ func TestIsTypeNameSupported(t *testing.T) { {"jpeg", true}, {"png", true}, {"webp", true}, - {"gif", false}, + {"gif", true}, + {"pdf", true}, } for _, n := range types { diff --git a/vips.go b/vips.go index 5829e24..0e25fba 100644 --- a/vips.go +++ b/vips.go @@ -341,6 +341,12 @@ func vipsSave(image *C.VipsImage, o vipsSaveOptions) ([]byte, error) { case PNG: saveErr = C.vips_pngsave_bridge(tmpImage, &ptr, &length, 1, C.int(o.Compression), quality, interlace) break + case GIF: + return nil, errors.New("VIPS cannot save to GIF") + case PDF: + return nil, errors.New("VIPS cannot save to PDF") + case SVG: + return nil, errors.New("VIPS cannot save to SVG") default: saveErr = C.vips_jpegsave_bridge(tmpImage, &ptr, &length, 1, quality, interlace) break @@ -467,6 +473,17 @@ func vipsImageType(bytes []byte) ImageType { (bytes[0] == 0x4D && bytes[1] == 0x4D && bytes[2] == 0x0 && bytes[3] == 0x2A) { return TIFF } + if bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == '8' && + bytes[4] == '9' && bytes[5] == 'a' { + return GIF + } + if bytes[0] == '%' && bytes[1] == 'P' && bytes[2] == 'D' && bytes[3] == 'F' { + return PDF + } + if bytes[0] == '<' && bytes[1] == '?' && bytes[2] == 'x' && bytes[3] == 'm' && + bytes[4] == 'l' && bytes[5] == ' ' { + return SVG + } if HasMagickSupport && strings.HasSuffix(readImageType(bytes), "MagickBuffer") { return MAGICK } diff --git a/vips.h b/vips.h index e3dc1c8..dfdfa0f 100644 --- a/vips.h +++ b/vips.h @@ -30,6 +30,9 @@ enum types { WEBP, PNG, TIFF, + GIF, + PDF, + SVG, MAGICK }; @@ -266,6 +269,14 @@ vips_init_image (void *buf, size_t len, int imageType, VipsImage **out) { } else if (imageType == TIFF) { code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); #if (VIPS_MAJOR_VERSION >= 8) +#if (VIPS_MINOR_VERSION >= 3) + } else if (imageType == GIF) { + code = vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); + } else if (imageType == PDF) { + code = vips_pdfload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); + } else if (imageType == SVG) { + code = vips_svgload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); +#endif } else if (imageType == MAGICK) { code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); #endif