本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
golang 实现 pdf 转高清晰度 jpeg的处理方法
我是陈晓 · 256浏览 · 发布于2022-10-08 +关注

这篇文章主要介绍了golang 实现 pdf 转高清晰度 jpeg,下面主要介绍Golang 代码使用方法及Golang PDF转JPEG的详细代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


ImageMagick 是一个功能丰富的图片处理工具

具体安装方式可以参考官方,MacOS 上可以通过 homebrew 安装

brew install imagemagick@6

homebrew 最新的源是 7.* 版本,由于我的场景需要在 linux 部署,linux 的 apt 源目前是 6.9, 为了保持一致,所以使用的是旧版本

命令行使用

convert -density 128 1.pdf -quality 100 -alpha remove output.jpeg

Golang 代码使用

核心要点:

pdf 需要去除 alpha 通道,然后背景色设置白色(你可以可以根据需求设置其它颜色)留意内存泄露,因为这是 cgo,一旦泄露就 gg 了。比如你没有 mw.RemoveImage()上述的 density 设置就是 resolution, 需要设置一个合理的值,否则转换的图片就会糊

golang 的 binding 安装方式可以按照 github 介绍 https://github.com/gographics/imagick

package main
 import (
    "fmt"
    "io/ioutil"
    "runtime"
    "runtime/debug"
    "time"
     "gopkg.in/gographics/imagick.v2/imagick"
)
 func main() {
    imagick.Initialize()
    //defer imagick.Terminate()
    data, _ := ioutil.ReadFile("1.pdf")
     start := time.Now()
    for i := 0; i < 100; i++ {
        if i%10 == 0 {
            fmt.Println("i", i)
        }
        go createCoverImage(data, "1-1.jpeg")
    }
    fmt.Println("duration", time.Now().Sub(start))
    PrintMemUsage()
    debug.FreeOSMemory()
    PrintMemUsage()
    time.Sleep(10 * time.Second)
    imagick.Terminate()
    fmt.Println("free cgo")
    PrintMemUsage()
    time.Sleep(10 * time.Minute)
}
 // PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage() {
    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    // For info on each, see: https://golang.org/pkg/runtime/#MemStats
    fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
    fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
    fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
    fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
 func bToMb(b uint64) uint64 {
    return b / 1024 / 1024
}
 func clearImagickWand(mw *imagick.MagickWand) {
    mw.RemoveImage()
    mw.Clear()
    mw.Destroy()
    //runtime.SetFinalizer(mw, nil)
    mw = nil
}
 func createCoverImage(data []byte, coverPathName string) bool {
    //sourceImagePath := getSourceImageForCover(filepath.Dir(pathNoExtension))
    mw := imagick.NewMagickWand()
    defer clearImagickWand(mw)
    mw.SetResolution(192, 192)
    err := mw.ReadImageBlob(data)
    if err != nil {
        return false
    }
     //length := mw.GetImageIterations()
    //fmt.Println("length", length)
    //fmt.Println("width", mw.GetImageWidth())
    //fmt.Println("height", mw.GetImageHeight())
     pix := imagick.NewPixelWand()
    pix.SetColor("white")
    //mw.SetBackgroundColor(pix)
    mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE)
    mw.SetImageFormat("jpeg")
     err = mw.WriteImage(coverPathName)
    if err != nil {
        return false
    }
    _ = mw.GetImageBlob()
     return true
}

特别地,需要设置两个环境变量

export CGO_CFLAGS_ALLOW='-Xpreprocessor'
export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig" # 取决于 brew install 的输出

Golang PDF转JPEG

package main
 import (
    "fmt"
    "os"
     "github.com/h2non/bimg"
)
 func main() {
    buffer, err := bimg.Read("test.pdf")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
     newImage, err := bimg.NewImage(buffer).Convert(bimg.JPEG)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
     if bimg.NewImage(newImage).Type() == "jpeg" {
        fmt.Fprintln(os.Stderr, "The image was converted into jpeg")
    }
     bimg.Write("test.jpg", newImage)
}

到此这篇关于golang 实现 pdf 转高清晰度 jpeg的文章就介绍到这了


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 963浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
我是一名在上海一家互联网公司上班,专注技术开发工作等。
小鸟云服务器
扫码进入手机网页