【问题标题】:Contao 3.5 and Imagick php ExtensionContao 3.5 和 Imagick php 扩展
【发布时间】:2018-05-04 15:54:12
【问题描述】:

我正在尝试使用 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> 

现在是第二个让我头疼的情况...
在使用“下载内容元素”时,我需要使用 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_ 模板的工作方式以及它们在后台的显示方式有关。
我真的不知道如何在不干扰 ce_download 模板的情况下让这个 php 代码工作。

我会非常感谢任何可以帮助我的人。
问候
文尼

【问题讨论】:

  • 试试new Imagick(TL_ROOT . '/' . $pdf_file);
  • @fritzmg 哇!它现在可以工作了......所以这是一个路径问题:-/请随时更改您的评论作为答案,这样我就可以选择它作为赖特的答案。谢谢一百万...

标签: php imagick php-extension contao


【解决方案1】:

您应该通过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);
}

【讨论】:

    猜你喜欢
    • 2012-11-03
    • 2011-02-19
    • 2013-10-23
    • 2021-09-30
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多