将 pdf 从 url 转换为图像时出错 - Laravel Imagick

问题描述 投票:0回答:2

在我的带有 sail (docker) 的 Laravel 应用程序中,我正在制作一个将 pdf 文档转换为图像的函数。

该函数接收一个 URL,其中包含 pdf 文档。
例如:“http://localhost:5000/my-application/hojavavida/8/documents/1676480714qhRZ57Bjl2nD673.pdf”

所有这些文档都来自Minio容器。
我使用Imagick进行转换,功能如下:

public function pdfToImage($pdfFilePath)    
{ 
        $imagick = new Imagick();
        $imagick->readImage($pdfFilePath);
        
        $filename = time().Str::random(15).'.'.'jpg';
        $imagick->setImageFormat('jpg');
        $imagick->writeImage(Storage::disk('tmp')->put($directory .'/'. 
        $filename, $imagick));
        dd("created");
}

但它返回以下消息:

"message": "Failed to read the file",
"exception": "ImagickException",

我已经检查过,通过传递另一个 pdf 文档的 URL,它能够读取它并从文档生成图像。

主要问题:我无法让它读取来自我的 docker 的 URL。

我进行了必要条件验证
验证容器上是否安装了 Imagick

php -i | grep imagick

imagick
imagick module => enabled
imagick module version => 3.7.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
imagick.allow_zero_dimension_images => 0 => 0
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.set_single_thread => 1 => 1
imagick.shutdown_sleep_count => 10 => 10
imagick.skip_version_check => 1 => 1

验证 GhostScript

which gs

/usr/bin/gs
php laravel-8 imagick
2个回答
0
投票

最后发现只有本地 URL 才是问题所在。 因此,当这些 URL 来自本地主机时,我会解析它们并将其转换为“http://minio:9000”。

// 从 URL 获取主机

$pdfFilePath = [];
    foreach ($urls as $url) {
        $host = parse_url($url, PHP_URL_HOST);
        if ($host == 'localhost') {
            $pdfFilePath[] = $this->transformUrl($url);
        }
    }

现在将新 URL 传递给 Imagick,图像转换是正确的。

$imagick = new Imagick();
        $imagePaths = [];
        foreach ($pdfFilePath as $pdf) {
            $imagick->readImage($pdf);
            $filename = pathinfo($pdf, PATHINFO_FILENAME);
            $imagick->setImageFormat('jpg');
            $imagick->writeImage(public_path($directory . '/' . $filename . '.jpg'));
            $imagePaths[] = url($directory . '/' . $filename . '.jpg');

-1
投票
Best solution for convert pdf to image using Imagick in Laravel

Get Pdf Path from Storage

`$pdf_path = Storage::disk('public')->path($product_asset->pdf_path);`

   
Create the Output Directory in Storage Folder

    $directory_create = Storage::disk('public')->path('products/pdf_images');
    if (!file_exists($directory_create)) {
       mkdir($directory_create, 0777, true);
    }
    $output_images = $directory_create.'/';
    
Call the imagick Lib with method to convert the high quality jpg images.

    $im = new Imagick();
    //Sets the image resolution
    $im->setResolution(250, 250);
    //Reads image from filename
    $im->readImage($pdf_path);
    $im->setImageFormat('jpg');
    $im->setImageCompression(Imagick::COMPRESSION_JPEG);
    //Sets the image compression quality.
    $im->setImageCompressionQuality(100);
    //Set the compression quality for newly created images
    $im->setCompressionQuality(100);
    //Clears all resources associated to Imagick object
    $im->clear();
    //Destroys the Imagick object
    $im->destroy();
© www.soinside.com 2019 - 2024. All rights reserved.