PHP使用imagick读取PDF生成png缩略图的两种方法_php实例
ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。
二、php_imagick什么
一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。
三、PDF生成png缩略图的两种方法
第一种:
* PDF2PNG
* @param $pdf 待处理的PDF文件
* @param $path 待保存的图片路径
* @param $page 待导出的页面 -1为全部 0为第一页 1为第二页
* @return 保存好的图片路径和文件名
*/
function pdf2png($pdf,$path,$page=0)
{
if(!is_dir($path))
{
mkdir($path,true);
}
if(!extension_loaded('imagick'))
{
echo '没有找到imagick!' ;
return false;
}
if(!file_exists($pdf))
{
echo '没有找到pdf' ;
return false;
}
$im = new Imagick();
$im->setResolution(120,120); //设置图像分辨率
$im->setCompressionQuality(80); //压缩比
$im->readImage($pdf."[".$page."]"); //设置读取pdf的第一页
//$im->thumbnailImage(200, 100, true); // 改变图像的大小
$im->scaleImage(200,100,true); //缩放大小图像
$filename = $path."/". time().'.png';
if($im->writeImage($filename) == true)
{
$Return = $filename;
}
return $Return;
}
$s = pdf2png('file/1371273225-ceshi_ppt.pdf','images');
echo '

第二种:
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 $Key => $Var){
$Var->setImageFormat('png');
$Filename = $Path.'/'.md5($Key.time()).'.png';
if($Var->writeImage($Filename) == true){
$Return[] = $Filename;
}
}
return $Return;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
