pepebe
5/18/2014 - 9:42 PM

pdf2jpg - MODX snippet to create a thumbnail jpg from a pdf file

pdf2jpg - MODX snippet to create a thumbnail jpg from a pdf file

<?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->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'];