[英]Create thumbnail image for PDF in Smarty
我在Smarty模板中有以下代碼,該模板可生成文件鏈接(將為PDF格式):
{foreach from=$attachments_data item="file"}
<p class="attachment__item">
{$file.description} ({$file.filename}, {$file.filesize|formatfilesize nofilter}) [<a class="attachment__a cm-no-ajax" href="{"attachments.getfile?attachment_id=`$file.attachment_id`"|fn_url}">{__("download")}</a>]
</p>
{/foreach}
我在另一個問題中發現了一種使用PHP從PDF生成預覽圖像的方法。
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
有沒有一種方法可以將此PHP轉換為Smarty,以便與CS-Cart模板文件一起使用? 還是有其他方法可以做到這一點?
我的偏好是將它們緩存到臨時文件夾中以進行靜態讀取,但是,您可以按需使用已提供的那部分PHP輕松完成此操作
請實施更好的文件系統安全性
pdf_to_image.php
<?php
// you must create better file system security than this
$filename = 'directory_with_pdfs' . DIRECTORY_SEPERATOR . basename( $_GET['filename'] );
if( file_exists( $filename ) === true )
{
$im = new imagick( $filename . '[0]' );
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
echo $im;
}
else
{
http_response_code(404);
}
Smarty語法
{foreach from=$attachments_data item="file"}
<img src="pdf_to_image.php?filename={$file.filename|urlencode}">
{/foreach}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.