-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# API | ||
|
||
Timmy started out with functions that returned markup. But this made it harder to access data that you might need before accessing an image. All [functions](https://github.com/mindkomm/timmy/blob/master/docs/functions.md) still work fine, but with version 1.0.0, there’s a new API. | ||
|
||
The new API works mostly with the `Timmy\Image` class. You can use it to get data about an image with a certain size and also generate the markup you need. | ||
|
||
First, to get a Timmy image, you can use `Timmy\Timmy::get_image()`. | ||
|
||
**PHP** | ||
|
||
```php | ||
<?php | ||
|
||
use Timmy\Timmy; | ||
|
||
$image = Timmy::get_image( $attachment_id, 'large' ); | ||
``` | ||
|
||
**Twig** | ||
|
||
```twig | ||
{% set image = get_timmy_image( attachment_id, 'large' ) %} | ||
``` | ||
|
||
This function returns a `Timmy\Image` object if you pass an ID for an existing attachment and `null` if it’s not a valid attachment. | ||
|
||
As soon as you have that image, you can start interacting with it. | ||
|
||
```twig | ||
{% if image %} | ||
{% if image.is_webp %} | ||
<picture> | ||
{{ image.picture_responsive }} | ||
<picture> | ||
{% else %} | ||
<img {{ image.image_responsive }}> | ||
{% endif %} | ||
{% endif % | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Extending Timmy | ||
|
||
When you work with the image class, you may find that you need functionality that doesn’t exist in the Image class. In that case, you can extend Timmy to use your own class that extends `Timmy\Image`. | ||
|
||
Here’s an example where we tell Timmy to use a custom TimmyImage class that we extend with an `aspect_class()` method that returns a CSS class we can use based on the aspect ratio of an image. | ||
|
||
**functions.php** | ||
|
||
```php | ||
add_filter( 'timmy/image/class', function( $class ) { | ||
return TimmyImage::class; | ||
} ); | ||
``` | ||
|
||
**TimmyImage.php** | ||
|
||
```php | ||
<?php | ||
|
||
use Timmy\Image; | ||
|
||
/** | ||
* Class TimmyImage | ||
*/ | ||
class TimmyImage extends Image { | ||
/** | ||
* Gets a CSS class based on the aspect ratio. | ||
* | ||
* This also works for SVG images. | ||
* | ||
* @return string|null | ||
*/ | ||
public function aspect_class() { | ||
if ( $this->is_squarish() ) { | ||
return 'isSquare'; | ||
} elseif ( $this->is_landscape() ) { | ||
return 'isLandscape'; | ||
} | ||
|
||
return 'isPortrait'; | ||
} | ||
} | ||
``` | ||
|
||
In Twig, you can then access the method through `image.aspect_class`: | ||
|
||
```twig | ||
<img class="{{ image.aspect_class }}" {# … #}> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Picture | ||
|
||
You will use a `<picture>` element instead of an `<img>` | ||
|
||
- If you want to use **modern image formats like WebP** and provide fallbacks for browsers that don’t support it yet. | ||
- If you want to use **art direction** and provide different images for differents screens. (Using `<img>` is for serving differently-sized version of the *same image*.) | ||
|
||
## WebP | ||
|
||
### Simple responsive picture | ||
|
||
```html | ||
<picture> | ||
<source srcset="burrito-720x0-default.webp 720w, burrito-1200x0-default.webp 1200w" sizes="(min-width: 1200px) 1200px, 100vw" type="image/webp"> | ||
<source srcset="burrito-720x0-default.jpg 720w, burrito-1200x0-default.jpg 1200w" sizes="(min-width: 1200px) 1200px, 100vw"> | ||
|
||
<img src="burrito-1200x0-default.jpg" width="1200" height="600" alt="Your alt text"> | ||
</picture> | ||
``` | ||
|
||
**Twig** | ||
|
||
```twig | ||
<picture> | ||
{{ post.thumbnail|get_timber_picture_responsive('webp-picture') }} | ||
</picture> | ||
``` | ||
|
||
If you want to enable WebP conversion for all image sizes, you can add it to your image sizes with the `timmy/sizes` filter. | ||
|
||
```php | ||
add_filter( 'timmy/sizes', function( $sizes ) { | ||
$sizes = array_map( function( $size ) { | ||
if ( ! isset( $size['to_webp'] ) ) { | ||
$size['to_webp'] = true; | ||
} | ||
|
||
return $size; | ||
}, $sizes ); | ||
|
||
return $sizes; | ||
}, 50 ); | ||
``` | ||
|
||
### Art directed picture with fallbacks | ||
|
||
To be implemented … | ||
|
||
```html | ||
<picture> | ||
<source media="(min-width: 56.25em)" srcset="large.webp" type="image/webp"> | ||
<source media="(min-width: 56.25em)" srcset="large.jpg"> | ||
|
||
<source media="(min-width: 37.5em)" srcset="medium.webp" type="image/webp"> | ||
<source media="(min-width: 37.5em)" srcset="medium.jpg"> | ||
|
||
<source srcset="small.webp" type="image/webp"> | ||
<source srcset="small.jpg"> | ||
<img src="fallback.jpg" alt=""> | ||
</picture> | ||
``` | ||
|
||
**Twig** | ||
|
||
```twig | ||
<picture> | ||
{{ get_timber_picture_sources('webp-picture', [ | ||
{ | ||
media: '(min-width: 62em)' | ||
srcset | ||
} | ||
]) }} | ||
</picture> | ||
``` | ||
|
||
**Timmy config** | ||
|
||
```php | ||
[ | ||
'webp-picture' => [ | ||
'type' => 'picture', | ||
'towebp' => true, | ||
// This will be used | ||
'resize' => '', | ||
'sources' => [ | ||
'desktop' => [ | ||
|
||
], | ||
'default' => [ | ||
|
||
], | ||
'fallback' => [ | ||
|
||
], | ||
], | ||
], | ||
], | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters