带有 cloudinary 的 spatie 媒体库

spatie media library with cloudinary

我正在尝试集成 spatie 媒体库包以使用 cloudinary 来托管和提供图像..

该软件包不支持 cloudinary,所以我不得不使用另一个旧软件包:flysystem-cloudinary

我还关注了关于 Whosebug 的讨论,其中有人也与之抗争:

我设法将图像上传到 cloudinary,但当我尝试检索它时,出现此错误:

Declaration of App\Cloudinary\CloudinaryUrlGenerator::getTemporaryUrl(): string must be compatible with Spatie\MediaLibrary\UrlGenerator\UrlGenerator::getTemporaryUrl(DateTimeInterface $expiration, array $options = Array): string

这是我的 CloudinaryUrlGenerator:

<?php

namespace App\Cloudinary;

use Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator;

class CloudinaryUrlGenerator extends BaseUrlGenerator
{
    const HOST = 'https://res.cloudinary.com/';

/**
 * Get the url for a media item.
 *
 * @return string
 */
public function getUrl(): string
{
    $cloudBaseUrl = self::HOST . config('filesystems.disks.cloudinary.cloud_name') . '/';

    $options = [
        'q_auto',
    ];

    $filePathIncludingFilenameAndExtension = '/' . $this->pathGenerator->getPath($this->media) . $this->media->file_name;

    return $cloudBaseUrl . implode(',', $options) . $filePathIncludingFilenameAndExtension;
}

/**
 * Get the temp url for a media item.
 *
 * @return string
 */
public function getTemporaryUrl(): string
{
    return $this->getUrl();
}

/**
 * Get the responsive images directory url for a media item.
 *
 * @return string
 */
public function getResponsiveImagesDirectoryUrl(): string
{
    return $this->getUrl();
}
}

我试过函数定义,但没有解决。

如果您正在使用< v7,则可以使用它。

v7发布以来,getTemporaryUrl有两个参数:

getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string;

您可以将这些参数添加到您的方法中:

use DateTimeInterface;

// 

public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
{
    return $this->getUrl();
}
``