* @license http://www.gnu.org/licenses/gpl.html GNU General Public License * * Requires ImageMagick */ $RecipeInfo['PDFThumb']['Version'] = '2020-04-23'; // Linking SDV($LinkFunctions['PDF:'],'PDFThumb'); SDV($IMap['PDF:'],'$1'); function PDFThumb($pagename,$imap,$path,$title,$txt,$fmt=NULL) { global $EnableUpload, $UploadFileFmt, $UploadUrlFmt, $UploadDir, $UploadPrefixFmt; $ext = strtolower(substr ($path, -4)); $path = substr_replace($path, $ext, -4); preg_match('/\\.([^.]+)$/',$path,$match); $ext=@$match[1]; if (!(($ext=='pdf')OR($ext=='PDF'))) return "$path: ".XL('IMGbadtype'); $pathThumb = str_replace(array('.pdf','.PDF'), array('.jpg','.jpg'),$path); $upname = MakeUploadName($pagename,$path); //eliminate non alphanum chars $upnameThumb = MakeUploadName($pagename,$pathThumb); $flpth = FmtPageName("$UploadFileFmt/$upname",$pagename); $flpthThumb = FmtPageName("$UploadFileFmt/$upnameThumb",$pagename); $pageurl = FmtPageName('$PageUrl', $pagename); $uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt", $pagename); $uploadurl = FmtPageName( IsEnabled($EnableDirectDownload, 1) ? "$UploadUrlFmt$UploadPrefixFmt/" : "\$PageUrl?action=download&upname=", $pagename ); if (!file_exists($flpthThumb)) { if (!file_exists($flpth)) { $aopt = "href='$pageurl?upname=$upname&action=upload'"; $out = "Upload $upname"; return $out; } $out = CreatePDFThumbnail($flpth, $flpthThumb, 100); if ($out != "") { return $out; } } $url = PUE("$uploadurl$path"); $PDFThumbCaption = 1; $thumb = Url_Attach ($pagename, $upnameThumb); $out .= "
\n"; return $out; } function CreatePDFThumbnail($PDF, $Thumb, $size) { if (mime_content_type($PDF) != 'application/pdf') return "Invalid file type"; $img = new Imagick(); $img->setResolution(300, 300); //set the resolution of the resulting jp $img->readImage($PDF."[0]"); $height = $img->getImageHeight(); $width = $img->getImageWidth(); if ($height == 0) $height = 1; if ($width == 0) $width = 1; $sizeR = round($size*(min($width,$height)/max($width,$height))); if ($height == $width) $img->thumbnailimage($size,$size); else if ($height < $width) $img->thumbnailimage($size,$sizeR); else $img->thumbnailimage($sizeR,$size); $img->setImageColorspace(255); $img->setImageBackgroundColor('white'); $img = $img->flattenImages(); $img->setimageformat('jpeg'); $img-> writeimage($Thumb); $img-> clear(); $img-> destroy(); if (file_exists($Thumb)) return ""; return "Create PDF Failed"; }