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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?