Last active
February 8, 2024 16:38
-
-
Save pepebe/075c24a949a46582c4a0 to your computer and use it in GitHub Desktop.
pdf2jpg - MODX snippet to create a thumbnail jpg from a pdf file
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 | |
/* | |
pdf2jpg v.0.0.1 | |
--------------------------------------- | |
MODX snippet to create a jpg thumbnail from a pdf file | |
AUTHOR: | |
--------------------------------------- | |
info@pepebe.de | |
CHANGELOG: | |
--------------------------------------- | |
v0.0.1 - 2014-05-19 - Initial public release | |
NOTE: | |
--------------------------------------- | |
phpthumbof and its descendents can do this too, | |
but for various reasons they sometimes just don't work. | |
pdf2jpg has a couple of advantages: | |
- It works with Imagick | |
- You can choose the page you want to output | |
- It caches the jpg in a custom directory | |
REQUIREMENTS: | |
---------------------------------------- | |
Imagick php library (available at any decent hoster) | |
Tested on: skytoaster and modxcloud. | |
USAGE: | |
---------------------------------------- | |
Use it as an output filter | |
[[+pdf:pdf2jpg=`w=300`]] | |
[[+pdf:pdf2jpg=`w=300&h=300`]] | |
[[+pdf:pdf2jpg=`w=300&h=300&p=0`]] | |
OPTIONS: | |
---------------------------------------- | |
w = (int) width - defaults to 250 | |
h = (int) height - defaults to false (proportional) | |
p = (int) page - defaults to 0 (first page) | |
d = (bolean) debug - defaults to 0 (false) | |
*/ | |
if(!extension_loaded('imagick')) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[pdf2jpg]: Error: Imagick not available'); | |
return ""; | |
} | |
$msg = array(); | |
$pkg_name = "ppb_pdf2jpg/"; | |
$assets_path = "assets/components/"; | |
$core_path = "core/components/"; | |
$cache_path = $assets_path.$pkg_name."cache/"; | |
$base_path = $modx->getOption('base_path'); | |
$pdf = $base_path.trim($input); | |
$param = array( | |
'w' => 300 | |
,'h' => false | |
,'p' => 0 | |
,'d' => 1 | |
); | |
/* */ | |
if(!function_exists('debug')) | |
{ | |
function debug($arr){ | |
return "<pre>".print_r($arr,true)."</pre>"; | |
} | |
} | |
/* Check/create cache directory*/ | |
if (!file_exists($base_path.$cache_path)) { | |
mkdir($base_path.$cache_path, 0755, true); | |
} | |
/* Setup parameter array from options string */ | |
if(!empty($options)){ | |
$options = explode('&',$options); | |
foreach($options as $option) | |
{ | |
$vars = explode('=', $option); | |
$name = $vars[0]; | |
$value = $vars[1]; | |
switch($name) | |
{ | |
case 'w': | |
settype($value, 'integer'); | |
$param['w'] = $value; | |
break; | |
case 'h': | |
if(!empty($value)) | |
{settype($value, 'integer');} | |
else | |
{$value = false;} | |
$param['h'] = $value; | |
break; | |
case 'p': | |
settype($value, 'integer'); | |
$param['p'] = $value; | |
break; | |
case 'd': | |
settype($value, 'integer'); | |
$param['d'] = $value; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
if(!function_exists('pdf2jpg')){ | |
function pdf2jpg($pdf,$param,$base_path,$cache_path){ | |
global $modx; | |
$path = $base_path.$cache_path; | |
/* check if pdf exists*/ | |
if(!file_exists($pdf)) | |
{ | |
$output['msg'] = "PDF '" . $pdf . "' does not exists"; | |
$output['img'] = ""; | |
} | |
else | |
{ | |
/* check if there is already a thumbnail for it */ | |
$filename = basename($pdf,'.pdf'); | |
$thumbnail = $path.$filename.".jpg"; | |
if(!file_exists($thumbnail) AND file_exists($pdf) ){ | |
/* Read the image */ | |
$img = new Imagick(); | |
$img->setResolution(72,72); | |
/* ".$param['p']." */ | |
$img->readImage("{$pdf}[".$param['p']."]"); | |
/* Thumbnail the image */ | |
$img->thumbnailImage($param['w'], $param['h']); | |
$img->setImageBackgroundColor('#ffffff'); | |
$img->setImageAlphaChannel(12); // Imagick::ALPHACHANNEL_REMOVE | |
$img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); | |
$img->writeImage($path.$filename.".jpg"); | |
if(!file_exists($path.$filename.".jpg")){ | |
$output['msg'] = 'PDF thumbnail could not be created'; | |
$output['img'] = ''; | |
} | |
else { | |
$output['msg'] = 'PDF thumbnail created'; | |
$output['img'] = $cache_path.$filename.".jpg"; | |
} | |
} | |
else{ | |
$output['msg'] = 'PDf thumbnail already exists at' . $thumbnail; | |
$output['img'] = $cache_path.$filename.".jpg"; | |
} | |
} | |
return $output; | |
} | |
} | |
$output = pdf2jpg($pdf,$param,$base_path,$cache_path); | |
if($param['d'] == 1){ | |
$msg = print_r($output['msg'], true); | |
$modx->log(modX::LOG_LEVEL_ERROR, '[pdf2jpg]: ' .$msg); | |
} | |
return $output['img']; |
@JensWolff Danke fürs Feedback an Transparenzen hatte ich nicht gedacht.
Ich war auf der Suche nach einer Lösung um Grafiken aus PDFs zu erstellen und finde mein eigenes Zeug. Ist gut gealtert. Funktioniert noch immer ;)
@pepebe 5 years later, we found my initial fix not working anymore.. (funny to dig out my own comment, not knowing it was there).. And we have another fix:
$im->setImageAlphaChannel(12); // Imagick::ALPHACHANNEL_REMOVE
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
instead of the deprecated:
$img = $img->flattenImages();
After that, it works hopefully again for many years :P
Its 2024, and I again Im stumbling upon this one.
@JensWolff Thanks four your update!
@pepebe Always here to help, bro! ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome code, pepebe! Works like a charm for me! 👍
Adding these two lines of code at line 155 prevents ugly black backgrounds if there is transparency in the PDF:
$img->setImageBackgroundColor('#ffffff');
$img = $img->flattenImages();
Perhaps you can add that! :)
Greetings and hope to see you soon again on a MODX-Event,
Jens