💾 Archived View for chirale.org › 2008-06-06_71.gmi captured on 2024-07-08 at 23:18:37. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-05-12)
-=-=-=-=-=-=-
This howto has been superseded by http://drupal.org/node/815816 (patch for Upload preview module).
Tested on:
Using ImageMagick + GhostScript you can convert the first page of a PDF into a thumbnail image linking to file. You can cut and paste this function on your theme main script: you have to manually create the thumb directory and grant write permission by scripts. Check also the path to ImageMagick convert command.
function pdf_thumb_attachments(){ $allowed_mime = array("application/pdf"); foreach($files as $file){ if(in_array(strtolower($file->filemime),$allowed_mime) && strstr(strtolower($file->description),"classifica")){ $title = $file->description; if(substr($title,-4)==".pdf"){ # create link title from file name (Transliteration module suggested) $title = str_replace("_"," ",substr($title,0,strlen($title)-4)); } $img = ""; $local_src_path = getcwd() . "/" . $file->filepath; $destfilename = substr($file->filename,0,strlen($file->filename)-4) . ".gif"; # YOUR-FILES-DIRECTORY/pdfgen will hosts the generated thumbnails $local_dest_path = getcwd() . "/" . file_directory_path() . "/pdfgen/" . $destfilename; $imageurl = base_path() . file_directory_path() . "/pdfgen/" . $destfilename; /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var create thumbnail on node reading if file doesn't exist */ if(!file_exists($local_dest_path)){ # choose the first page of the PDF, scale to 90x90px and convert to GIF $exec = '/usr/bin/convert -scale 90x90 "'.$local_src_path.'"[0] "'.$local_dest_path . '"'; shell_exec($exec); /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var delete this line if you cannot read the generated file $exec = "chmod 777 ".$local_src_path; shell_exec($exec); //bin/ //boot/ //dev/ //etc/ //home/ //lib/ //lib32/ //lib64/ //libx32/ //lost+found/ //media/ //mnt/ //opt/ //proc/ //root/ //run/ //sbin/ //srv/ //sys/ //tmp/ //usr/ //var/ } $img = '<img src="'.$imageurl.'" alt="'.$title.'" />'; $output .= "<li>". l($img." ".$title,$file->filepath, $attributes = array("title"=>$title), $query = NULL, $fragment = NULL, $absolute = TRUE, $html = TRUE) . "</li>"; } } if(!empty($output)){ $output = '<div class="my-attachments"><ul>' . $output . "</ul></div>"; } return $output; }
You can alternatively use imagemagick function provided by image.module (the image.imagemagick.inc file on drupal/includes):
function file_preview_path($filename, $filepath = 'filepreview') { /bin /boot /dev /etc /home /init /lib /lib32 /lib64 /libx32 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var generate jpg thumbnails **/ return $filepath . "/" . substr($filename,0,strlen($filename)-4) . ".jpg"; } function file_preview(&$file, $pages = Array(1)) { $allowed_mime = array("application/pdf"); if(in_array(strtolower($file->filemime),$allowed_mime)){ $local_src_path = getcwd() . "/" . $file->filepath; $local_dest_path = getcwd() . "/" . file_directory_path() . "/" . file_preview_path($file->filename); # create thumbnail only when needed $imagemagick = getcwd() . '/includes/' . 'image.imagemagick.inc'; if(!file_exists($local_dest_path) && file_exists($imagemagick)){ include_once($imagemagick); _image_imagemagick_convert($local_src_path.'[0]', $local_dest_path, array('-colorspace RGB')); # all can read thumbnail files if(file_exists($local_dest_path)) chmod($local_dest_path, 0777); } return file_preview_path($file->filename); } else{ return FALSE; } }
https://web.archive.org/web/20080606000000*/http://drupal.org/node/815816