本文最后更新于2021-06-11,已超过 1年没有更新,如果文章内容、图片或者下载资源失效,请留言反馈,我会及时处理,谢谢!
温馨提示:本文共1737个字,读完预计5分钟。
/**
* 将pdf文件转化为多张png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @return array|bool
*/
function pdf2png($pdf, $path)
{
if (!extension_loaded('imagick')) {
return false;
}
if (!file_exists($pdf)) {
return false;
}
$im = new Imagick();
$im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
$im->setCompressionQuality(100);
$im->readImage($pdf);
foreach ($im as $k => $v) {
$v->setImageFormat('png');
$fileName = $path . md5($k . time()) . '.png';
if ($v->writeImage($fileName) == true) {
$return[] = $fileName;
}
}
return $return;
}
/**
* 将pdf转化为单一png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @throws Exception
*/
function pdf2png2($pdf, $path)
{
try {
$im = new Imagick();
$im->setCompressionQuality(100);
$im->setResolution(120, 120);//设置分辨率 值越大分辨率越高
$im->readImage($pdf);
$canvas = new Imagick();
$imgNum = $im->getNumberImages();
//$canvas->setResolution(120, 120);
foreach ($im as $k => $sub) {
$sub->setImageFormat('png');
//$sub->setResolution(120, 120);
$sub->stripImage();
$sub->trimImage(0);
$width = $sub->getImageWidth() + 10;
$height = $sub->getImageHeight() + 10;
if ($k + 1 == $imgNum) {
$height += 10;
} //最后添加10的height
$canvas->newImage($width, $height, new ImagickPixel('white'));
$canvas->compositeImage($sub, Imagick::COMPOSITE_DEFAULT, 5, 5);
}
$canvas->resetIterator();
$canvas->appendImages(true)->writeImage($path . microtime(true) . '.png');
} catch (Exception $e) {
throw $e;
}
}
* 将pdf文件转化为多张png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @return array|bool
*/
function pdf2png($pdf, $path)
{
if (!extension_loaded('imagick')) {
return false;
}
if (!file_exists($pdf)) {
return false;
}
$im = new Imagick();
$im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
$im->setCompressionQuality(100);
$im->readImage($pdf);
foreach ($im as $k => $v) {
$v->setImageFormat('png');
$fileName = $path . md5($k . time()) . '.png';
if ($v->writeImage($fileName) == true) {
$return[] = $fileName;
}
}
return $return;
}
/**
* 将pdf转化为单一png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @throws Exception
*/
function pdf2png2($pdf, $path)
{
try {
$im = new Imagick();
$im->setCompressionQuality(100);
$im->setResolution(120, 120);//设置分辨率 值越大分辨率越高
$im->readImage($pdf);
$canvas = new Imagick();
$imgNum = $im->getNumberImages();
//$canvas->setResolution(120, 120);
foreach ($im as $k => $sub) {
$sub->setImageFormat('png');
//$sub->setResolution(120, 120);
$sub->stripImage();
$sub->trimImage(0);
$width = $sub->getImageWidth() + 10;
$height = $sub->getImageHeight() + 10;
if ($k + 1 == $imgNum) {
$height += 10;
} //最后添加10的height
$canvas->newImage($width, $height, new ImagickPixel('white'));
$canvas->compositeImage($sub, Imagick::COMPOSITE_DEFAULT, 5, 5);
}
$canvas->resetIterator();
$canvas->appendImages(true)->writeImage($path . microtime(true) . '.png');
} catch (Exception $e) {
throw $e;
}
}
————————————————
版权声明:本文为CSDN博主「ainiaa」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jeff_love_marina/article/details/80838055