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