croxton / imgixer

Generate Imgix URLs in Craft 3
MIT License
11 stars 4 forks source link

Subfolder in imgix url when using Element API and Google Cloud as a bucket #12

Closed eriknoorland closed 1 year ago

eriknoorland commented 1 year ago

Hi,

I'm in the process of switching asset storage from Amazon S3 to Google Cloud and I'm running into an issue regarding the Element API for this plugin.

I have configured a subfolder for the Google Cloud plugin but the environment variable name (GCP_ASSET_VOLUME_SUBFOLDER) I use for it seems to show up in my imgix url (both the value or the actual subfolder name shouldn't be in the url at all).

https://<my-domain>.imgix.net/%24GCP_ASSET_VOLUME_SUBFOLDER/<my-image>?auto=compress%2Cformat&dm=1673516825&ixlib=php-3.1.0&q=80&w=300

Have I misconfigured something here or is this a bug?

croxton commented 1 year ago

Have you created a config file at /config/imgixer.php?

Is so, are you passing an Asset object ($asset) object or an image path ($asset->path) when retrieving the image via the Element API?

'image'   => (new ImgixerTwigExtension)->imgix($asset, array(
    'ar' => '16:9',
    'w' => 2000,
    'signed' => true
))

If you are passing an Asset object, you can remove any subfolder value in the config for your source (/config/imgixer.php) - Craft will be able to parse it from your filesystem settings (it will parse any environmental value you used).

I have configured a subfolder for the Google Cloud plugin but the environment variable name (GCP_ASSET_VOLUME_SUBFOLDER) I use for it seems to show up in my imgix url (both the value or the actual subfolder name shouldn't be in the url at all).

Perhaps I've misunderstood, but why wouldn't a subfolder appear in the URL? If you enter a value for the subfolder on the filesystem screen that will become part of the URL, no? Or do you mean you have Imgix configured as a web proxy pointing to a specific subfolder of a GCP bucket, rather than directly connected to the bucket? If so, you don't need to tell Craft or Imgixer about it at all, "subfolders" you manage in Craft would actually be child folders of that subfolder.

eriknoorland commented 1 year ago

My config/imgixer.php looks like this:

return [
  'sources' => array(
    'default' => array(
      'domain' => getenv('IMGIX_DOMAIN'),
      'key' => getenv('IMGIX_KEY'),
      'defaultParams' => [
        'auto' => 'compress,format',
        'q' => '80',
      ],
    ),
  ),
];

I am passing as Asset object to the imgix function like so:

$image = $entry->featuredImage->one();

(new ImgixerTwigExtension)->imgix($image, array('w' => 300))

On the website I the twig templates Imgixer works very well. The urls look like the following: https://<domain>.imgix.net/<image>. There is no subfolder in the url even though it is configured in Google Cloud (which is how I expect it to work).

It's just the Element API that seems to have an issue when configuring a subfolder in Google Cloud config. The url looks like this: https://<domain>.imgix.net/%24GCP_ASSET_VOLUME_SUBFOLDER/<image>

First of all the env variable isn't being replaced but second, it shouldn't even be in there at all when you compare it too how it is compiled in the twig templates.

I don' think there is any proxying going on. It seems there is a difference in rendering the url between the twig template function and the Element API function.

croxton commented 1 year ago

Are you passing the Asset path to Imgixer in your Twig templates rather than the Asset itself, e.g:

{{ image.path | imgix({ ar:'16:9', w:1024 }) }}

In Imgix, did you add the bucket as a Google Cloud Storage source or as a Web Folder, pointing to a subfolder of a GCP bucket? https://docs.imgix.com/setup/creating-sources/google-cloud-storage https://docs.imgix.com/setup/creating-sources/web-folder

eriknoorland commented 1 year ago

In the Twig templates I'm passing the asset path as shown in the docs and your comment.

It seems that our Imgix source is still set to Amazon S3. I will create a new Imgix source with a Google Cloud setup and try that first.

eriknoorland commented 1 year ago

Changing the Element API implementation to:

(new ImgixerTwigExtension)->imgix($image->path, array('w' => 300))

using the asset path seems to work. So I think I'll be using that.

Thanks for the fast response and thinking along. I really appreciate it.