Contao 3.5 和 Imagick php 扩展
Contao 3.5 and Imagick php Extension
我正在尝试使用 php 扩展 "Imagick" 从 PDF 文件创建 JPG 图像。
我使用 Contao 3.5.14
有两种情况我需要此扩展才能工作:
第一种情况是当我在新闻文章中附加 PDF 文件时。
在本例中,我只是创建了一个自定义新闻列表模块,并为其分配了一个自定义模板。
在这个自定义模板中,我只是扔了一些 php 来保存我的 JPG。
它确实工作正常:
<div class="publication layout_full block<?= $this->class ?>">
<h2><?= $this->headline ?></h2>
<?php if ($this->hasMetaFields): ?>
<p class="info"><?php echo $this->parseDate("F Y", strtotime($this->date)); ?><?= $this->author ?> <?= $this->commentCount ?></p>
<?php endif; ?>
<?php if ($this->hasSubHeadline): ?>
<h4><?= $this->subHeadline ?></h4>
<?php endif; ?>
<?php if ($this->addImage): ?>
<div class='image_container'>
<img src='<?= $this->singleSRC ?>'/>
</div>
<?php endif; ?>
<?php if ($this->enclosure): ?>
<div class="enclosure">
<?php for($i=0;$i<count($this->enclosure);$i++) {
// the IMAGICK bit:
$pdf_file = $this->enclosure[$i]['enclosure'].'[0]';
$save_to = 'files/thumbnails/'.$this->enclosure[$i]['link'].'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat ("jpeg");
$im->writeImage ($save_to);
}
?>
<div class="vignette"><a target="_blank" href="<?= $this->enclosure[$i]['enclosure'] ?>" title="<?= $this->enclosure[$i]['title'] ?> (<?= $this->enclosure[$i]['filesize'] ?>)"><img src='<?= $save_to ?>'/></a></div>
<?php } ?>
</div>
<?php endif; ?>
// and so on...
</div>
现在来第二个让我很头疼的情况...
在使用 "Download content element".
时,我需要使用 Imagick 扩展从 PDF 创建 jpg
我修改了 ce_download.html5 模板以添加我的 Imagick 位:
<?php $this->extend('block_searchable'); ?>
<?php $this->block('content'); ?>
<!--------Here's the IMAGICK bit------------->
<?php
$pdf_file = $this->singleSRC.'[0]';
$save_to = 'files/thumbnails/'.$this->id.'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat ("jpeg");
$im->writeImage ($save_to);
}
?>
<!--------Here's end the IMAGICK bit------------>
<a href="<?= $this->href ?>" title="<?= $this->title ?>">
<div class="image_container">
<img style="width:100%;height:auto" src='<?= $save_to ?>' />
</div>
</a>
<div class="teaser">
<a href="<?= $this->href ?>" title="<?= $this->title ?>">
<h2> <?= $this->link ?> </h2>
</a>
</div>
<?php $this->endblock(); ?>
当试图转到我放置下载元素的文章时后台抛出的致命错误:
Fatal error: Uncaught exception ImagickException with message unable to open image `files/Folder/myFile.pdf': No such file or directory @ error/blob.c/OpenBlob/2589 thrown in templates/ce_download.html5 on line 11
#0 templates/ce_download.html5(11): Imagick->__construct('files/Folder...')
#1 system/modules/core/library/Contao/BaseTemplate.php(88): include('/home/www/clien...')
#2 system/modules/core/library/Contao/Template.php(277): Contao\BaseTemplate->parse()
#3 system/modules/core/classes/FrontendTemplate.php(46): Contao\Template->parse()
#4 system/modules/core/elements/ContentElement.php(289): Contao\FrontendTemplate->parse()
#5 system/modules/core/elements/ContentDownload.php(72): Contao\ContentElement->generate()
#6 system/modules/core/library/Contao/Controller.php(484): Contao\ContentDownload->generate()
#7 system/cache/dca/tl_content.php(1166): Contao\Controller::getContentElement(Object(Contao\ContentModel))
#8 system/modules/core/drivers/DC_Table.php(4321): tl_content->addCteType(Array)
#9 system/modules/core/drivers/DC_Table.php(378): Contao\DC_Table->parentView()
#10 system/modules/core/classes/Backend.php(650): Contao\DC_Table->showAll()
#11 system/modules/core/controllers/BackendMain.php(131): Contao\Backend->getBackendModule('article')
#12 contao/main.php(20): Contao\BackendMain->run()
#13 {main}
ce_download.html5 的第 11 行是
$im = new Imagick($pdf_file);
我首先认为这是一个与路径相关的问题,但并不是因为 JPG 已正确创建并在前端显示良好。
所以我想这一定与ce_模板的工作方式以及它们在后台的显示方式有关。
我真的不知道如何让这个 php 代码在不干扰 ce_download 模板的情况下工作。
我会非常感谢任何能帮助我的人。
此致
文尼
您应该通过 TL_ROOT
常量使用绝对路径。例如:
$pdf_file = TL_ROOT . '/' . $this->singleSRC.'[0]';
$save_to = TL_ROOT . '/files/thumbnails/'.$this->id.'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat("jpeg");
$im->writeImage($save_to);
}
我正在尝试使用 php 扩展 "Imagick" 从 PDF 文件创建 JPG 图像。
我使用 Contao 3.5.14
有两种情况我需要此扩展才能工作:
第一种情况是当我在新闻文章中附加 PDF 文件时。
在本例中,我只是创建了一个自定义新闻列表模块,并为其分配了一个自定义模板。
在这个自定义模板中,我只是扔了一些 php 来保存我的 JPG。
它确实工作正常:
<div class="publication layout_full block<?= $this->class ?>">
<h2><?= $this->headline ?></h2>
<?php if ($this->hasMetaFields): ?>
<p class="info"><?php echo $this->parseDate("F Y", strtotime($this->date)); ?><?= $this->author ?> <?= $this->commentCount ?></p>
<?php endif; ?>
<?php if ($this->hasSubHeadline): ?>
<h4><?= $this->subHeadline ?></h4>
<?php endif; ?>
<?php if ($this->addImage): ?>
<div class='image_container'>
<img src='<?= $this->singleSRC ?>'/>
</div>
<?php endif; ?>
<?php if ($this->enclosure): ?>
<div class="enclosure">
<?php for($i=0;$i<count($this->enclosure);$i++) {
// the IMAGICK bit:
$pdf_file = $this->enclosure[$i]['enclosure'].'[0]';
$save_to = 'files/thumbnails/'.$this->enclosure[$i]['link'].'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat ("jpeg");
$im->writeImage ($save_to);
}
?>
<div class="vignette"><a target="_blank" href="<?= $this->enclosure[$i]['enclosure'] ?>" title="<?= $this->enclosure[$i]['title'] ?> (<?= $this->enclosure[$i]['filesize'] ?>)"><img src='<?= $save_to ?>'/></a></div>
<?php } ?>
</div>
<?php endif; ?>
// and so on...
</div>
现在来第二个让我很头疼的情况...
在使用 "Download content element".
时,我需要使用 Imagick 扩展从 PDF 创建 jpg
我修改了 ce_download.html5 模板以添加我的 Imagick 位:
<?php $this->extend('block_searchable'); ?>
<?php $this->block('content'); ?>
<!--------Here's the IMAGICK bit------------->
<?php
$pdf_file = $this->singleSRC.'[0]';
$save_to = 'files/thumbnails/'.$this->id.'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat ("jpeg");
$im->writeImage ($save_to);
}
?>
<!--------Here's end the IMAGICK bit------------>
<a href="<?= $this->href ?>" title="<?= $this->title ?>">
<div class="image_container">
<img style="width:100%;height:auto" src='<?= $save_to ?>' />
</div>
</a>
<div class="teaser">
<a href="<?= $this->href ?>" title="<?= $this->title ?>">
<h2> <?= $this->link ?> </h2>
</a>
</div>
<?php $this->endblock(); ?>
当试图转到我放置下载元素的文章时后台抛出的致命错误:
Fatal error: Uncaught exception ImagickException with message unable to open image `files/Folder/myFile.pdf': No such file or directory @ error/blob.c/OpenBlob/2589 thrown in templates/ce_download.html5 on line 11
#0 templates/ce_download.html5(11): Imagick->__construct('files/Folder...')
#1 system/modules/core/library/Contao/BaseTemplate.php(88): include('/home/www/clien...')
#2 system/modules/core/library/Contao/Template.php(277): Contao\BaseTemplate->parse()
#3 system/modules/core/classes/FrontendTemplate.php(46): Contao\Template->parse()
#4 system/modules/core/elements/ContentElement.php(289): Contao\FrontendTemplate->parse()
#5 system/modules/core/elements/ContentDownload.php(72): Contao\ContentElement->generate()
#6 system/modules/core/library/Contao/Controller.php(484): Contao\ContentDownload->generate()
#7 system/cache/dca/tl_content.php(1166): Contao\Controller::getContentElement(Object(Contao\ContentModel))
#8 system/modules/core/drivers/DC_Table.php(4321): tl_content->addCteType(Array)
#9 system/modules/core/drivers/DC_Table.php(378): Contao\DC_Table->parentView()
#10 system/modules/core/classes/Backend.php(650): Contao\DC_Table->showAll()
#11 system/modules/core/controllers/BackendMain.php(131): Contao\Backend->getBackendModule('article')
#12 contao/main.php(20): Contao\BackendMain->run()
#13 {main}
ce_download.html5 的第 11 行是
$im = new Imagick($pdf_file);
我首先认为这是一个与路径相关的问题,但并不是因为 JPG 已正确创建并在前端显示良好。
所以我想这一定与ce_模板的工作方式以及它们在后台的显示方式有关。
我真的不知道如何让这个 php 代码在不干扰 ce_download 模板的情况下工作。
我会非常感谢任何能帮助我的人。
此致
文尼
您应该通过 TL_ROOT
常量使用绝对路径。例如:
$pdf_file = TL_ROOT . '/' . $this->singleSRC.'[0]';
$save_to = TL_ROOT . '/files/thumbnails/'.$this->id.'.jpg';
if (!file_exists($save_to)) {
$im = new Imagick($pdf_file);
$im->setImageFormat("jpeg");
$im->writeImage($save_to);
}