Skip to content

Commit 9d2368f

Browse files
author
Austin Hyde
committed
Support embedding GIF images by way of converting to PNG
1 parent 4d028a6 commit 9d2368f

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/main/java/com/koadweb/javafpdf/FPDF.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
package com.koadweb.javafpdf;
2323

24+
import com.koadweb.javafpdf.util.Compressor;
2425
import java.awt.color.ColorSpace;
2526
import java.awt.image.BufferedImage;
2627
import java.io.File;
@@ -37,11 +38,8 @@
3738
import java.util.Locale;
3839
import java.util.Map;
3940
import java.util.Set;
40-
4141
import javax.imageio.ImageIO;
4242

43-
import com.koadweb.javafpdf.util.Compressor;
44-
4543
/**
4644
* Faithful Java port of <a href="http://www.fpdf.org">FPDF for PHP</a>.
4745
*
@@ -1814,6 +1812,7 @@ public float getY() {
18141812
* link identifier for the image
18151813
* @throws IOException
18161814
*/
1815+
@SuppressWarnings("fallthrough")
18171816
public void Image(final String file, final Coordinate coords, final float w, final float h, final ImageType type,
18181817
final int link) throws IOException {
18191818
Map<String, Object> info = null;
@@ -1830,12 +1829,20 @@ public void Image(final String file, final Coordinate coords, final float w, fin
18301829
} else {
18311830
type1 = type;
18321831
}
1833-
if (ImageType.PNG.equals(type1)) {
1834-
info = this._parsepng(new File(file));
1835-
} else if (ImageType.JPEG.equals(type1)) {
1836-
info = this._parsejpg(new File(file));
1837-
} else {
1838-
throw new IOException("Image type not supported.");
1832+
File f = new File(file);
1833+
switch (type1) {
1834+
case GIF:
1835+
// gifs: convert to png first
1836+
ImageIO.write(ImageIO.read(f), "png", f);
1837+
// fallthrough!
1838+
case PNG:
1839+
info = this._parsepng(f);
1840+
break;
1841+
case JPEG:
1842+
info = this._parsejpg(f);
1843+
break;
1844+
default:
1845+
throw new IOException("Image type not supported.");
18391846
}
18401847
// FIXME no support for other formats
18411848
this.images.put(file, info);

src/main/java/com/koadweb/javafpdf/ImageType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public enum ImageType {
3232
/** Portable Network Graphics image. */
3333
PNG,
3434
/** Joint Photographic Experts Group image. */
35-
JPEG;
35+
JPEG,
36+
/** Graphics Interchange Format image. */
37+
GIF;
3638
}

0 commit comments

Comments
 (0)