有人在使用Cloudinary的Laravel包空间/媒体库吗?
我原以为用飞天系统来实现它是件很简单的事。
实际上,我正在使用Cloudinary作为一个Nova域,使用silvanite/nova- field -cloudinary,它工作得很好,但我需要媒体的支持,它不支持它的盒子。
所以,我所做的:
'cloudinary' => [
'driver' => 'cloudinary',
'api_key' => env('CLOUDINARY_API_KEY'),
'api_secret' => env('CLOUDINARY_API_SECRET'),
'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
'url' => env('CLOUDINARY_URL'),
],->toMediaCollection();
然后我得到了“文件未找到”错误。图像已上载,但记录未保存到DB。
#message: "File not found at path: 711297/my-image.jpg"
#code: 0
#file: "./vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php"
#line: 435
-previous: League\Flysystem\FileNotFoundException^ {#2389
#path: "711297/my-image.jpg"
#message: "File not found at path: 711297/my-image.jpg"
#code: 0
#file: "./vendor/league/flysystem/src/Filesystem.php"
#line: 389
trace: {
./vendor/league/flysystem/src/Filesystem.php:389 { …}
./vendor/league/flysystem/src/Filesystem.php:194 { …}
./vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:433 { …}
./vendor/spatie/laravel-medialibrary/src/Filesystem/Filesystem.php:81 { …}
./vendor/spatie/laravel-medialibrary/src/Filesystem/Filesystem.php:88 { …}
./vendor/spatie/laravel-medialibrary/src/FileManipulator.php:77 { …}
./vendor/spatie/laravel-medialibrary/src/FileManipulator.php:44 { …}
./vendor/spatie/laravel-medialibrary/src/Filesystem/Filesystem.php:33 { …}
./vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:310 { …}
./vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:301 { …}
./vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php:251 { …}
./vendor/efdi/carmarket-module/src/Jobs/ImportImages.php:145 { …}
./vendor/efdi/carmarket-module/src/Jobs/ImportImages.php:84 { …}
./vendor/efdi/carmarket-module/src/Jobs/ImportImages.php:43 { …}所以问题似乎是,它试图使用本地路径加载图像,这当然是不存在的。
我尝试使用自定义的PathGenerator,因此它返回url,这是没有解决方案的,因为它需要路径。
我不知道他们是怎么为S3做的。
因此,如果有人知道如何解决这个问题,或者有一个可行的解决方案,我将不胜感激。
发布于 2019-11-26 08:08:59
之所以会发生这种情况,是因为spatie包试图对上传的图像进行转换。转到medialibrary.php配置文件并注释掉以下行:
'image_generators' => [
Spatie\MediaLibrary\ImageGenerators\FileTypes\Image::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Webp::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg::class,
Spatie\MediaLibrary\ImageGenerators\FileTypes\Video::class,
],当没有为给定的图像mime类型找到生成器时,图像将不会被转换.因此,错误将不会抛出。然后使用cloudinary转换您的图像。
为此,我创建了一个扩展CloudinaryUrlGenerator的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;
}在getTemporaryUrl和getResponsiveImagesDirectoryUrl中,我只是返回了:$this->getUrl()。
一旦你这样做了,你就可以得到这样的图像:
<img src="{{ $model->media->first()->getUrl() }}" />https://stackoverflow.com/questions/58977528
复制相似问题