function pdf2png($PDF,$Path,$ImageName){ if(!extension_loaded('imagick')){ echo "<p>no imagick'</p>"; return false; } if(!file_exists($PDF)){ echo "<p>no file'</p>"; return false; } $IM = new imagick(); $IM->setOption('density','200'); $IM->setOption('antialias',''); $IM->setOption('sharpen','0x1.0'); //$IM->setResolution(794,1123); // echo 'image resolution'; $IM->setCompressionQuality(100); $IM->readImage($PDF); foreach ($IM as $Key => $Var){ $Var->paintTransparentImage($Var->getImageBackgroundColor(), 1, 10000); $Var->setImageFormat('png'); $Filename = $Path.'/'.$ImageName.'.png'; if($Var->writeImage($Filename) == true){ $Return[] = $Filename; } } return $Return; }
/** * Convert to output format. This method convert from pdf to specified format with optimizing * @throw Exception * @access public * @param string $outputPath - path to file. May content only path or path with filename * @param int/string[=ALL] $page - number of document page wich will be converted into image. If specified 'ALL' - will be converted all pages. * @param string $format - output format (see self::getFormats()) * @param array $resolution - array with x&y resolution DPI * @param int $depth - bit depth image * @return string/bool[false] - return image path of last converted page */ public function convert($outputPath = '', $page = 'ALL', $format = 'png', $resolution = array('x' => 300, 'y' => 300), $depth = 8) { if (!Imagick::queryFormats(strtoupper($format))) { throw new Exception('Unsupported format'); } $startTime = microtime(true); $im = new imagick(); $im->setResolution($resolution['x'], $resolution['y']); $format = strtolower($format); $im->setFormat($format); if ($outputPath) { if (is_dir($outputPath)) { $outputPath = $outputPath . pathinfo($this->filePathWithoutType, PATHINFO_FILENAME); } $outputFileName = $outputPath; } else { $outputFileName = $this->filePathWithoutType; } if ($page === 'ALL') { $im->readImage($this->filePathWithoutType . '.pdf'); $im->setImageFormat($format); $im->setImageAlphaChannel(11); // it's a new constant imagick::ALPHACHANNEL_REMOVE $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); $im->setOption($format . ':bit-depth', $depth); $im->writeImages($outputFileName . "." . $format, false); $logString = '[POINT] File "' . $this->filePathWithoutType . '.pdf" converted to "' . $format . '" with ' . $im->getNumberImages() . ' pages (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); //Optimizing if ($format == 'png' && $this->optipngChecking()) { $startTime = microtime(true); for ($page = $i = 0; $i < $im->getNumberImages(); $i++) { $this->execute('optipng -o=5', $outputFileName . "-" . (int) $i . "." . $format); } $logString = '[POINT] Files "' . $outputFileName . '-x.' . $format . '" optimized (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); } } else { $im->readImage($this->filePathWithoutType . '.pdf[' . (int) $page . ']'); $im->setImageFormat($format); $im->setImageAlphaChannel(11); // it's a new constant imagick::ALPHACHANNEL_REMOVE $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); $im->setOption($format . ':color-type', 2); $im->setOption($format . ':bit-depth', $depth); $im->writeImage($outputFileName . "-" . (int) $page . "." . $format); $logString = '[POINT] File "' . $outputFileName . '.pdf" converted to "' . $format . '" one page (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); //Optimizing if ($format == 'png' && $this->optipngChecking()) { $startTime = microtime(true); $this->execute('optipng -o=5', $outputFileName . "-" . (int) $page . "." . $format); $logString = '[POINT] File "' . $outputFileName . "-" . (int) $page . "." . $format . '" optimized (ex: ' . (microtime(true) - $startTime) . 's)'; $this->setLog($logString); } } if (file_exists($outputFileName . "-" . (int) $page . "." . $format)) { return $outputFileName . "-" . (int) $page . "." . $format; } else { return false; } }