Skip to content

Instantly share code, notes, and snippets.

@hh-com
Last active November 17, 2017 11:14
Show Gist options
  • Save hh-com/3c60c176a2d67e975b18b5598d07e89f to your computer and use it in GitHub Desktop.
Save hh-com/3c60c176a2d67e975b18b5598d07e89f to your computer and use it in GitHub Desktop.
Create Images from each page in a PDF-File with Imagick
<?php
function pdf2Png($pdfPath, $imgPath = ".")
{
$path_parts = pathinfo($pdfPath);
$PdfFilename = "pdf".$path_parts['filename'];
$pdf = new \Imagick($pdfPath);
$anzahlDerSeiten = $pdf->getNumberImages();
for ($i = 0; $i < $anzahlDerSeiten; $i++) {
$filename = $PdfFilename.$i.".jpg";
$image = new \Imagick();
/*Incread Resolution before reading PDF File */
$image->setResolution(600,600);
$image->readImage($pdfPath."[".$i."]" );
#$image->setBackgroundColor('white');
#$image->setImageAlphaChannel(imagick::ALPHACHANNEL_DEACTIVATE );
$image->setImageType (imagick::IMGTYPE_TRUECOLOR);
$image->setImageUnits(1);
$image->setImageResolution(300,300);
// cut around in px
$image->shaveImage(25, 25);
if ($image->getImageWidth() > $image->getImageHeight() ) {
/* Landscape */
$image->resizeImage(3508,2480,Imagick::FILTER_LANCZOS,1);
} else {
/* Portrait */
$image->resizeImage(2480,3508,Imagick::FILTER_LANCZOS,1);
}
$image->setImageFormat( "jpg" );
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(100);
// Write image to path
$image->writeImage($imgPath."/".$filename);
$image->clear();
$image->destroy();
#echo "<img src='".$imgPath."/".$filename."' />";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment