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.
|