1、首先安装imagemagick
去 http://mirror.aarnet.edu.au/pub/imagemagick/ 寻找最新版本
我的是 http://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.9.10-49.tar.gz
wget ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.9.10-49.tar.gz
tar -xzvf ImageMagick-6.9.10-49.tar.gz
cd ImageMagick-6.9.10-49
./configure --prefix=/usr/local/imagemagick
make
make install
2、安装Imagick
去 http://pecl.php.net/package/imagick 寻找最新版本
例如:http://pecl.php.net/get/imagick-3.4.4.tgz
wget http://pecl.php.net/get/imagick-3.4.4.tgz
tar -xzvf imagick-3.4.4.tgz
cd imagick-3.4.4
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick
make
make install
make install 执行完的结果:
Installing shared extensions: /usr/local/data/php/lib/php/extensions/no-debug-non-zts-20131226/
Installing header files: /usr/local/data/php/include/php/
imagick.so 被生成到/usr/local/data/php/lib/php/extensions/no-debug-non-zts-20131226/
接下来我们把imagick.so添加到php.ini然后重启PHP 就可以看见我们的环境中已经安装上依赖啦~
但是我接下来的运行却不顺利,通过报错得知是ghostscript版本过低 处理不了,那么我们也安装最新版
3.1、替换最新版(之前没有安装ghostscript的小伙伴看3.2)
照例去寻找最新版:https://www.ghostscript.com/download/gsdnld.html
获取自己PHP可用的版本
wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs927/ghostscript-9.27-linux-x86_64.tgz
tar -xzvf ghostscript-9.27-linux-x86_64.tgz
cd ghostscript-9.27-linux-x86_64
//这个时候文件夹下的Ghostscript 9.27 for Linux x86是可以直接使用的,我们去替换掉/usr/bin下面的gs就可以了
mv ./Ghostscript 9.27 for Linux x86 /usr/bin/gs
3.2、安装最新版
寻找最新版:https://github.com/ArtifexSoftware/ghostpdl-downloads/releases
wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs927/ghostscript-9.27.tar.gz
tar -xzvf ghostscript-9.27.tar.gz
cd ghostscript-9.27
./configure --prefix=/usr/bin
make
make install
4、PHP代码运用:
/**
* pdfToJpg
* @param $pdfPath 待处理的PDF文件路径
* @param $path 待保存的图片路径
* @return 保存好的图片路径和文件名
*/
public function pdfToJpg($pdfPath,$Path){
//判断是否安装imagick
if(!extension_loaded('imagick')){
return false;
}
//判断pdf是否存在
if(!file_exists($pdfPath)){
return false;
}
//实例化imagick
$IM = new imagick();
$IM->setResolution(120,120);
$IM->setCompressionQuality(100);
$IM->readImage($pdfPath);
//添加白色底色
$IM->borderImage(new ImagickPixel('white'),1,1);
//只生成最后一页
//$IM->setImageFormat('jpg');
//$Filename = $Path.'/'.md5(time()).'.jpg';
//if($IM->writeImage($Filename) == true){
// $Return = $Filename;
//}
//每页都生成
foreach ($IM as $Key => $Var){
$Var->setImageFormat('png');
$Filename = $Path.'/'.md5($Key.time()).'.png';
if($Var->writeImage($Filename) == true){
$Return[] = $Filename;
}
}
return $Return;
}
以上就是centos linux下使用PHP利用imagick截取pdf生成图片的小小案例啦~