diff --git a/dports/graphics/freeimage/Portfile b/dports/graphics/freeimage/Portfile
index bbf2a5a..39462c9 100644
--- a/dports/graphics/freeimage/Portfile
+++ b/dports/graphics/freeimage/Portfile
@@ -4,7 +4,8 @@
 PortSystem 1.0
 
 name            freeimage
-version         3.12.0
+version         3.13.1
+license         FreeImage-1.0 GPL-2
 categories      graphics
 maintainers     toby
 description     Library for FreeImage, a dependency-less graphics library
@@ -20,28 +21,81 @@ distname        FreeImage[strsed ${version} {g/\.//}]
 use_zip         yes
 worksrcdir      FreeImage
 
-checksums       md5 47b259102f776a4bcd7affc00942f3b4 \
-                sha1 d9a5efc9590cb45e176c7e5552afef961594a1cb \
-                rmd160 421ddbd81343d14561ade844c67c31802ab444ad
+checksums       md5     a2e20b223a2cf6a5791cc47686364e99 \
+                sha1    52ba4453aa9682c57104c3420e58f843aaa6ab61 \
+                rmd160  b3f3e7791ded7d3ce76eb2d9c2a5acb5687a16ee
 
-patchfiles      patch-Makefile.gnu
+patchfiles      patch-Makefile.gnu.diff
 
-configure {
-    reinplace "s|__PREFIX__|${prefix}|" ${worksrcpath}/Makefile.gnu
+post-patch {
+    # Libraries extension is ".dylib" on Darwin, not ".so"
+    reinplace {s/\.so/.dylib/} ${worksrcpath}/Makefile.gnu
 
-    reinplace "s|__LDFLAGS__|${configure.ldflags}|" ${worksrcpath}/Makefile.gnu
+    # A dash separate the library name from its version on Darwin
+    reinplace /^SHAREDLIB/s/-/./ ${worksrcpath}/Makefile.gnu
 
-    reinplace "s|__CC__|${configure.cc}|" ${worksrcpath}/Makefile.gnu
-    reinplace "s|__CFLAGS__|${configure.cflags}|" ${worksrcpath}/Makefile.gnu
+    # Use libtool(1) instead of ar(1)
+    reinplace {s/\$(AR) r/libtool -o/} ${worksrcpath}/Makefile.gnu
 
-    reinplace "s|__CXX__|${configure.cxx}|" ${worksrcpath}/Makefile.gnu
-    reinplace "s|__CXXFLAGS__|${configure.cxxflags}|" ${worksrcpath}/Makefile.gnu
+    # Do not force installation as root
+    reinplace {s/-o root -g root//} ${worksrcpath}/Makefile.gnu
 }
 
+use_configure   no
+
+set CC "${configure.cc} ${configure.cflags}"
+set CXX "${configure.cxx} ${configure.cxxflags}"
+set LDFLAGS ${configure.ldflags}
+set VERLIBNAME libfreeimage.[lindex [split ${version} .] 0].dylib
+
 build.target    FreeImage
-build.args      -f Makefile.gnu
+build.args      -f Makefile.gnu \
+                PREFIX=${prefix} \
+                VERLIBNAME=${VERLIBNAME}
+
+pre-build {
+    build.args-append \
+        CC="${CC}" \
+        CXX="${CXX}" \
+        LDFLAGS="${LDFLAGS}"
+}
+
+destroot.args   -f Makefile.gnu \
+                INCDIR=${destroot}${prefix}/include \
+                INSTALLDIR=${destroot}${prefix}/lib \
+                PREFIX=${prefix} \
+                VERLIBNAME=${VERLIBNAME}
+
+post-destroot {
+    set docdir ${prefix}/share/doc/${name}
+    xinstall -d ${destroot}${docdir}
+    xinstall -m 644 -W ${worksrcpath} Whatsnew.txt license-fi.txt \
+        license-gpl.txt ${destroot}${docdir}
+    copy ${worksrcpath}/Examples ${destroot}${docdir}/examples
+}
 
-destroot.args   -f Makefile.gnu
+test.run    yes
+test.cmd    ./TestAPI
+test.dir    ${worksrcpath}/TestAPI
+test.target
+
+pre-test {
+    # Use correct compiler and flags when compiling test
+    reinplace s:g++:${CXX}: ${test.dir}/Makefile
+
+    # Use headers and libraries in source directories as Dist is not used
+    reinplace s:-I../Dist/:-I../Source/: ${test.dir}/Makefile
+    reinplace s:Dist/:: ${test.dir}/Makefile
+
+    # Build the test
+    system "make -C ${test.dir}"
+}
+
+variant universal {
+    append CC " ${configure.universal_cflags}"
+    append CXX " ${configure.universal_cxxflags}"
+    append LDFLAGS " ${configure.universal_ldflags}"
+}
 
 livecheck.type  regex
 livecheck.regex {FreeImage (\d+(?:\.\d+)*) released}
diff --git a/dports/graphics/freeimage/files/patch-Makefile.gnu b/dports/graphics/freeimage/files/patch-Makefile.gnu
deleted file mode 100644
index 8a84068..0000000
--- a/dports/graphics/freeimage/files/patch-Makefile.gnu
+++ /dev/null
@@ -1,69 +0,0 @@
---- Makefile.gnu.orig	2009-06-24 17:19:47.000000000 -0700
-+++ Makefile.gnu	2009-06-24 17:57:28.000000000 -0700
-@@ -3,10 +3,14 @@
- # This file can be generated by ./gensrclist.sh
- include Makefile.srcs
- 
-+CC = __CC__
-+CXX = __CXX__
-+LDFLAGS = __LDFLAGS__
-+
- # General configuration variables:
--DESTDIR ?= /
--INCDIR ?= $(DESTDIR)/usr/include
--INSTALLDIR ?= $(DESTDIR)/usr/lib
-+DESTDIR ?= 
-+INCDIR ?= $(DESTDIR)__PREFIX__/include
-+INSTALLDIR ?= $(DESTDIR)__PREFIX__/lib
- 
- # Converts cr/lf to just lf
- DOS2UNIX = dos2unix
-@@ -15,9 +19,9 @@
- 
- MODULES = $(SRCS:.c=.o)
- MODULES := $(MODULES:.cpp=.o)
--CFLAGS ?= -O3 -fPIC -fexceptions -fvisibility=hidden
-+CFLAGS ?= __CFLAGS__ -fPIC -fexceptions -fvisibility=hidden
- CFLAGS += $(INCLUDE)
--CXXFLAGS ?= -O3 -fPIC -fexceptions -fvisibility=hidden -Wno-ctor-dtor-privacy
-+CXXFLAGS ?= __CXXFLAGS__ -fPIC -fexceptions -fvisibility=hidden -Wno-ctor-dtor-privacy
- CXXFLAGS += $(INCLUDE)
- 
- ifeq ($(shell sh -c 'uname -m 2>/dev/null || echo not'),x86_64)
-@@ -27,9 +31,9 @@
- 
- TARGET  = freeimage
- STATICLIB = lib$(TARGET).a
--SHAREDLIB = lib$(TARGET)-$(VER_MAJOR).$(VER_MINOR).so
--LIBNAME	= lib$(TARGET).so
--VERLIBNAME = $(LIBNAME).$(VER_MAJOR)
-+SHAREDLIB = lib$(TARGET).$(VER_MAJOR).$(VER_MINOR).dylib
-+LIBNAME	= lib$(TARGET).dylib
-+VERLIBNAME = lib$(TARGET).$(VER_MAJOR).dylib
- HEADER = Source/FreeImage.h
- 
- 
-@@ -55,16 +59,18 @@
- 	$(CXX) $(CXXFLAGS) -c $< -o $@
- 
- $(STATICLIB): $(MODULES)
--	$(AR) r $@ $(MODULES)
-+	/usr/bin/libtool -o $@ $(MODULES)
- 
- $(SHAREDLIB): $(MODULES)
--	$(CC) -shared -Wl,-soname,$(VERLIBNAME) $(LDFLAGS) -o $@ $(MODULES) $(LIBRARIES)
-+	$(CC) -dynamiclib -install_name $(INSTALLDIR)/$(VERLIBNAME) -compatibility_version $(VER_MAJOR) -current_version $(VER_MAJOR).$(VER_MINOR) $(LDFLAGS) -o $@ $(MODULES) $(LIBRARIES)
- 
- install:
- 	install -d $(INCDIR) $(INSTALLDIR)
--	install -m 644 -o root -g root $(HEADER) $(INCDIR)
--	install -m 644 -o root -g root $(STATICLIB) $(INSTALLDIR)
--	install -m 755 -o root -g root $(SHAREDLIB) $(INSTALLDIR)
-+	install -m 644 $(HEADER) $(INCDIR)
-+	install -m 644 $(STATICLIB) $(INSTALLDIR)
-+	install -m 755 $(SHAREDLIB) $(INSTALLDIR)
-+	ln -s $(SHAREDLIB) $(INSTALLDIR)/$(VERLIBNAME)
-+	ln -s $(SHAREDLIB) $(INSTALLDIR)/$(LIBNAME)
- 
- clean:
- 	rm -f core Dist/*.* u2dtmp* $(MODULES) $(STATICLIB) $(SHAREDLIB) $(LIBNAME)
diff --git a/dports/graphics/freeimage/files/patch-Makefile.gnu.diff b/dports/graphics/freeimage/files/patch-Makefile.gnu.diff
new file mode 100644
index 0000000..e856daa
--- /dev/null
+++ b/dports/graphics/freeimage/files/patch-Makefile.gnu.diff
@@ -0,0 +1,19 @@
+--- Makefile.gnu.orig	2010-01-13 16:32:29.000000000 +0100
++++ Makefile.gnu	2010-01-13 17:17:12.000000000 +0100
+@@ -58,13 +58,15 @@
+ 	$(AR) r $@ $(MODULES)
+ 
+ $(SHAREDLIB): $(MODULES)
+-	$(CC) -shared -Wl,-soname,$(VERLIBNAME) $(LDFLAGS) -o $@ $(MODULES) $(LIBRARIES)
++	$(CC) -dynamiclib -install_name $(PREFIX)/lib/$(VERLIBNAME) -compatibility_version $(VER_MAJOR) -current_version $(VER_MAJOR).$(VER_MINOR) -o $@ $(MODULES) $(LIBRARIES)
+ 
+ install:
+ 	install -d $(INCDIR) $(INSTALLDIR)
+ 	install -m 644 -o root -g root $(HEADER) $(INCDIR)
+ 	install -m 644 -o root -g root $(STATICLIB) $(INSTALLDIR)
+ 	install -m 755 -o root -g root $(SHAREDLIB) $(INSTALLDIR)
++	ln -sf $(PREFIX)/lib/$(SHAREDLIB) $(INSTALLDIR)/$(VERLIBNAME)
++	ln -sf $(PREFIX)/lib/$(SHAREDLIB) $(INSTALLDIR)/$(LIBNAME)
+ 
+ clean:
+ 	rm -f core Dist/*.* u2dtmp* $(MODULES) $(STATICLIB) $(SHAREDLIB) $(LIBNAME)
