Last active
November 17, 2017 11:14
-
-
Save hh-com/3c60c176a2d67e975b18b5598d07e89f to your computer and use it in GitHub Desktop.
Create Images from each page in a PDF-File with Imagick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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