0

I'm trying to integrate spatie media library package to use cloudinary to host and serve images..

the package didnt support cloudinary so I had to use another old package: flysystem-cloudinary

I also followed a discussion on stackoverflow where someone also battled it: spatie-cloudinary

I managed to upload an image to cloudinary but when I'm trying to retrieve it, I get this error:

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

This is my 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();
}
}

I tried to play with the function definition but it didnt solve it.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
nscode
  • 147
  • 1
  • 9
  • What do you think could be wrong by reading the error? Especially the "must be compatible with" part? – Thomas Van der Veen Mar 01 '20 at 18:06
  • hi @ThomasVanderVeen, thanks for replying - I tried to rewrite the function definition to be same as the spatie option: getTemporaryUrl(DateTimeInterface $expiration, array $options): string but it didnt work and produced the same error. – nscode Mar 01 '20 at 18:09
  • Make sure you import all the classes you need. If done correctly I think there should not be an issue. Are you sure the error is thrown because of the implementation in **your** `App\Cloudinary\CloudinaryUrlGenerator` class (just to be sure)? – Thomas Van der Veen Mar 01 '20 at 18:12

1 Answers1

1

You can use it if you are using < v7.

Since v7 was released, getTemporaryUrl has two parameters :

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

You can add these parameters to your method :

use DateTimeInterface;

// 

public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
{
    return $this->getUrl();
}
``
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
  • thx but I'm using V7.. I actually fixed the problem exactly like you wrote, but I have another problem.. the overwrite of images in cloudinary doesnt work... It really sucks there isn't a package for this.. Would I be better off with s3 and cloudfront/flare? I think the cloudinary package is the problematic – nscode Mar 01 '20 at 21:50