dougong5817 2017-11-21 08:48
浏览 85
已采纳

Contao 3.5和Imagick php扩展

I am trying to use the php extension "Imagick" in order to create JPG images from PDF files.
I use Contao 3.5.14

There are two situations where I need this extension to work:
The first situation is when I attach a PDF file to a news article.
In this case, I just create a custom News List module, which I assign a custom template to.
In this custom template, I just throw some php to save my JPG.
It does work fine:

<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> 

Now here comes the second situation which gives me headaches...
I need to use the Imagick extension to create jpg out of PDFs while using the "Download content element".
I modified the ce_download.html5 template in order to add my Imagick bit:

<?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(); ?>

And the fatal error thrown in back office when trying to go to the article where I placed my Download element:

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}

line 11 of ce_download.html5 is $im = new Imagick($pdf_file);
I first thought it was a path-related issue but it's not because the JPG is correctly created and displays fine in the Front-end. So I guess it must be related with the way ce_ templates work and how they display in the back-office.
I really don't know how to get this php code to work without interfering with the ce_download template.

I would be so thankful to anyone who could help me out there.
Regards
Vinny

  • 写回答

1条回答 默认 最新

  • doujingdai5521 2017-11-21 13:00
    关注

    You should use absolute paths via the TL_ROOT constant. e.g.:

    $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加