将Cloudinary与spatie/media-library Laravel软件包一起使用 [英] Using Cloudinary with spatie/media-library Laravel package

查看:256
本文介绍了将Cloudinary与spatie/media-library Laravel软件包一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在Cloudinary上使用Laravel软件包spatie/media-library吗?
我以为用flysystem实现它是微不足道的.
实际上,我实际上将Cloudinary用作具有silvanite/nova-field-cloudinary的Nova字段,它的效果很好,但是我需要一个媒体libaray,它不提供现成的支持.

Is anyone using Laravel package spatie/media-library with Cloudinary?
I thought implementing it would be trivial with flysystem.
I'm actually using Cloudinary as a Nova field with silvanite/nova-field-cloudinary and it works great but I have a need for the media-libaray which doesn't support it out of the box.

所以,我做了什么:
-添加云盘:

So, what I did:
- add cloudinary disk:

'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'),
],

  • 将磁盘名称更改为cloudinary

    • changed disk name to cloudinary

      'disk_name' => 'cloudinary',
      

    • 尝试添加图片

    • tried adding the image

      $newMedia = $myModel
          ->addMediaFromUrl($imageUrl)
          ->setFileName($filename)
          ->toMediaCollection();
      

    • ,然后出现找不到文件"错误.图像已上传,但记录未保存到数据库.

      and then I get "File not found" error. The image is uploaded but record is not saved to 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上做到这一点.

      So it seems the problem is it's trying to load the image using local path which of course doesn't exist.
      I tried using a custom PathGenerator so it returns the url that's no solution since it expects a path.
      I can't figure out how they do it for S3.

      因此,如果有人知道如何解决此问题或有可行的解决方案,我将不胜感激.

      So if anyone knows how to solve this or has a working solution I would appreciate it.

      推荐答案

      之所以会发生这种情况,是因为spatie软件包正在尝试对上载的图片进行转换.转到medialibrary.php配置文件并注释掉以下几行:

      This happens because the spatie package is trying to make conversion of the uploaded image. Go to the medialibrary.php config file and comment out these lines:

          '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,
          ],
      

      当找不到给定图像哑剧类型的生成器时,将不会转换图像.这样就不会抛出错误.然后使用cloudinary转换图像.

      When no generator is found for the given image mime-type, the image will not be converted. Thus the error will not be thrown. Then convert your images using cloudinary.

      为此,我创建了扩展BaseUrlGeneratorCloudinaryUrlGenerator.

      For this I've created a CloudinaryUrlGenerator which 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;
          }
      

      getTemporaryUrlgetResponsiveImagesDirectoryUrl中,我只是返回了:$this->getUrl().

      In getTemporaryUrl and getResponsiveImagesDirectoryUrl I've simply returned: $this->getUrl().

      完成此操作后,您可以像这样获得图像:

      Once you've done this, then you can get your images like so:

      <img src="{{ $model->media->first()->getUrl() }}" />
      

      这篇关于将Cloudinary与spatie/media-library Laravel软件包一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆