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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 03:05:13
Script start
Timing: Jan 18 2025 03:05:13
Module start 'layout'
Timing: Jan 18 2025 03:05:13
Module start 'content'
Timing: Jan 18 2025 03:05:13
Module end 'content'
Timing: Jan 18 2025 03:05:13
Script end

Main resources:

Total runtime0.6773 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 591.8906152.6406
Module start 'layout' 0.00590.0032 744.531339.4766
Module start 'content' 0.00910.6669 784.0078559.3047
Module end 'content' 0.67600.0013 1,343.312516.1250
Script end 0.6773  1,359.4375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.4583160.0002
Check MTime0.00130.1979160.0001
Mysql Total
Database connection0.00060.088110.0006
Mysqli_queries0.628692.8031600.0105
Looping result0.00050.0758580.0000
Template Total0.645095.220.3225
Template load0.00210.316020.0011
Template processing0.642894.911420.3214
Template load and register function0.00010.018310.0001
states
state_id_array0.00080.124210.0008
state_identifier_array0.00150.218120.0007
Override
Cache load0.00190.2773290.0001
Sytem overhead
Fetch class attribute can translate value0.00050.070620.0002
Fetch class attribute name0.00120.178550.0002
XML
Image XML parsing0.00060.093820.0003
class_abstraction
Instantiating content class attribute0.00000.001650.0000
General
dbfile0.00120.1748170.0001
String conversion0.00000.001340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
8content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 20
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs