一、centos下环境部署
步骤1:部署ImageMagick
(1)在http://www.imagemagick.org/download/上找到合适的版本,然后下载,我选择的版本是ImageMagick-6.9.9-36
(2)解压刚才下载的文件: tar xvf ImageMagick-6.9.9-36.tar.gz
(3)进入解压目录:cd ImageMagick-6.9.9-36
(4)检查配置:./configure
(5)安装jpeg:yum install libjpeg* libpng* freetype* gd*
(6)安装ImageMagick:make install
输入convert -resize 100x100 hua.jpg hua1.jpg 如果执行成功,表明安装成功。我在安装的时候报错如下:
{[Error:Command failed:convert:nodecode delegate for this image format `JPEG' @ error/constitute.c/ReadImage/501.
convert: no images defined `/home/node/head/I/IMG_0005.JPG' @ error/convert.c/ConvertImageCommand/3230.
] code: 1, signal: null }
解决办法:重装Imagemagick
make uninstall
./configure
make
make install
再次调用成功。
(7)环境变量配置: 进入/etc/profile,写入
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
export MAGICK_CODER_MODULE_PATH=/data/download/ImageMagick-6.9.9-36/coders/
其中MAGICK_CODER_MODULE_PATH指ImageMagick-6.9.9-36安装目录下的coders(需要将/usr/local/lib下面的.so复制到该路径下),PKG_CONFIG_PATH路径下的内容如图中所示:
步骤2:部署GhostScript
(1)下载安装 GhostScript,官网地址:http://ghostscript.com/download/gsdnld.html。我这里下载的是ghostscript-9.22-linux-x86_64.tgz
(2)解压刚才下载的文件: tar xvf ghostscript-9.22-linux-x86_64.tgz
(3)进入解压目录,将gs直接复制到 /usr/bin 目录下
(4)进入/usr/bin 目录中,chmod 777 gs
(5)输入命令 gs -version 如果有如下输出,说明安装成功,打印的是版本:
二、代码实现
步骤1:下载imagick
步骤2:go实现代码如下
package main
import (
"os"
"strconv"
"gopkg.in/gographics/imagick.v2/imagick"
)
func main() {
ConvertPdfToImage("test.pdf", 800, 1212, 200, 85)
}
//ConvertPdfToImage 转换pdf为图片格式
//@resolution:扫描精度
//@CompressionQuality:图片质量: 1~100
func ConvertPdfToImage(bookname string, pageWidth uint, pageHeight uint, resolution float64, compressionQuality uint) (err error) {
imagick.Initialize()
defer imagick.Terminate()
mw := imagick.NewMagickWand()
//defer mw.Clear()
defer mw.Destroy()
if err := mw.SetResolution(resolution, resolution); err != nil {
println("扫描精度设置失败")
return err
}
if err := mw.ReadImage(bookname); err != nil {
println("文件读取失败")
return err
}
var pages = int(mw.GetNumberImages())
println("页数:", pages)
//裁剪会使页数增加
addPages := 0
path := ""
for i := 0; i < pages; i++ {
mw.SetIteratorIndex(i) // This being the page offset
//压平图像,去掉alpha通道,防止JPG中的alpha变黑,用在ReadImage之后
if err := mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_FLATTEN); err != nil {
println("图片")
return err
}
mw.SetImageFormat("jpg")
mw.SetImageCompression(imagick.COMPRESSION_JPEG)
mw.SetImageCompressionQuality(compressionQuality)
//如果width>height ,就裁剪成两张
pWidth := mw.GetImageWidth()
pHeight := mw.GetImageHeight()
//需要裁剪
if pWidth > pHeight {
//mw.ResizeImage(pageWidth*2, pageHeight, imagick.FILTER_UNDEFINED, 1.0)
mw.ThumbnailImage(pageWidth*2, pageHeight)
tempImage := mw.GetImageFromMagickWand()
leftMw := imagick.NewMagickWandFromImage(tempImage) //由于返回的是指针,需要提前初始化,不然写完左半业tempImage就变了
//左半页
mw.CropImage(pageWidth, pageHeight, 0, 0)
path = "./book/page" + strconv.Itoa(i+addPages) + ".jpg"
mw.WriteImage(path)
//右半页
leftMw.SetImageFormat("jpg")
leftMw.SetImageCompression(imagick.COMPRESSION_JPEG)
leftMw.SetImageCompressionQuality(compressionQuality)
leftMw.CropImage(pageWidth, pageHeight, int(pageWidth), 0)
addPages++
path = "./book/page" + strconv.Itoa(i+addPages) + ".jpg"
leftMw.WriteImage(path)
leftMw.Destroy()
} else {
mw.ThumbnailImage(pageWidth, pageHeight)
path = "./book/page" + strconv.Itoa(i+addPages) + ".jpg"
mw.WriteImage(path)
}
}
println("转换完毕!")
os.Exit(0) //模拟退出程序,删掉!
return nil
}
三、问题解决
在运行过程中报错如下:
问题一:../gopkg.in/gographics/imagick.v2/imagick/drawing_wand.go:39: undefined: runtime.KeepAlive
解决:go版本过低导致
问题二:go build gopkg.in/gographics/imagick.v2/imagick: invalid flag in pkg-config --cflags: -fopenmp
解决:go版本原因。
问题三:/tmp/go-build695941684/command-line-arguments/_obj/exe/pdfconvert: error while loading shared libraries: libMagickWand-6.Q16.so.5: cannot open shared object file: No such file or directory
解决:进入目录/user/local/lib中,运行ldconfig /user/local/lib。即解决。
参考文章:http://blog.csdn.net/gaofuqi/article/details/26698547
http://blog.csdn.net/lisaem/article/details/50330953
http://blog.csdn.net/catoop/article/details/42677021
http://blog.csdn.net/arackethis/article/details/42241471