From 27b605858db6d8f5439e9b689f70626801324f02 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Wed, 19 Aug 2015 09:42:15 -0400 Subject: Add image information retrieval method and test --- fpdf.go | 12 ++++++++++++ fpdf_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/fpdf.go b/fpdf.go index c7042c7..470a81b 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2358,6 +2358,18 @@ func (f *Fpdf) RegisterImage(fileStr, tp string) (info *ImageInfoType) { return f.RegisterImageReader(fileStr, tp, file) } +// GetImageInfo returns information about the registered image specified by +// imageStr. If the image has not been registered, an empty structure followed +// by false is returned. The internal error is not modified by this method. +func (f *Fpdf) GetImageInfo(imageStr string) (info ImageInfoType, ok bool) { + var infoPtr *ImageInfoType + infoPtr, ok = f.images[imageStr] + if ok { + info = *infoPtr + } + return +} + // GetXY returns the abscissa and ordinate of the current position. // // Note: the value returned for the abscissa will be affected by the current diff --git a/fpdf_test.go b/fpdf_test.go index 81ff68f..b556713 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -1083,9 +1083,27 @@ func ExampleFpdf_RegisterImage() { pdf.Image(imageFileStr, lf, tp, imgWd, imgHt, false, "", 0, "") } fileStr := exampleFilename("Fpdf_RegisterImage") + // Test the image information retrieval method + infoShow := func(imageStr string) { + imageStr = imageFile(imageStr) + info, ok := pdf.GetImageInfo(imageStr) + if ok { + if info.Width() > 0.0 { + fmt.Printf("Image %s is registered\n", imageStr) + } else { + fmt.Printf("Incorrect information for image %s\n", imageStr) + } + } else { + fmt.Printf("Image %s is not registered\n", imageStr) + } + } + infoShow(fileList[0]) + infoShow("foo.png") err := pdf.OutputFileAndClose(fileStr) summary(err, fileStr) // Output: + // Image image/logo-gray.png is registered + // Image image/foo.png is not registered // Successfully generated pdf/Fpdf_RegisterImage.pdf } -- cgit v1.2.1-24-ge1ad