'PDFThumbnails', 'version' => '1.0', 'author' => '[http://landofbile.com bile]', 'url' => 'http://landofbile.com', 'description' => 'Generate thumbnails for PDFs just as SVGs'); $wgMediaHandlers['application/pdf'] = 'PdfHandler'; $wgPDFThumbType = array('jpg', 'image/jpg'); $wgPDFConverters = array('ImageMagick' => '$path/convert -background white -thumbnail $widthx$height\! $input[0] JPG:$output'); $wgPDFConverter = 'ImageMagick'; $wgPDFConverterPath = ''; $wgPDFMaxSize = 2048; class PdfHandler extends ImageHandler { function isEnabled() { global $wgPDFConverters, $wgPDFConverter; if ( !isset( $wgPDFConverters[$wgPDFConverter] ) ) { wfDebug( "\$wgPDFConverter is invalid, disabling PDF rendering.\n" ); return false; } else { return true; } } function mustRender( $file ) { return true; } function normaliseParams( $image, &$params ) { global $wgPDFMaxSize; if ( !parent::normaliseParams( $image, $params ) ) { return false; } # Don't make an image bigger than wgMaxPDFSize $params['physicalWidth'] = $params['width']; $params['physicalHeight'] = $params['height']; if ( $params['physicalWidth'] > $wgPDFMaxSize ) { $srcWidth = $image->getWidth( $params['page'] ); $srcHeight = $image->getHeight( $params['page'] ); $params['physicalWidth'] = $wgPDFMaxSize; $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgPDFMaxSize ); } return true; } function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { global $wgPDFConverters, $wgPDFConverter, $wgPDFConverterPath; if ( !$this->normaliseParams( $image, $params ) ) { return new TransformParameterError( $params ); } $clientWidth = $params['width']; $clientHeight = $params['height']; $physicalWidth = $params['physicalWidth']; $physicalHeight = $params['physicalHeight']; $srcPath = $image->getPath(); if ( $flags & self::TRANSFORM_LATER ) { return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); } if ( !wfMkdirParents( dirname( $dstPath ) ) ) { return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, wfMsg( 'thumbnail_dest_directory' ) ); } $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight ); if( $status === true ) { return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); } else { return $status; // MediaTransformError } } /* * Transform an PDF file to ... * This function can be called outside of thumbnail contexts * @param string $srcPath * @param string $dstPath * @param string $width * @param string $height * @returns TRUE/MediaTransformError */ public function rasterize( $srcPath, $dstPath, $width, $height ) { global $wgPDFConverters, $wgPDFConverter, $wgPDFConverterPath; $err = false; if ( isset( $wgPDFConverters[$wgPDFConverter] ) ) { $cmd = str_replace( array( '$path/', '$width', '$height', '$input', '$output' ), array( $wgPDFConverterPath ? wfEscapeShellArg( "$wgPDFConverterPath/" ) : "", intval( $width ), intval( $height ), wfEscapeShellArg( $srcPath ), wfEscapeShellArg( $dstPath ) ), $wgPDFConverters[$wgPDFConverter] ) . " 2>&1"; wfProfileIn( 'convert' ); wfDebug( __METHOD__.": $cmd\n" ); $err = wfShellExec( $cmd, $retval ); wfProfileOut( 'convert' ); } $removed = $this->removeBadFile( $dstPath, $retval ); if ( $retval != 0 || $removed ) { wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"', wfHostname(), $retval, trim($err), $cmd ) ); return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); } return true; } function getImageSize( $image, $path ) { return array(2550, 3300, 'PDF', "width=2550", "height=3300"); //US-Letter //at 300ppi } function getThumbType( $ext, $mime ) { global $wgPDFThumbType; return $wgPDFThumbType; } }