-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimberPhoton.php
196 lines (156 loc) · 5.32 KB
/
TimberPhoton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/*
Plugin Name: Timber with Jetpack Photon
Plugin URI: http://slimndap.com
Description: Make the Timber plugin work with Jetpack's Photon. Once installed, all TimberImages will use Photon as a CDN and for image manipulation (eg. resize).
Author: Jeroen Schmit
Version: 0.4
Author URI: http://slimndap.com
*/
class TimberPhoton {
function __construct() {
$this->admin_notices = array();
$this->photon_hosts = array(
'i0.wp.com',
'i1.wp.com',
'i2.wp.com'
);
add_action('plugins_loaded',array($this,'plugins_loaded'));
}
function twig_apply_filters($twig) {
$twig->addFilter('resize', new Twig_Filter_Function(array($this, 'resize')));
$twig->addFilter('letterbox', new Twig_Filter_Function(array($this, 'letterbox')));
return $twig;
}
function admin_notices() {
if (!empty($this->admin_notices)) {
echo '<div class="error"><p>';
if (in_array('timber', $this->admin_notices)) {
_e('Timber with Jetpack Photon requires the Timber plugin to be installed and activated. <a href="http://jarednova.github.io/timber/">Get it here</a>.');
}
if (in_array('photon', $this->admin_notices)) {
_e('Timber with Jetpack Photon requires the Jetpack plugin to be installed with Photon activated.');
}
echo '</p></div>';
}
}
function letterbox($src, $w, $h, $color = '#000000', $force = false) {
/*
* Translate the URL.
* Only necessary for Timber versions (0.18.0 and older) that lack the 'timber_image_src' filter.
*/
$src = $this->photon_url($src);
/* Apply letterbox
* Photon docs: Add black letterboxing effect to images, by scaling them to width, height
* while maintaining the aspect ratio and filling the rest with black.
* See: http://developer.wordpress.com/docs/photon/api/#lb
*/
$args = array(
'lb' => $w.','.$h
);
$src = add_query_arg($args, $src);
return $src;
}
function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false ) {
if (empty($src)){
return '';
}
/*
* Translate the URL.
* Only necessary for Timber versions (0.18.0 and older) that lack the 'timber_image_src' filter.
*/
$src = $this->photon_url($src);
/* Set width
* Photon docs: Set the width of an image. Defaults to pixels, supports percentages.
* See: http://developer.wordpress.com/docs/photon/api/#w
*/
$args = array(
'w' => $w
);
/* Use resize if height is set
* Photon docs: Resize and crop an image to exact width,height pixel dimensions.
* Set the first number as close to the target size as possible and then crop the rest.
* Which direction it’s resized and cropped depends on the aspect ratios of the original image and the target size.
* See: http://developer.wordpress.com/docs/photon/api/#resize
*/
if (!empty($h)) {
$args['resize'] = $w.','.$h;
unset ($args['w']);
}
$src = add_query_arg($args, $src);
return $src;
}
/**
* Activates hooks that add Photon support to Timber.
*
* @since 0.1
* @since 0.4 Added support for the renamed 'timber/twig/filters' in Timber.
*
* @return void
*/
function plugins_loaded() {
if ($this->system_ready()) {
add_action('timber/twig/filters', array(&$this, 'twig_apply_filters'), 99);
add_action('twig_apply_filters', array(&$this, 'twig_apply_filters'), 99);
add_filter('timber_image_src', array($this, 'timber_image_src'));
}
}
/*
* Translate a URL to a Photon URL.
* Photon docs: http://i0.wp.com/$REMOTE_IMAGE_URL
*/
function photon_url($url) {
if ($parsed = parse_url($url)) {
if (in_array($parsed['host'], $this->photon_hosts)) {
// $url is already a Photon URL.
// Leave it alone.
} else {
// Strip http:// from $url.
$stripped_url = $parsed['host'].$parsed['path'];
$args = array();
if (!empty($parsed['query'])) {
$args = parse_str($parsed['query']);
}
$args = apply_filters('jetpack_photon_pre_args', $args, $url, $parsed['scheme']);
if (!empty($args)) {
$stripped_url.= '?'.http_build_query( $args );
}
/*
* Pick a Photon host based on the crc32 of the stripped_url.
* Photon docs: Multiple domains. In order to take advantage of parallel downloads
* we support multiple sub-domains for Photon. If you tend to have many images per
* page you can split them across i0.wp.com, i1.wp.com, and i2.wp.com.
*/
$photon_host = $this->photon_hosts[abs(crc32($stripped_url) % 2)];
// Create a Photon URL.
$url = $parsed['scheme'].'://'.$photon_host.'/'.$stripped_url;
}
}
return $url;
}
/*
* Check if Timber and Jetpack are installed and activated.
* Check if Photon is activated
*/
function system_ready() {
global $timber;
// Is Timber installed and activated?
if (!class_exists('Timber')) {
$this->admin_notices[] = 'timber';
add_action( 'admin_notices', array($this,'admin_notices'));
return false;
}
// Determine if Jetpack is installed and can generate photon URLs.
if (!class_exists( 'Jetpack' ) || !method_exists( 'Jetpack', 'get_active_modules' ) || !in_array( 'photon', Jetpack::get_active_modules() )) {
$this->admin_notices[] = 'photon';
add_action( 'admin_notices', array($this,'admin_notices'));
return false;
}
return true;
}
function timber_image_src($src) {
return $this->photon_url($src);
}
}
new TimberPhoton();
?>