summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Westcott <joewestcott@users.noreply.github.com>2019-08-30 14:32:24 +0100
committerJoe Westcott <joewestcott@users.noreply.github.com>2019-08-30 14:32:24 +0100
commitf8eb4ab245f4dfe947b595efb0b2e2c3b3a37625 (patch)
tree8e7f8fdc1d5d265debc4dd88bc11564d515a0dd5
parente27b17d1defd471e6f0c0c422d2fa8ce9b9c3bb6 (diff)
Prevent inserting duplicate images
-rw-r--r--fpdf.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/fpdf.go b/fpdf.go
index 24e79bc..620e742 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -4244,11 +4244,31 @@ func (f *Fpdf) putimages() {
for key = range f.images {
keyList = append(keyList, key)
}
+
+ // Sort the keyList []string by the corrosponding image's width.
if f.catalogSort {
sort.SliceStable(keyList, func(i, j int) bool { return f.images[keyList[i]].w < f.images[keyList[j]].w })
}
+
+ // Maintain a list of inserted image SHA-1 hashes, with their
+ // corresponding object ID number.
+ insertedImages := map[string]int{}
+
for _, key = range keyList {
- f.putimage(f.images[key])
+ image := f.images[key]
+
+ // Check if this image has already been inserted using it's SHA-1 hash.
+ insertedImageObjN, isFound := insertedImages[image.i]
+
+ // If found, skip inserting the image as a new object, and
+ // use the object ID from the insertedImages map.
+ // If not, insert the image into the PDF and store the object ID.
+ if isFound {
+ image.n = insertedImageObjN
+ } else {
+ f.putimage(image)
+ insertedImages[image.i] = image.n
+ }
}
}