Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Filesystem Disks and Watermarks in Glide #5725

Merged
merged 24 commits into from
Apr 20, 2022

Conversation

jasonvarga
Copy link
Member

@jasonvarga jasonvarga commented Apr 1, 2022

This PR adds support for using any Flysystem disk (defined in config/filesystems.php) for the Glide cache.
Closes #5143

It also adds support for Glide's watermark feature.
Closes statamic/ideas#84

Cache Disks

You can:

  • keep caching "disabled" (the default - there's still technically a cache, it's just within storage)
  • specify the path (the current way to do it - for backwards compatibility)
  • and the new way is to specify the name of a filesystem disk so you can do whatever you want, eg. using S3.

The configuration is explained in more detail on the docs: statamic/docs#791

Absolute URL usage

The glide tag currently outputs relative urls by default, and you can opt into absolute URLs using a parameter, like this:

{{ glide:whatever width="100" absolute="true" }}

If you're using something like S3, it'd be lame to have to add absolute="true" to every tag.

This PR makes it so that if your Glide route is absolute, all the tags will be absolute by default, without the need for a parameter.

Caching Layer

The way Glide works, it will check the cache filesystem to see if the image has already been generated. That way, it won't try to re-generate it.

If you're using an API based disk like S3, it'll be an API call just to check if a file exists. This definitely causes a slowdown.

To combat this, we will now cache the path once it's generated. Then the next time you use a glide tag, it will check the cache first (which is fast) and not bother making an API call at all.

This Glide cache will be separate from your regular application cache, so that during deployments or other times when you might run artisan cache:clear, your Glide cache doesn't get wiped.

However, when you run please glide:clear, it'll get cleared, as expected.

The Glide cache store will be on the local filesystem by default. If you want a custom one (e.g. using redis), you can create a new store in config/cache.php named glide.

Watermarks

You may now provide watermarks to Glide with no additional setup needed. Simply feed it a mark parameter, which is a reference to an image, and can be any format that the Glide tag would use as a source.

For example, you could give it a relative path, which will assume it's in your public directory:

{{ glide:src mark="logos/small.jpg" }} // assumes its in public/logos/small.jpg

a relative URL:

{{ glide:src mark="/assets/watermark.jpg" }} // if its a url of an asset, it'll use that, otherwise assume public/assets/watermark.jpg

an external URL:

{{ glide:src mark="http://anothersite.com/watermark.jpg" }}

or an asset, by passing a field:

{{ glide:src :mark="assetfield" }}

Todo:

  • Tests for Flysystem v1
  • Ensure I didn't break multisite usage (as per Improve multisite Glide support #2379)
  • The glide:generate tag has some assumptions its on a local disk.
  • Support watermarks (see Add "markalpha" support to the Glide tag #4129)
  • Rename the GlideServer class since it does a little more now.
  • Tidy routes/web.php so it uses methods in GlideServer.
  • glide:clear command
  • Performance considerations? The cache disk gets checked to see if the image is generated. If it's on S3 that means many API calls potentially. Probably need to add a layer of caching.
  • Docs Improve glide docs docs#791

@jasonvarga jasonvarga marked this pull request as ready for review April 13, 2022 16:52
@jasonvarga jasonvarga changed the title Support Filesystem Disks in Glide Support Filesystem Disks and Watermarks in Glide Apr 13, 2022
@martyf
Copy link
Contributor

martyf commented Apr 13, 2022

For watermarks, in the GlideImageManipulator, there are a few missing mark configuration options that would be really useful. Can these please be added the private $api:

  • markalpha
  • markh
  • markfit

(not sure if I'm able to contribute to the PR myself - happy to make this change if it's something I'm able to... still new to contributing, sorry)

Admittedly these three were missing in my initial PR for watermarks, but were added in a later commit.

After upgrading a site that overrode the GlideImageManipulator (to get watermarks working in 3.2), the only issue was these missing properties. WIth these aded back to the $api, watermarks start working a treat 👍

@jasonvarga
Copy link
Member Author

Done. Thanks!

@martyf
Copy link
Contributor

martyf commented Apr 13, 2022

Is there a reason (with Glide or other) why markh wasn't added? Just in case someone wants to size a watermark based on height, not width (markw)?

@jasonvarga
Copy link
Member Author

No reason at all. Whoops!

@martyf
Copy link
Contributor

martyf commented Apr 13, 2022

Woop! Love your stuff 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Filesystem disks within Glide Glide Watermarking
2 participants