¿Cómo están? Necesito su ayuda para resolver un problema que tengo con una clase que necesito convertir en una función, lo que hace esta clase es convertir archivos PDF, a imágenes JPG. Esta clase está usando una "función nativa de PHP" llamada Imagick, ya estuve desglosando esta clase, pero ya me he quedado estancado, no sé qué más hacer. Si serían tan amables de ayudarme, por favor.
require_once __DIR__.'ConvertPdfToImage.php';
$pdf = './_data/Primer_pdf_.pdf';
$out = './_result/local.jpg';
$cpi = new ConvertPdfToImage($pdf);
$cpi->save($out,['page'=>'all']);
Clase:
class ConvertPdfToImage{
protected $filename = ''; // original (from user) name of pdf file
protected $realFileName = ''; // real name of pdf file (must be not equal to original)
protected $imagick;
protected $count; //count of pages (reloaded on after call Load)
protected $params = [
'resolution' =>144, // resolution
'page' =>'all', // page number or 'all'
'format' =>'jpg', // jpg | png
'addIndexPageToName'=>false,// only for page === number
'needHeader'=>false, // out header("Content-Type: image/...) before out to browwser (for out only)
'outReturn' =>false, // out return image as result or not to browser (for out only)
'compression'=>100,
];
function __construct($filename = '',$params=[]){
$this->params = array_merge($this->params,$params);
if ($filename!=='')
$this->load($filename);
return $this;
}
/**
* загрузка файла, можно использовать удаленный путь
*/
public function load($filename){
$this->realFileName = $filename;
$this->realFileName = realpath($this->realFileName);
$this->filename = $filename;
$this->imagick = new \Imagick();
$this->imagick->readImage($this->realFileName);
$this->count = $this->imagick->getNumberImages();
return $this;
}
private function _read(&$params){
$params = array_merge($this->params,$params);
$this->imagick = new \Imagick();
$this->imagick->setResolution(144,144);
$this->imagick->setCompressionQuality(100);
$page = $params['page'] !== 'all' ?'['.$params['page'].']' : '' ;
$this->imagick->readImage($this->realFileName.$page);
$this->imagick->setImageFormat($params['format']);
}
/**
* сохранение файла на диск
*/
public function save($toFileName,$params=[]){
try{
$this->_read($params);
if ($params['page'] === 'all'){
for($i = 0 ;$i<$this->count;$i++)
$this->save($toFileName,['addIndexPageToName'=>true,'page'=>$i]);
}else{
if ($params['addIndexPageToName'])
$toFileName = $this->addIndex($toFileName,$params['page']);
$this->imagick->writeImage($toFileName);
}
return $this;
}catch(\Exception $e){
error_log(__METHOD__.' Exception: '.$e->getMessage());
}
}
/**
* вывод файла
*/
private function addIndex($filename,$index){
$info = pathinfo($filename);
$file = $info['basename'];
$pos = strrpos($file,'.');
if ($pos!==false)
$file=substr($file,0,$pos).'_'.$index.'.'.$info['extension'];
else
$file.='_'.$index;
return $info['dirname'].($info['dirname']!==''?'/':'').$file;
}
};
require_once __DIR__.'ConvertPdfToImage.php'; function pdf_a_imagenes($src, $trgt, $params[]){ $tool = new ConvertPdfToImage($src, $params); return $tool->save($trgt,['page'=>'all']); }
pdf_a_imagenes($pdf, $out);