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

eZ debug

Timing: Jan 17 2025 23:44:00
Script start
Timing: Jan 17 2025 23:44:00
Module start 'content'
Timing: Jan 17 2025 23:44:00
Module end 'content'
Timing: Jan 17 2025 23:44:00
Script end

Main resources:

Total runtime0.1677 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0058 587.8359180.8203
Module start 'content' 0.00580.0057 768.656398.0234
Module end 'content' 0.01150.1561 866.6797526.6250
Script end 0.1676  1,393.3047 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00372.1806200.0002
Check MTime0.00150.8725200.0001
Mysql Total
Database connection0.00080.499810.0008
Mysqli_queries0.121872.64561410.0009
Looping result0.00160.92471390.0000
Template Total0.155892.910.1558
Template load0.00100.580210.0010
Template processing0.154892.292410.1548
Override
Cache load0.00070.414410.0007
Sytem overhead
Fetch class attribute can translate value0.00070.417410.0007
XML
Image XML parsing0.00030.184810.0003
General
dbfile0.00160.9451200.0001
String conversion0.00000.003630.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs