我需要 景某个pdf文档中的某一页裁切其中一部分, 生成一新的png格式的文档.
gd不支持pdf格式, imagick 支持打开pdf, 并将其中一页转成png, 但是如何将此页的一部分裁切出来呢
参考代码来自网络
代码如下
function pdf2png($pdf,$path,$page=-1)
{
if(!extension_loaded('imagick'))
{
return false;
}
if(!file_exists($pdf))
{
return false;
}
$im = new imagick();
$im->setresolution(600,600);
$im->setcompressionquality(100);
if($page==-1)
$im->readimage($pdf);
else
$im->readimage($pdf.[.$page.]);
foreach ($im as $key => $var)
{
$height=$var->getimageheight();//取得原图的高度
$width=$var->getimagewidth();//取得原图的宽度
//echo h.$height.w:. $width;
$im_cut = new imagick(); //这里设置新图的实例
$im_cut->setresolution(600,600); //分辨率
$im_cut->setcompressionquality(100);//压缩率
// $im_cut->setimageformat('png'); //设置新图的格式 这里出错
// $im_cut->setimagetype (0);
// $im_cut-> 接下来不知道如何做了
//如何设置新图的高度长度
//如何把 $var中的一部分复制到新图,
//如何保存新图
$var->setimageformat('png');
$var->setimagetype (0);
$filename = $path./. md5($key.time()).'.png';
if($var->writeimage($filename) == true)
{
$return[] = $filename;
}
}
return $return;
}
分享到: 更多
------解决方案--------------------
$myurl = '/pdf/mypdf.pdf';
$image = new imagick(realpath($myurl).'[0]'); //[0] indicate the number of the wanted page
$image->setresolution( 300, 300 );
$image->setimageformat( png );
$image->writeimage(realpath('./mypdf.png'));
