【问题标题】:Imagick: Failed to read the fileimagick:无法读取文件
【发布时间】:2018-07-11 08:14:18
【问题描述】:

我正在使用 Sonata Media Bundle 和 Imagick,希望为 PDF 生成预览图像。 Ghostscript 已安装。我尝试以下方法:

    $binaryContent = $media->getBinaryContent();
    $tempPdfFile = tempnam(sys_get_temp_dir(),'pdf');
    $tempJpegFile = tempnam(sys_get_temp_dir(),'jpeg');
    file_put_contents($tempPdfFile, $binaryContent);

    rename($tempPdfFile, $tempPdfFile . '.pdf');
    rename($tempJpegFile,$tempJpegFile . '.jpg');

    $im = new \Imagick($tempPdfFile . '.pdf');
    $im->setImageColorspace(255);
    $im->setResolution(72,72);
    $im->setCompressionQuality(75);
    $im->setImageFormat('jpeg');
    $im->writeImage($tempJpegFile.'.jpg');
    $im->clear();
    $im->destroy();

...但我收到此错误消息:

读取文件失败

...上线$im = new \Imagick($tempPdfFile . '.pdf');

我在这里做错了什么?

====

附加信息:我也试过$im = new \Imagick($tempPdfFile . '.pdf[0]');,没有任何改善。

【问题讨论】:

    标签: php symfony imagick


    【解决方案1】:

    这大致是为了让我对 Imagick 可以读取的文件有一个有用的参考。

    public function preUpdate(Document $document)
    {
        $media = $document->getMedia();
        if ($media === null) {
            return;
        }
        $mediaName = $media->getName();
        if ($media->getContentType() !== 'application/pdf') {
            return;
        }
    
        $context = $media->getContext();
        $formats = $pool->getFormatNamesByContext($context);
    
        if (null === $formats) {
            return;
        }
    
        $provider = $this->getProvider($media->getProviderName());
        $publicUrls = [];
    
        $fullFilePath = $provider->getReferenceImage($media);
    
    
    
    }
    
    ...
    
    private function getPool()
    {
        return $this->container->get('sonata.media.pool');
    }
    
    ...
    
    private function getProvider($name)
    {
        return $this->container->get($name);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2017-03-16
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多