Msys2 is required to build ImageMagick dll. You can download from Msys2.
Follow the install guide described in the page.
Open msys2 shell.
Run commands below.
If you want to manually confirm installation, please remove the --noconfirm
option.
-
Refresh package database
pacman -Syuu
-
install gcc
pacman -S --noconfirm mingw-w64-x86_64-gcc
-
install pkg-config
pacman -S --noconfirm mingw-w64-x86_64-pkg-config
-
install zlib
pacman -S --noconfirm mingw-w64-x86_64-zlib
-
install imagemagick
pacman -S --noconfirm mingw-w64-x86_64-imagemagick
we install the latest ImageMagick version here, which is 7.x . This is very important.
There are many ways to set env variables. Through Windows GUI or by command. In this instruction,
we use PowerShell
. If you are not sure about the right command in your shell environment, please
use Windows GUI.
-
enable msys2 shell to inherit environment variables to enable calling
go build
command in msys2 shell from windows. setMSYS2_PATH_TYPE
toinherit
$env:MSYS2_PATH_TYPE="inherit"
-
add
msys2_shell.cmd
toPath
to enable invoking msys2 shell from anywhere. Filemsys2_shell.cmd
is in the root of msys2 install directory, just add it to your systemPath
variable.$env:Path += ";<your_msys2_root>"
-
set decoders variable. This is very important for building. The decoders need to be included at build time for your code to recognize different image formats.
$env:MAGICK_CODER_MODULE_PATH="<your_msys2_root>/mingw64/lib/ImageMagick-7.x.x/modules-Q16HDRI/coders"
If you fail to set this variable, no error would be reported at build time, but this might cause
no decode delegate for this image format
error at run time.
-
invoke the mingw64 shell in msys2 in your
GOPATH
or any directory withgo.mod
.msys2_shell.cmd -mingw64 -here
Commands below should be finished in the invoked mingw64 shell.
-
check for
ImageMagick
version. Please note the version here. Go binding version differs from differentImageMagick
version. if any error occurs, please try to reinstallImageMagic
.convert --version
-
check for
pkg-config
recognizing theImageMagick
header file. if any error occurs, please try to reinstallImageMagic
andpkg-config
in step 2.pkg-config --cflags --libs MagickWand
-
build go binding.
Please choose the correct binding version for your installed
ImageMagick
version.master (tag v2.x.x): 6.9.1-7 <= ImageMagick <= 6.9.9-35 im-7 (tag v3.x.x): 7.x <= ImageMagick <= 7.x legacy (tag v1.x.x): 6.7.x <= ImageMagick <= 6.8.9-10
gopkg.in/gographics/imagick.v2/imagick gopkg.in/gographics/imagick.v3/imagick gopkg.in/gographics/imagick.v1/imagick
in this instruction, we should install
v3
, as it aligned with ImageMagick7.go build gopkg.in/gographics/imagick.v3/imagick
If any reported header file error like
fatal error: wand/MagickWand.h: No such file or directory
Please check that you are using the correct binding version to match the installed
ImageMagick
version. -
install the package.
go get gopkg.in/gographics/imagick.v3/imagick
-
every time you build or run your code, you should do it in the invoked
mingw64
shell. -
If you meet some errors like this
undefined references to __errno in errno_itself
It may be caused by a badly cached cgo file. If you build the wrong version for your
ImageMagick
, you might meet this error. Please clean the cache:go clean -cache
then choose the correct version to rebuild. option
-a
here is to force rebuild the package to prevent using the cached cgo file.go build -a gopkg.in/gographics/imagick.<right-version>/imagick
then install again
go get gopkg.in/gographics/imagick.<right-version>/imagick