一、linux 安装
1、yum安装
-------------------php73 imagick扩展安装--------------------------
1、需要先安装
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install 'dnf-command(config-manager)'
dnf config-manager --set-enabled powertools
2、ImageMagick安装
yum install ImageMagick ImageMagick-devel ImageMagick-perl
convert --version
3、php73 imagick扩展安装
#yum --enablerepo=remi install php73-php php73-php-pear php73-php-bcmath php73-php-pecl-jsond-devel php73-php-mysqlnd php73-php-gd php73-php-common php73-php-fpm php73-php-intl php73-php-cli php73-php php73-php-xml php73-php-opcache php73-php-pecl-apcu php73-php-pdo php73-php-gmp php73-php-process php73-php-pecl-imagick php73-php-devel php73-php-mbstring php73-php-zip php73-php-ldap php73-php-imap php73-php-pecl-mcrypt
yum install php73-php-pecl-imagick
4、在php.ini中添加
extension=imagick.so
-------------------php73 imagick扩展安装--------------------------
2、手动安装
分别下载imagick、ImageMagick和ghostscript。下载地址如下:
imagick下载地址
imagemagick下载地址
ghostscript下载地址
安装软件包
tar xvzf ImageMagick.tar.gz
cd ImageMagick
./configure
ldconfig /usr/local/lib
make && make install
cd imagick-3.4.3
phpize
./configure --with-php-config=/usr/local/bin/php-config --with-imagick=/usr/local/etc/php/conf.d/ImageMagick
make && make install
tar xvzf ghostscript-9.53.3.tar.gz
cd ghostscript-9.53.3
./configure
make && make install
3、Docker内安装
#安装ImageMagick
sudo apt-get install imagemagick
#安装imagemagick 的lib 供php调用
sudo apt-get install libmagick++-dev
#调用当前的pecl安装imagick
pecl install imagick
#docker开启支持扩展
docker-php-ext-enable imagick
安装成功分别进行验证
convert -version # imagemagick
php -m | grep imagick # imagick
gs -v # ghostscript
二、Windows安装
个人操作步骤补充:
1、先下载php_imagick扩展,拷贝待php安装目录的ext目录下,修改php.ini文件,extension=imagick
2、下载ImageMagick解压到D盘soft,然后在环境变量中PATH添加D:\soft\ImageMagick\bin
3、重启apache,此时才可以看到php_imagick扩展安装成功
4、下载Ghostscript并安装,我这边的安装目录D:\soft\gs9.53.3,在bin目录下复制gswin64.exe重命名为gs.exe,最后在环境变量中PATH添加D:\soft\gs9.53.3\bin
根据环境下载合适的 imagick扩展 和 imagemagick程序
1、下载拓展
imagick下载地址
要点: 注意对应php版本 ts还是nts x86还是x64,以phpinfo()为准
2、 安装拓展
解压上述文件后,将php_imagick.dll复制到php/ext目录,或者其他你的存放拓展的目录
修改php.ini 加上extension=php_imagick.dll,注意php可能有多个ini,以phpinfo为准
此时复制解压上述文件目录中其他dll到php目录,重启apache,此时phpinfo显示拓展安装成功,但是 ImageMagick number of supported formats为0,到这里成功安装了一半
3、下载imagemagick程序
imagemagick下载地址
imagemagick还有官网下载,此处不鼓励从imagemagick官方下载,他们的网站上我并没有找到历史版本下载,安装失败的几率很大
下载与phpinfo提示一致的版本,此时需要注意 1.软件版本对应 2.vc11还是vc14 3.x86还是x64 都要以phpinfo为准。
4、安装Ghostscript
三、代码实现
/**
* 计算PDF页数
* @param $pdfFileUrl
* @return int
*/
function count_pages($pdfFileUrl) {
$pdfText = file_get_contents($pdfFileUrl);
$num = preg_match_all("/\/Page\W/", $pdfText, $dummy);
return $num;
}
/**
* 将pdf文件转化为多张图片
* @param string $path 本地路径
* @param string $name 文件名称
* @return array|bool
* @throws ImagickException
*/
function pdfToImgMore($path = '',$name = '')
{
$return = [];
if (!file_exists($path)) {
return false;
}
//获取pdf文件的页数
$num = count_pages($path);
if($num == 0){
return false;
}
$im = new Imagick();
$im->setResolution(150, 150); //设置分辨率 值越大分辨率越高
$im->setCompressionQuality(100);
//防止pdf生成图片页数不对
$savePath = "pdf/" . date('Ymd') . "/";
$filePath = __DIR__.$savePath;
mkFolder($filePath);
for($i=0;$i<$num;$i++){
$im->setColorspace(Imagick::COLORSPACE_SRGB); //保证pdf彩色
$im->readImage($path."[".$i."]"); //设置读取pdf的第一页
$im->setImageFormat('png');
$im->setImageBackgroundColor('white');
$im->setImageCompose(Imagick::COMPOSITE_ATOP);
$name = $i . '_' . $name . '.png';
$fileName = $filePath.$name;
//处理黑色背景问题
$blankPage = new Imagick();
$blankPage->newPseudoImage($im->getImageWidth(), $im->getImageHeight(), "canvas:white");
$blankPage->compositeImage($im, Imagick::COMPOSITE_ATOP, 0, 0);
if ($blankPage->writeImage($fileName) == true) {
$return[] = $savePath . $name;
}
}
$im->clear();
$im->destroy();
return $return;
}