120, 'm' => 240, 'l' => '320'); if (!array_key_exists($thumb, $size)) { unset($thumb); } $sql = "SELECT doctype, docsize, mimetype, hash, extension, filename, pages FROM document WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$docid]); $doc = $sth->fetch(PDO::FETCH_OBJ); // alter Dateiname // $ofn = sprintf("%s-%s.%s", $dtmap[$row['doctype']], $row['hash'], $row['extension']) // $file = sprintf("%s/%s-%s.%s", $g_doc_basepath, $g_doc_typemap[$row['doctype']], $row['hash'], $row['extension']); /*if (file_exists($file)) { // Hash-Dateinamen: Umbenennen in neues Format // document-format: ttt-nnnnnn.eee // thumbnail-format: ttt-nnnnnn-ppp-s.eee $fnew = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $g_doc_typemap[$row['doctype']], $docid, $row['extension']); rename($file, $fnew); $file = $fnew; } else { */ $file = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $g_doc_typemap[$doc->doctype], $docid, $doc->extension); // } if (isset($thumb)) { if ($page < 1) { $page = 1; } elseif ($page > $doc->pages) { $page = $doc->pages; } if ($doc->mimetype == 'application/pdf') { // PDF creates PNG-thumbnails $thumbname = sprintf('%s-%06d-%03d-%s.png', $g_doc_typemap[$doc->doctype], $docid, $page, $thumb); $thumbpath = $g_doc_basepath . '/thumb/' . $thumbname; if (!file_exists($thumbpath)) { exec("/usr/bin/mutool draw -o {$thumbpath} -w {$size[$thumb]} $file $page", $output, $retval); } } else { $thumbname = sprintf('%s-%06d-%03d-%s.%s', $g_doc_typemap[$doc->doctype], $docid, $page, $thumb, $doc->extension); $thumbpath = $g_doc_basepath . '/thumb/' . $thumbname; if (!file_exists($thumbpath)) { list($width, $height) = getimagesize($file); $factor = $size[$thumb] / max($width, $height); // scale down if ($doc->mimetype == 'image/jpeg') { $orig = imagecreatefromjpeg($file); $scaled = imagescale($orig, (int)($width * $factor), (int)($height * $factor), IMG_BICUBIC); imagejpeg($scaled, $thumbpath, 85); $file = $thumbpath; $filename = $thumbname; $fsize = 0; // let's calc later } elseif ($doc->mimetype == 'image/png') { $orig = imagecreatefrompng($file); } elseif ($doc_>mimetype == 'image/xml+svg') { // TODO anything here? } } } $file = $thumbpath; $filename = $thumbname; $fsize = 0; // let's calc later } else { // normal $filename = $doc->filename; $fsize = $doc->docsize; } if (file_exists($file)) { // send file to caller header('Content-Description: File Transfer'); header('Content-Type: ' . $doc->mimetype); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); if ($fsize == 0) { header('Content-Length: ' . filesize($file)); } else { header('Content-Length: ' . $fsize); } readfile($file); exit; } else { // dynamically create error image header("Content-Type: image/png"); $im = @imagecreate(240, 120) or die(_('Cannot initialize new GD image stream')); $bgcolor = imagecolorallocate($im, 250, 250, 250); $txcolor = imagecolorallocate($im, 233, 14, 91); $border = imagecolorallocate($im, 180, 180, 180); $text = _('Resource not found!'); $font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'; $size = 12; $bbox = imagettfbbox($size, 0, $font, $text); $textWidth = $bbox[2] - $bbox[0]; $textHeight = $bbox[1] - $bbox[7]; $x = (240 - $textWidth) / 2; $y = (120 + $textHeight) / 2; imagettftext($im, $size, 0, $x, $y, $txcolor, $font, $text); imagerectangle($im, 0, 0, 239, 119, $border); // imagestring($im, 1, 5, 5, _('Resource not found!'), $txcolor); imagepng($im); imagedestroy($im); }