1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class to calculate thumb dimensions for a gallery. The default gallery templates |
4
|
|
|
* require width and height attributes on the thumb img tags. In some cases these need to be |
5
|
|
|
* calculated based on the aspect ratio of the individual thumbs. |
6
|
|
|
* |
7
|
|
|
* Date: 21/03/2017 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) { |
12
|
|
|
|
13
|
|
|
class FooGallery_Thumbnail_Dimensions |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
function __construct() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
//hook into the filter that build up the img attributes |
18
|
|
|
// and add the width and height attributes if the gallery requires them |
19
|
|
|
add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 ); |
20
|
|
|
|
21
|
|
|
//calculate the thumbnail dimensions for all attachments in the gallery |
22
|
|
|
// for the specific gallery template that is being used. |
23
|
|
|
add_action( 'foogallery_located_template', array( $this, 'calculate_all_thumbnail_dimensions' ) ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Helper function to check if the gallery requires thumbnail dimensions |
28
|
|
|
* @param $gallery_template string |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
function does_gallery_template_use_thumbnail_dimensions( $gallery_template ) { |
|
|
|
|
32
|
|
|
if ( empty( $gallery_template ) ) return false; |
33
|
|
|
|
34
|
|
|
//first do a check if the template needs thumbnail dimensions calculated |
35
|
|
|
$template_data = foogallery_get_gallery_template( $gallery_template ); |
36
|
|
|
|
37
|
|
|
if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) { |
38
|
|
|
//this template requires thumb dimensions to be provided |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function empty_dimensions( $thumbnail_dimensions ) { |
|
|
|
|
46
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
47
|
|
|
$thumb_width = (int)$thumbnail_dimensions['width']; |
48
|
|
|
$thumb_height = (int)$thumbnail_dimensions['height']; |
49
|
|
|
|
50
|
|
|
return $thumb_width === 0 && $thumb_height === 0; |
51
|
|
|
} |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Calculates all the thumbnail dimensions for the gallery |
57
|
|
|
* @param $foogallery FooGallery |
58
|
|
|
*/ |
59
|
|
|
function calculate_all_thumbnail_dimensions( $foogallery ) { |
|
|
|
|
60
|
|
|
global $current_foogallery; |
|
|
|
|
61
|
|
|
global $current_foogallery_template; |
62
|
|
|
global $current_foogallery_arguments; |
63
|
|
|
|
64
|
|
|
//check if we are dealing with a gallery. This check ensures this is not done for albums |
65
|
|
|
if ( isset( $current_foogallery ) && isset( $current_foogallery_template ) ) { |
66
|
|
|
|
67
|
|
|
//first do a check if the template needs thumbnail dimensions calculated |
68
|
|
|
if ($this->does_gallery_template_use_thumbnail_dimensions( $current_foogallery_template ) ) { |
69
|
|
|
|
70
|
|
|
//load the thumbnail dimensions specific to the gallery, taking preference to arguments |
71
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_template, array(), $current_foogallery_arguments ); |
72
|
|
|
|
73
|
|
|
//if we have no dimensions then load them from the gallery settings |
74
|
|
|
if ( $this->empty_dimensions( $thumbnail_dimensions ) ) { |
75
|
|
|
$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $current_foogallery_template, $thumbnail_dimensions, $current_foogallery ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) { |
79
|
|
|
|
80
|
|
|
//$thumbnail_dimensions |
81
|
|
|
$thumb_width = (int)$thumbnail_dimensions['width']; |
82
|
|
|
$thumb_height = (int)$thumbnail_dimensions['height']; |
83
|
|
|
$thumb_crop = (bool)$thumbnail_dimensions['crop']; |
84
|
|
|
|
85
|
|
|
//set the appropriate arguments on the attachments so that they can be |
86
|
|
|
// picked up and used in the 'include_thumb_dimension_attributes' function below |
87
|
|
|
foreach ($foogallery->attachments() as $attachment) { |
88
|
|
|
if ( $thumb_crop && $thumb_width > 0 && $thumb_height > 0 ) { |
89
|
|
|
//we have set width and height and crop = true |
90
|
|
|
//we do not need to calculate the dimensions |
91
|
|
|
$calculated_thumb_width = $thumb_width; |
92
|
|
|
$calculated_thumb_height = $thumb_height; |
93
|
|
|
} else { |
94
|
|
|
$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop ); |
95
|
|
|
if ( false !== $size_array ) { |
96
|
|
|
$calculated_thumb_width = $size_array[4]; |
97
|
|
|
$calculated_thumb_height = $size_array[5]; |
98
|
|
|
} else { |
99
|
|
|
$size_array = $this->calculate_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height ); |
100
|
|
|
if ( false !== $size_array ) { |
101
|
|
|
$calculated_thumb_width = $size_array[0]; |
102
|
|
|
$calculated_thumb_height = $size_array[1]; |
103
|
|
|
} else { |
104
|
|
|
//fallback for when we cannot calculate dimensions. Use the originals |
105
|
|
|
$calculated_thumb_width = $attachment->width; |
106
|
|
|
$calculated_thumb_height = $attachment->height; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$attachment->has_thumbnail_dimensions = true; |
112
|
|
|
$attachment->thumb_width = $calculated_thumb_width; |
113
|
|
|
$attachment->thumb_height = $calculated_thumb_height; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a resized width and height value given the original dimensions and then either the new width or height value, one can be 0. |
122
|
|
|
* |
123
|
|
|
* @param {int} $orig_width - The original width of the image. |
|
|
|
|
124
|
|
|
* @param {int} $orig_height - The original height of the image. |
|
|
|
|
125
|
|
|
* @param {int} $new_width - The new width for the image or 0 to calculate it from the supplied new height and original dimensions. |
|
|
|
|
126
|
|
|
* @param {int} $new_height - The new height for the image or 0 to calculate it from the supplied new width and original dimensions. |
|
|
|
|
127
|
|
|
* |
128
|
|
|
* @return array|false - Returns an array with the width value at index 0 and the height value at index 1. |
129
|
|
|
* Returns false if the original dimensions are invalid or if both the new width and height are invalid. |
130
|
|
|
* |
131
|
|
|
* @example |
132
|
|
|
* |
133
|
|
|
* $size_1 = calculate_dimensions(800, 600, 0, 300); // => [400, 300] |
134
|
|
|
* |
135
|
|
|
* $size_2 = calculate_dimensions(800, 600, 400, 0); // => [400, 300] |
136
|
|
|
*/ |
137
|
|
|
function calculate_dimensions($orig_width, $orig_height, $new_width, $new_height){ |
|
|
|
|
138
|
|
|
// if we have an invalid original size provided exit early and return false |
139
|
|
|
if (!is_numeric($orig_width) || $orig_width === 0 || !is_numeric($orig_height) || $orig_height === 0){ |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
$needs_width = !is_numeric($new_width) || $new_width === 0; |
143
|
|
|
$needs_height = !is_numeric($new_height) || $new_height === 0; |
144
|
|
|
// if we have an invalid new size provided i.e. both new values are not numbers or both are 0 then exit early and return false |
145
|
|
|
if ($needs_width && $needs_height){ |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
// otherwise get the ratio of the original so we can calculate any missing new values |
149
|
|
|
$ratio = $orig_width / $orig_height; |
150
|
|
|
if ($needs_width){ |
151
|
|
|
$new_width = $new_height * $ratio; |
152
|
|
|
} |
153
|
|
|
if ($needs_height){ |
154
|
|
|
$new_height = $new_width / $ratio; |
155
|
|
|
} |
156
|
|
|
return array($new_width, $new_height); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Include the thumb dimension html attributes in the rendered HTML |
161
|
|
|
* |
162
|
|
|
* @param $attr |
163
|
|
|
* @param $args |
164
|
|
|
* @param $foogallery_attachment |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) { |
|
|
|
|
169
|
|
|
global $current_foogallery; |
|
|
|
|
170
|
|
|
|
171
|
|
|
//check if we are dealing with a gallery. This check ensures this is not done for albums |
172
|
|
|
if ( isset( $current_foogallery ) ) { |
173
|
|
|
|
174
|
|
|
//check if we have anything set |
175
|
|
|
if ( isset( $foogallery_attachment->has_thumbnail_dimensions ) ) { |
176
|
|
|
if ( $foogallery_attachment->thumb_width > 0 ) { |
177
|
|
|
$attr['width'] = $foogallery_attachment->thumb_width; |
178
|
|
|
} |
179
|
|
|
if ( $foogallery_attachment->thumb_height > 0 ) { |
180
|
|
|
$attr['height'] = $foogallery_attachment->thumb_height; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $attr; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.