Home >Backend Development >PHP Tutorial >用php自带的ImageMagick来实现PDF文件转换成图片

用php自带的ImageMagick来实现PDF文件转换成图片

WBOY
WBOYOriginal
2016-06-20 12:26:391158browse

不管你是windows还是linux,要实现PDF文件转换成png还是jpg首先,都安装Imagick扩展。这扩展网上都有详细的安装教程,灵活的运用安装。下面是实现功能的代码:

 $fileName1 = 'CZ2016-01-01/o_1ak57jr6ms741efo1ndk8b4daic.jpg'; $path1 = "http://img.qkhl.net/test"; chmod($path1.'/'.$fileName1, '-rwxrwxrwx' );//赋给文件权限(777) $data_i =  pdf2png($fileName1, $path1); die('{"status" s: "success", "result": {"imgpath" : "' . $data_i . '"}}');//返回参数function pdf2png($pdf,$path1,$page=-1){   if(!extension_loaded('imagick'))   {       return 4;   }   if(!file_exists($pdf))   {       return 5;   }   $im = new Imagick();   $im->setResolution(60,60); //设置分辨率   $im->setCompressionQuality(10);//设置图片压缩的质量   if($page==-1)  {    $ss =   $im->readImage($pdf);   }   else{      $im->readImage($pdf."[".$page."]");//从文件名读取图像       return 7;   }       $im->setImageFormat('jpg'); //为图片设置指定的格式       $filename1 = $path1."/". md5(time()).'.jpg';       $dd = $im->writeImage($filename1);       // return '阻住';       if($dd == true)//把图片写入指定的文件       {          // return 'yes';           $return = $filename1;       } else{            return '失败';       }   return $return;}
注意当我们引入函数的时候 pdf2png($pdf,$path1,$page=-1)当直接引用。还有就是路径必须写正确,否则会转换失败。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn