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

Extensible dimension calculators #193

Merged
merged 45 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d3e279b
Extensible DimensionCalculator
ncla Jan 12, 2023
8dbe3f6
Fix styling
ncla Jan 12, 2023
d7b4ee6
Decouple Sources from Breakpoints
ncla Jan 15, 2023
a0ff0db
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 15, 2023
3c5217f
Fix styling
ncla Jan 15, 2023
80bd4be
Determine mimetype for original format in Source
ncla Jan 16, 2023
687806a
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 16, 2023
9ac43d1
Fix styling
ncla Jan 16, 2023
9c454c9
Only pass Breakpoint/Source, no Asset since you can retrieve it throu…
ncla Jan 16, 2023
78f05a2
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 16, 2023
7c7b278
Test custom dimension calculator; do not output <source> if srcset is…
ncla Jan 17, 2023
f37d387
Simplify ResponsiveDimensionCalculator by removing maxWidth arg to ca…
ncla Jan 20, 2023
ca6b9df
Cleanup, protect properties to read-only, rename `breakpointParams` t…
ncla Jan 21, 2023
fccc5cb
Fix styling
ncla Jan 21, 2023
6c22dce
<img> tag will now receive src from Glide instead of linking to origi…
ncla Jan 21, 2023
6c38cac
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 21, 2023
1675606
Resolve warnings about float number conversion to int
ncla Jan 21, 2023
bf2af0e
Cache getSources() and breakpoints()
ncla Jan 21, 2023
e1f6f71
Fix styling
ncla Jan 21, 2023
7c4a241
If it is `breakpoints()` in `Responsive` then it should be `sources()…
ncla Jan 21, 2023
9dad7ce
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 21, 2023
bcdb288
Change `calculate` to `calculateForBreakpoint`
ncla Jan 21, 2023
1c64971
Implement GraphQL ResponsiveFieldtype test
ncla Jan 21, 2023
c023233
Fix styling
ncla Jan 21, 2023
193b7a4
Fix addon fiedltypes not being registered due to unnecessary slash in…
ncla Jan 21, 2023
79026a6
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 21, 2023
b793115
Remove comment
ncla Jan 22, 2023
02e69a7
Revert `params` to `parameters`
ncla Jan 22, 2023
b1391ff
Remove duplicate asset ID in blink cache key
ncla Jan 22, 2023
2a8eb71
Update PHPDoc for DimensionCalculator
ncla Jan 22, 2023
de06281
Fix styling
ncla Jan 22, 2023
d0ce8e2
Remove no longer used `assetHeight` method on `Responsive` class
ncla Jan 22, 2023
bc8029e
Merge branch 'feature/167' of github.com:ncla/statamic-responsive-ima…
ncla Jan 22, 2023
1e0463b
Remove unnecessary conditional
ncla Jan 22, 2023
a12e43f
Check width exact value
ncla Jan 22, 2023
522f41c
Update README
ncla Jan 28, 2023
a5adfad
Add UPGRADE.md
ncla Jan 28, 2023
e1308a7
Nitpick
ncla Jan 28, 2023
fbfd5b4
Nitpick
ncla Jan 28, 2023
e748cea
Nitpick
ncla Jan 28, 2023
f19e51d
Nitpick
ncla Jan 28, 2023
ba54990
Nitpick
ncla Jan 28, 2023
3a9c93e
A kick to trigger Github actions
ncla Jan 28, 2023
693965b
Merge remote-tracking branch 'upstream/main' into feature/167
ncla Jan 28, 2023
eccc6ef
Merge remote-tracking branch 'upstream/main' into feature/167
ncla Jan 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,31 +188,6 @@ This addon comes with 2 GraphQL goodies, it adds a `responsive` field to all ass

You can retrieve a responsive version of an asset like this:

```graphql
{
entries {
data {
id,
image {
responsive {
label
value
unit
ratio
mediaString
srcSet
srcSetWebp
srcSetAvif
placeholder
}
}
}
}
}
```

Setting parameters like a ratio is also possible:

```graphql
{
entries {
Expand All @@ -221,14 +196,18 @@ Setting parameters like a ratio is also possible:
image {
responsive(ratio: 1.2) {
label
value
unit
minWidth
widthUnit
ratio
mediaString
srcSet
srcSetWebp
srcSetAvif
placeholder
sources {
format
mimeType
minWidth
mediaWidthUnit
mediaString
srcSet
placeholder
}
}
}
}
Expand All @@ -248,14 +227,18 @@ A responsive fieldtype has all the same fields as a normal responsive field from
art_image {
breakpoints {
label
value
unit
ratio
mediaString
srcSet
srcSetWebp
srcSetAvif
placeholder
minWidth
widthUnit
ratio
sources {
format
mimeType
minWidth
mediaWidthUnit
mediaString
srcSet
placeholder
}
}
}
}
Expand Down
99 changes: 99 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Upgrade Guide

# Upgrading to 3.x from 2.x

`Breakpoint` object now contains `Source` objects which represent the `<source>` tag.

## High impact

### responsiveImage.blade.php template changes

If you had published the `responsiveImage.blade.php` template in your project, you will need to update it to use the
new `Source` objects.

If you had published the templates, you will find them in `resources/views/vendor/responsive-images`. If you do not see
this file in your project then you do not have to do anything for this.

To review the changes, please
use [the history of the template](https://github.com/spatie/statamic-responsive-images/commits/main/resources/views/responsiveImage.blade.php)
in this repository. We recommend just copy and pasting the whole template and adding your changes as you go.

### GraphQL query changes

If you are using GraphQL in your project, you will have to update your queries, as each breakpoint will now contain array of sources.

If you had a query like this:

```graphql
{
entries {
data {
id,
image {
responsive(ratio: 1.2) {
label
value
unit
ratio
mediaString
srcSet
srcSetWebp
srcSetAvif
placeholder
}
}
}
}
}
```

Then that would become:

```graphql
{
entries {
data {
id,
image {
responsive(ratio: 1.2) {
label
minWidth
widthUnit
ratio
sources {
format
mimeType
minWidth
mediaWidthUnit
mediaString
srcSet
placeholder
}
}
}
}
}
}
```

Additionally, some properties have changed name to be more descriptive of what they are:
- `value` to `minWidth`
- `unit` to `widthUnit`

A new addition is `mimeType` property which you should set on `<source>` tags to help browsers determine which image format to serve.

These changes are also in effect when you query responsive fieldtype, except the data is under `breakpoints` key, just like before.

To easily test the GraphQL changes and see what is available, we recommend using built-in GUI which you can visit through `/cp/graphiql` in your Statamic project. This tool provides auto-completion.

## Low impact

`<img>` `src` URL will always now go through Glide instead of pointing to original asset. Meaning, the filename will become base64 encoded by Statamic Glide controller. If you relied on readable filenames for SEO purposes then this may impact you. As a workaround:
- Let this happen and utilize `alt` attribute instead to help crawlers understand what the image contains
- Update `src` attribute to be `{{ $asset['url'] }}`. This will point to the original, source asset. However, as this is original asset - any image manipulations you have set will not happen on this image.

This is low impact change because most modern browsers will not hit the URL provided in `<img>` tag if `<source>` tags are provided.

## Very low impact

If you had made any changes or used objects in a way that are out of scope of the provided README in this project then please review the commits manually.
40 changes: 16 additions & 24 deletions resources/views/responsiveImage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,21 @@
@endonce

<picture>
@foreach (($sources ?? []) as $source)
@isset($source['srcSetAvif'])
<source
type="image/avif"
@isset($source['media']) media="{{ $source['media'] }}" @endisset
srcset="{{ $source['srcSetAvif'] }}"
@if($includePlaceholder ?? false) sizes="1px" @endif
>
@endisset
@isset($source['srcSetWebp'])
<source
type="image/webp"
@isset($source['media']) media="{{ $source['media'] }}" @endisset
srcset="{{ $source['srcSetWebp'] }}"
@if($includePlaceholder ?? false) sizes="1px" @endif
>
@endisset
@foreach (($breakpoints ?? []) as $breakpoint)
@foreach($breakpoint->sources() ?? [] as $source)
@php
$srcSet = $source->getSrcset();
@endphp

<source
@isset($source['media']) media="{{ $source['media'] }}" @endisset
srcset="{{ $source['srcSet'] }}"
@if($includePlaceholder ?? false) sizes="1px" @endif
>
@if($srcSet !== null)
<source
@if($type = $source->getMimeType()) type="{{ $type }}" @endif
@if($media = $source->getMediaString()) media="{{ $media }}" @endif
srcset="{{ $srcSet }}"
@if($includePlaceholder ?? false) sizes="1px" @endif
>
@endif
@endforeach
@endforeach

<img
Expand All @@ -52,8 +44,8 @@
@endunless
@isset($width) width="{{ $width }}" @endisset
@isset($height) height="{{ $height }}" @endisset
@isset($sources)
@if($hasSources)
data-statamic-responsive-images
@endisset
@endif
>
</picture>
Loading