diff --git a/resources/PDF-2.0-with-offset-start.pdf b/resources/PDF-2.0-with-offset-start.pdf new file mode 100644 index 00000000..4cf62cc2 Binary files /dev/null and b/resources/PDF-2.0-with-offset-start.pdf differ diff --git a/vips/foreign.go b/vips/foreign.go index 80f643b4..711baaaa 100644 --- a/vips/foreign.go +++ b/vips/foreign.go @@ -6,13 +6,12 @@ import ( "bytes" "encoding/xml" "fmt" + "golang.org/x/image/bmp" + "golang.org/x/net/html/charset" "image/png" "math" "runtime" "unsafe" - - "golang.org/x/image/bmp" - "golang.org/x/net/html/charset" ) // SubsampleMode correlates to a libvips subsample mode @@ -154,14 +153,14 @@ func DetermineImageType(buf []byte) ImageType { return ImageTypeHEIF } else if isSVG(buf) { return ImageTypeSVG - } else if isPDF(buf) { - return ImageTypePDF } else if isBMP(buf) { return ImageTypeBMP } else if isJP2K(buf) { return ImageTypeJP2K } else if isJXL(buf) { return ImageTypeJXL + } else if isPDF(buf) { + return ImageTypePDF } else { return ImageTypeUnknown } @@ -240,7 +239,10 @@ func isSVG(buf []byte) bool { var pdf = []byte("\x25\x50\x44\x46") func isPDF(buf []byte) bool { - return bytes.HasPrefix(buf, pdf) + if len(buf) <= 1024 { + return bytes.Contains(buf, pdf) + } + return bytes.Contains(buf[:1024], pdf) } var bmpHeader = []byte("BM") diff --git a/vips/foreign_test.go b/vips/foreign_test.go index 38a40d32..d905865b 100644 --- a/vips/foreign_test.go +++ b/vips/foreign_test.go @@ -106,6 +106,17 @@ func Test_DetermineImageType__PDF(t *testing.T) { assert.Equal(t, ImageTypePDF, imageType) } +func Test_DetermineImageType__PDF_1(t *testing.T) { + Startup(&Config{}) + + buf, err := os.ReadFile(resources + "PDF-2.0-with-offset-start.pdf") + assert.NoError(t, err) + assert.NotNil(t, buf) + + imageType := DetermineImageType(buf) + assert.Equal(t, ImageTypePDF, imageType) +} + func Test_DetermineImageType__BMP(t *testing.T) { Startup(&Config{}) diff --git a/vips/image_golden_test.go b/vips/image_golden_test.go index 5fb22dd3..a2276d37 100644 --- a/vips/image_golden_test.go +++ b/vips/image_golden_test.go @@ -954,6 +954,14 @@ func TestImage_Pixelate(t *testing.T) { nil, nil) } +func TestPDF_WithOffsetStart(t *testing.T) { + goldenTest(t, resources+"PDF-2.0-with-offset-start.pdf", + nil, func(img *ImageRef) { + assert.Equal(t, 612, img.Width()) + assert.Equal(t, 396, img.Height()) + }, nil) +} + func testWebpOptimizeIccProfile(t *testing.T, exportParams *WebpExportParams) []byte { return goldenTest(t, resources+"has-icc-profile.png", func(img *ImageRef) error { diff --git a/vips/image_test.go b/vips/image_test.go index 9c6090bc..8466aaac 100644 --- a/vips/image_test.go +++ b/vips/image_test.go @@ -1190,6 +1190,17 @@ func TestImageRef_SetGamma(t *testing.T) { require.NoError(t, err) } +func Test_NewImageFromFile(t *testing.T) { + Startup(nil) + + image, err := NewImageFromFile(resources + "PDF-2.0-with-offset-start.pdf") + require.NoError(t, err) + + assert.Equal(t, ImageTypePDF, image.originalFormat) + assert.Equal(t, ImageTypePDF, image.format) + assert.Equal(t, 1, image.Pages()) +} + // TODO unit tests to cover: // NewImageFromReader failing test // NewImageFromFile failing test