【问题标题】:spatie media library with cloudinary带有 cloudinary 的 spatie 媒体库
【发布时间】:2020-06-14 02:50:14
【问题描述】:

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

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

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

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

App\Cloudinary\CloudinaryUrlGenerator::getTemporaryUrl()的声明:string必须兼容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();
}
}

我尝试使用函数定义,但没有解决。

【问题讨论】:

  • 您认为通过阅读错误可能有什么问题?尤其是“必须兼容”部分?
  • 嗨@ThomasVanderVeen,感谢您的回复-我尝试将函数定义重写为与 spatie 选项相同:getTemporaryUrl(DateTimeInterface $expiration, array $options): string 但它不起作用并产生了同样的错误。
  • 确保导入所有需要的类。如果做得正确,我认为应该没有问题。您确定错误是由于 your App\Cloudinary\CloudinaryUrlGenerator 类中的实现而引发的(只是为了确定)?

标签: laravel cloudinary


【解决方案1】:

如果您使用的是&lt; v7,则可以使用它。

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

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

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

use DateTimeInterface;

// 

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

【讨论】:

  • thx 但我使用的是 V7 .. 我实际上完全按照你写的那样解决了这个问题,但我还有另一个问题.. 在 cloudinary 中覆盖图像不起作用......真的很糟糕这不是一个包.. 使用 s3 和 cloudfront/flare 会更好吗?我认为 cloudinary 包是有问题的
猜你喜欢
  • 1970-01-01
  • 2019-02-01
  • 2018-09-15
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 2021-07-12
  • 2021-03-03
  • 2018-10-08
相关资源
最近更新 更多