Forums / General / Generate pdf thumbnail / Automatic upload action

Generate pdf thumbnail / Automatic upload action

Author Message

Michaël Todorovic

Wednesday 06 May 2009 3:20:39 am

Hello,
I would like to add a functionnality to my ez 4.1.0. Currently, when I upload a pdf file, it is placed automatically under Media/Files/Pdf. I would like to create a snapshot (jpeg file) of the first page of the pdf and place it under Media/Files/Pdf/snapshots. I know how to create the jpeg snapshot with ghostscript. I saw in upload.ini that I can create a handler based on a mime-type. That handler has to do everything, right ? I mean, place file into media/files/pdf, etc. I just need to know how to add a file into media library. I don't want to re-invent the wheel but I can't find the code. Where is located code to add a file into Media library ?

Thank you

Steven E. Bailey

Wednesday 06 May 2009 8:35:20 am

This doesn't answer your question, but you do know you can use Imagemagick to generate the cover shot of a pdf too, right? If you are already using Imagemagick it might be easier to do it with that instead of ghostscript.

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

Michaël Todorovic

Tuesday 30 June 2009 12:15:35 am

Well, after some time, I got enough time to do what I wanted to do.

<b>This is how it works :</b> I upload a pdf file with ez interface with automatic location, the pdf is uploaded in Media/Files/Pdf and a thumbnail of the first page is automatically generated and uploaded in Media/Files/Pdf/Thumbnails.

<b>What are the prerequisites ?</b> Imagemagick. If your ez installation works with imagemagick, this little piece of code will work. If it works with gd, it won't work, sorry.

<b>Now how to do it :</b>
We need to create a file in extension/ezwebin/uploadhandlers/ezpdfuploadhandler.php

<?

include_once( 'kernel/classes/ezcontentuploadhandler.php' );

class eZpdfUploadHandler extends eZContentUploadHandler
{
    function eZpdfUploadHandler()
    {
		$this->eZContentUploadHandler( 'PDF Thumbnail Generator', 'pdfUploadHandler' );
    }

    /*!
    Handles the uploading of pdf files.
    */
    function handleFile( &$upload, &$result, $filePath, $originalFilename, $mimeInfo, $location, $existingNode )
    {
		//name of the temporary image
		$tmpFilename = "/tmp/" . $originalFilename . ".jpg";
	
		//construct command to convert pdf's first page to jpg
		//convert file.pdf[0] thumb.jpg
		//[0] means the first page (we count from 0, not 1 as arrays)
		$cmd = "convert \"" . $filePath . "[0]\" " . $tmpFilename;
		system($cmd);

		//upload the image in node 292 with the basename of the pdf file
		$upload->handleLocalFile($result, $tmpFilename, 292, 0, basename($originalFilename, ".pdf"));
		//destroy temporary file
		unlink($tmpFilename);
		
		//upload pdf file in $location (should be 'auto') and return results
		return $upload->handleUpload($result, 'UploadFile', $location, 0, '');
    }
}

?>

The node 292 is Media/Files/Pdf/Thumbnails (you have to create it and read the node id). Then we have to make some configuration. I made settings in global overrides but it also works in siteaccess overrides. Add this in settings/override/content.ini.append.php

[RelationAssignmentSettings]
ClassSpecificAssignment[]=pdf;media/files/pdf

Here we say that the pdf class must be placed in Media/Files/pdf

and this in settings/override/upload.ini.append.php

<?php /* #?ini charset="utf-8"?

[CreateSettings]
MimeClassMap[application/pdf]=pdf

MimeUploadHandlerMap[application/pdf]=ezpdfuploadhandler

[pdf_ClassSettings]
FileAttribute=pdf
NameAttribute=name
NamePattern=<original_filename_base>

*/ ?>

As you see, you will need to create a pdf class. Its identifier must be "pdf" (used in MimeClassMap, pdf_ClassSettings and FileAttribute). To create this class, just copy the file class and change its name and identifier.

<b>Clear settings caches</b>

That's all ! Now you can upload pdf files and get thumbnails automatically in a specified node.

Michaël Todorovic

Tuesday 15 December 2009 2:30:04 am

Me again !

I experience problems with this handler in webdav mode as UploadFile input does not exist.Using handleLocalFile instead of handleUpload makes a loop (after analyzing handleLocalFile code, this is normal) and web server comes crazy (forced to kill it). Is there a way to make an upload handler compatible with webdav AND http upload ?

Thank you

Edit: now I'm working with ez 4.2.0