PluginProbe ʕ •ᴥ•ʔ
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel / trunk
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel vtrunk
trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-thumbnail-dimensions.php
foogallery / includes Last commit date
admin 7 months ago compatibility 7 months ago extensions 7 months ago foopluginbase 7 months ago public 7 months ago thumbs 7 months ago .DS_Store 7 months ago class-attachment-filters.php 7 months ago class-foogallery-animated-gif-support.php 7 months ago class-foogallery-attachment-custom-class.php 7 months ago class-foogallery-attachment-type.php 7 months ago class-foogallery-attachment.php 7 months ago class-foogallery-cache.php 7 months ago class-foogallery-common-fields.php 7 months ago class-foogallery-crop-position.php 7 months ago class-foogallery-datasource-media_library.php 7 months ago class-foogallery-debug.php 7 months ago class-foogallery-extensions-compatibility.php 7 months ago class-foogallery-force-https.php 7 months ago class-foogallery-lazyload.php 7 months ago class-foogallery-lightbox.php 7 months ago class-foogallery-paging.php 7 months ago class-foogallery-password-protect.php 7 months ago class-foogallery-sitemaps.php 7 months ago class-foogallery-widget.php 7 months ago class-foogallery.php 7 months ago class-gallery-advanced-settings.php 7 months ago class-il8n.php 7 months ago class-override-thumbnail.php 7 months ago class-posttypes.php 7 months ago class-retina.php 7 months ago class-thumbnail-dimensions.php 7 months ago class-thumbnails.php 7 months ago class-version-check.php 7 months ago constants.php 7 months ago functions.php 7 months ago includes.php 7 months ago index.php 11 years ago render-functions.php 7 months ago
class-thumbnail-dimensions.php
198 lines
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 //hook into the filter that build up the img attributes
17 // and add the width and height attributes if the gallery requires them
18 add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 );
19
20 //calculate the thumbnail dimensions for all attachments in the gallery
21 // for the specific gallery template that is being used.
22 add_action( 'foogallery_located_template', array( $this, 'calculate_all_thumbnail_dimensions' ) );
23 }
24
25 /**
26 * Helper function to check if the gallery requires thumbnail dimensions
27 * @param $gallery_template string
28 * @return bool
29 */
30 function does_gallery_template_use_thumbnail_dimensions( $gallery_template ) {
31 if ( empty( $gallery_template ) ) return false;
32
33 //first do a check if the template needs thumbnail dimensions calculated
34 $template_data = foogallery_get_gallery_template( $gallery_template );
35
36 if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
37 //this template requires thumb dimensions to be provided
38 return true;
39 }
40
41 return false;
42 }
43
44 function empty_dimensions( $thumbnail_dimensions ) {
45 if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
46 $thumb_width = (int)$thumbnail_dimensions['width'];
47 $thumb_height = (int)$thumbnail_dimensions['height'];
48
49 return $thumb_width === 0 && $thumb_height === 0;
50 }
51 return true;
52 }
53
54 /**
55 * Calculates all the thumbnail dimensions for the gallery
56 * @param $foogallery FooGallery
57 */
58 function calculate_all_thumbnail_dimensions( $foogallery ) {
59 global $current_foogallery;
60 global $current_foogallery_template;
61 global $current_foogallery_arguments;
62
63 //check if we are dealing with a gallery. This check ensures this is not done for albums
64 if ( isset( $current_foogallery ) && isset( $current_foogallery_template ) ) {
65
66 //first do a check if the template needs thumbnail dimensions calculated
67 if ($this->does_gallery_template_use_thumbnail_dimensions( $current_foogallery_template ) ) {
68
69 //load the thumbnail dimensions specific to the gallery, taking preference to arguments
70 $thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_template, array(), $current_foogallery_arguments );
71
72 //if we have no dimensions then load them from the gallery settings
73 if ( $this->empty_dimensions( $thumbnail_dimensions ) ) {
74 $thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $current_foogallery_template, $thumbnail_dimensions, $current_foogallery );
75 }
76
77 if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
78
79 //$thumbnail_dimensions
80 $thumb_width = (int)$thumbnail_dimensions['width'];
81 $thumb_height = (int)$thumbnail_dimensions['height'];
82 $thumb_crop = (bool)$thumbnail_dimensions['crop'];
83
84 //allow width and height arguments to be overridden
85 $override_width = foogallery_gallery_template_setting( 'override_width', false );
86 if ( $override_width !== false && intval( $override_width ) > 0 ) {
87 $thumb_width = intval( $override_width );
88 }
89 $override_height = foogallery_gallery_template_setting( 'override_height', false );
90 if ( $override_height !== false && intval( $override_height ) > 0 ) {
91 $thumb_height = intval( $override_height );
92 }
93
94 //set the appropriate arguments on the attachments so that they can be
95 // picked up and used in the 'include_thumb_dimension_attributes' function below
96 foreach ($foogallery->attachments() as $attachment) {
97 if ( $thumb_crop && $thumb_width > 0 && $thumb_height > 0 ) {
98 //we have set width and height and crop = true
99 //we do not need to calculate the dimensions
100 $calculated_thumb_width = $thumb_width;
101 $calculated_thumb_height = $thumb_height;
102 } else {
103 $size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop );
104 if ( false !== $size_array ) {
105 $calculated_thumb_width = $size_array[4];
106 $calculated_thumb_height = $size_array[5];
107 } else {
108 $size_array = $this->calculate_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height );
109 if ( false !== $size_array ) {
110 $calculated_thumb_width = $size_array[0];
111 $calculated_thumb_height = $size_array[1];
112 } else {
113 //fallback for when we cannot calculate dimensions. Use the originals
114 $calculated_thumb_width = $attachment->width;
115 $calculated_thumb_height = $attachment->height;
116 }
117 }
118 }
119
120 $attachment->has_thumbnail_dimensions = true;
121 $attachment->thumb_width = $calculated_thumb_width;
122 $attachment->thumb_height = $calculated_thumb_height;
123 }
124 }
125 }
126 }
127 }
128
129 /**
130 * Returns a resized width and height value given the original dimensions and then either the new width or height value, one can be 0.
131 *
132 * @param {int} $orig_width - The original width of the image.
133 * @param {int} $orig_height - The original height of the image.
134 * @param {int} $new_width - The new width for the image or 0 to calculate it from the supplied new height and original dimensions.
135 * @param {int} $new_height - The new height for the image or 0 to calculate it from the supplied new width and original dimensions.
136 *
137 * @return array|false - Returns an array with the width value at index 0 and the height value at index 1.
138 * Returns false if the original dimensions are invalid or if both the new width and height are invalid.
139 *
140 * @example
141 *
142 * $size_1 = calculate_dimensions(800, 600, 0, 300); // => [400, 300]
143 *
144 * $size_2 = calculate_dimensions(800, 600, 400, 0); // => [400, 300]
145 */
146 function calculate_dimensions($orig_width, $orig_height, $new_width, $new_height){
147 // if we have an invalid original size provided exit early and return false
148 if (!is_numeric($orig_width) || $orig_width === 0 || !is_numeric($orig_height) || $orig_height === 0){
149 return false;
150 }
151 $needs_width = !is_numeric($new_width) || $new_width === 0;
152 $needs_height = !is_numeric($new_height) || $new_height === 0;
153 // 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
154 if ($needs_width && $needs_height){
155 return false;
156 }
157 // otherwise get the ratio of the original so we can calculate any missing new values
158 $ratio = $orig_width / $orig_height;
159 if ($needs_width){
160 $new_width = $new_height * $ratio;
161 }
162 if ($needs_height){
163 $new_height = $new_width / $ratio;
164 }
165 return array($new_width, $new_height);
166 }
167
168 /**
169 * Include the thumb dimension html attributes in the rendered HTML
170 *
171 * @param $attr
172 * @param $args
173 * @param $foogallery_attachment
174 *
175 * @return array
176 */
177 function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) {
178 global $current_foogallery;
179
180 //check if we are dealing with a gallery. This check ensures this is not done for albums
181 if ( isset( $current_foogallery ) ) {
182
183 //check if we have anything set
184 if ( isset( $foogallery_attachment->has_thumbnail_dimensions ) ) {
185 if ( $foogallery_attachment->thumb_width > 0 ) {
186 $attr['width'] = $foogallery_attachment->thumb_width;
187 }
188 if ( $foogallery_attachment->thumb_height > 0 ) {
189 $attr['height'] = $foogallery_attachment->thumb_height;
190 }
191 }
192 }
193
194 return $attr;
195 }
196
197 }
198 }