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
|
<?php
define('FIFU_FLICKR_SIZES', serialize(array('s', 't', 'q', 'm', 'n', '-', 'z', 'c', 'b')));
function fifu_is_valid_flickr_size($size) { foreach (unserialize(FIFU_FLICKR_SIZES) as $i) if ($size == $i) return true; return false; }
function is_from_flickr($url) { return strpos($url, "staticflickr.com") !== false; }
function fifu_resize_flickr_image($size, $url) { if (!fifu_is_valid_flickr_size($size)) $size = fifu_flickr_size_auto($size, 0);
if ($size == '-') $size = '';
$pattern = '/((_[\w])+)*\.[\w]+$/'; $extension = null; preg_match($pattern, $url, $aux); if ($aux) $extension = explode('.', $aux[0])[1];
$replacement = $size . '.';
if ($size) $replacement = '_' . $replacement;
if ($extension) return preg_replace($pattern, $replacement, $url) . $extension; return preg_replace($pattern, $replacement, $url); }
function fifu_flickr_size_auto($width, $height) { $longest = $width >= $height ? $width : $height;
if ($width == $height) { if ($longest <= 75) return 's'; if ($longest <= 150) return 'q'; }
if ($longest <= 100) return 't'; if ($longest <= 240) return 'm';
$longest *= 0.9;
if ($longest <= 320) return 'n'; if ($longest <= 500) return '-'; if ($longest <= 640) return 'z'; if ($longest <= 800) return 'c'; return 'b'; }
add_filter('woocommerce_single_product_image_thumbnail_html', 'fifu_woo_flickr_replace', 10, 2);
function fifu_woo_flickr_replace($html, $post_thumbnail_id) { $width = fifu_get_img_width_from_html($html); $aux = explode(',', get_option('fifu_flickr_prod')); $maxSize = end($aux); $width = $maxSize > $width ? $width : $maxSize;
$url = wp_get_attachment_url($post_thumbnail_id); if (is_from_flickr($url) && $width) { $size = fifu_flickr_size_auto($width, 0); if ($size) { $new_url = fifu_resize_flickr_image($size, $url); $html = str_replace($url, $new_url, $html); } } $url = fifu_get_data_large_from_html($html); if (is_from_flickr($url)) { $size = fifu_flickr_size_auto($width, 0); if ($size) { $new_url = fifu_resize_flickr_image($size, $url); $html = str_replace('data-large_image="' . $url, 'data-large_image="' . $new_url, $html); } } return $html; }
function fifu_flickr_replace($html, $post_thumbnail_id) { $width = fifu_get_img_width_from_html($html); $url = fifu_get_src_from_html($html); if (is_from_flickr($url) && $width) { $size = fifu_flickr_size_auto($width, 0); if ($size) { $new_url = fifu_resize_flickr_image($size, $url); $html = str_replace($url, $new_url, $html); } } return $html; }
function fifu_flickr_get_srcset($url, $maxWidth) { $srcset = fifu_is_on('fifu_lazy') ? 'data-srcset="' : 'srcset="'; $count = 0; $arr_sizes_int = explode(',', fifu_flickr_thumbnail_sizes()); foreach (fifu_flickr_convert_to_char($arr_sizes_int) as $i) { $srcset .= (($count != 0) ? ', ' : '') . fifu_resize_flickr_image($i, $url) . ' ' . $arr_sizes_int[$count] . 'w'; if ($maxWidth && $arr_sizes_int[$count] >= $maxWidth) break; $count++; } return $srcset . '"'; }
function fifu_flickr_thumbnail_sizes() { if (is_home()) return get_option('fifu_flickr_home');
if (class_exists('WooCommerce') && is_shop()) return get_option('fifu_flickr_shop');
if (class_exists('WooCommerce') && is_product_category()) return get_option('fifu_flickr_ctgr');
if (is_singular('post') || is_author() || is_search()) return get_option('fifu_flickr_post');
if (is_singular('page')) return class_exists('WooCommerce') && is_cart() ? get_option('fifu_flickr_cart') : get_option('fifu_flickr_page');
if (is_singular('product')) return get_option('fifu_flickr_prod');
if (is_archive()) return get_option('fifu_flickr_arch'); }
function fifu_flickr_convert_to_char($arr_sizes_int) { $aux = ''; $count = 0; foreach ($arr_sizes_int as $i) { if ($count++ > 0) $aux .= ',';
switch ($i) { case '75': $aux .= 's'; break; case '100': $aux .= 't'; break; case '150': $aux .= 'q'; break; case '240': $aux .= 'm'; break; case '320': $aux .= 'n'; break; case '500': $aux .= '-'; break; case '640': $aux .= 'z'; break; case '800': $aux .= 'c'; break; case '1024': $aux .= 'b'; break; default: $aux .= 'b'; } } return explode(',', $aux); }
|