PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
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-thumbnails.php
foogallery / includes Last commit date
abilities 2 months ago admin 2 months ago compatibility 2 months ago extensions 2 months ago foopluginbase 2 months ago public 2 months ago thumbs 2 months ago asset-manifest.php 2 months ago class-attachment-filters.php 2 months ago class-command-palette.php 2 months ago class-foogallery-animated-gif-support.php 2 months ago class-foogallery-attachment-custom-class.php 2 months ago class-foogallery-attachment-filename.php 2 months ago class-foogallery-attachment-type.php 2 months ago class-foogallery-attachment.php 2 months ago class-foogallery-cache.php 2 months ago class-foogallery-common-fields.php 2 months ago class-foogallery-crop-position.php 2 months ago class-foogallery-datasource-media_library.php 2 months ago class-foogallery-debug.php 2 months ago class-foogallery-delayed-runtime-loader.php 2 months ago class-foogallery-extensions-compatibility.php 2 months ago class-foogallery-force-https.php 2 months ago class-foogallery-lazyload.php 2 months ago class-foogallery-license-constant-handler.php 2 months ago class-foogallery-lightbox.php 2 months ago class-foogallery-paging.php 2 months ago class-foogallery-password-protect.php 2 months ago class-foogallery-sitemaps.php 2 months ago class-foogallery-widget.php 2 months ago class-foogallery.php 2 months ago class-gallery-advanced-settings.php 2 months ago class-il8n.php 2 months ago class-override-thumbnail.php 2 months ago class-posttypes.php 2 months ago class-previews.php 2 months ago class-retina.php 2 months ago class-thumbnail-dimensions.php 2 months ago class-thumbnails.php 2 months ago class-version-check.php 2 months ago constants.php 2 months ago functions.php 2 months ago includes.php 2 months ago index.php 2 months ago render-functions.php 2 months ago
class-thumbnails.php
324 lines
1 <?php
2 /*
3 * FooGallery Thumbnail Resizing class
4 */
5
6 if ( !class_exists( 'FooGallery_Thumbnails' ) ) {
7
8 class FooGallery_Thumbnails {
9
10 function __construct() {
11 add_filter( 'foogallery_attachment_resize_thumbnail', array( $this, 'resize' ), 10, 3 );
12
13 add_filter( 'foogallery_test_thumb_url', array( $this, 'override_test_thumb_url' ) );
14
15 add_filter( 'foogallery_thumbnail_resize_args', array( $this, 'check_for_force_original_thumb') );
16 }
17
18 function check_for_force_original_thumb( $args ){
19 global $current_foogallery;
20
21 if ( isset( $current_foogallery ) ) {
22 $args['force_use_original_thumb'] = $current_foogallery->force_use_original_thumbs;
23 }
24
25 return $args;
26 }
27
28 function resize( $original_image_src, $args, $thumbnail_object ) {
29 global $current_foogallery;
30 global $foogallery_last_generated_thumb_url;
31
32 $arg_defaults = array(
33 'width' => 0,
34 'height' => 0,
35 'crop' => true,
36 'jpeg_quality' => foogallery_thumbnail_jpeg_quality(),
37 'thumb_resize_animations' => true,
38 'foogallery_attachment_id'=> $thumbnail_object->ID
39 );
40
41 if ( isset( $current_foogallery ) ) {
42 $arg_defaults['foogallery_id'] = $current_foogallery->ID;
43 }
44
45 $args = wp_parse_args( $args, $arg_defaults );
46
47 //allow for plugins to change the thumbnail creation args
48 $args = apply_filters( 'foogallery_thumbnail_resize_args', $args, $original_image_src, $thumbnail_object );
49
50 //check the current arguments passed in by the shortcode
51 global $current_foogallery_arguments;
52 if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) {
53 $thumbnail_args = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], $args, $current_foogallery_arguments );
54 $args = wp_parse_args( $thumbnail_args, $args );
55 }
56
57 //allow for plugins to change the thumbnail creation args one final time
58 $args = apply_filters( 'foogallery_thumbnail_resize_args_final', $args, $original_image_src, $thumbnail_object );
59
60 $width = (int)$args['width'];
61 $height = (int)$args['height'];
62 $crop = (bool)$args['crop'];
63
64 if ( 0 === $width && 0 === $height ) {
65 return $original_image_src;
66 }
67
68 //we can force the use of the originally uploaded full-size image
69 $force_use_original_image = isset( $args['force_use_original_image'] ) && true === $args['force_use_original_image'];
70
71 if ( $thumbnail_object->ID > 0 && $force_use_original_image ) {
72 $fullsize = wp_get_attachment_image_src( $thumbnail_object->ID, 'fullsize' );
73
74 return $fullsize[0];
75 }
76
77 //we can force the use of the original WP icon or WP-generated thumb by passing through args individually
78 $force_use_original_thumb = isset( $args['force_use_original_thumb'] ) && true === $args['force_use_original_thumb'];
79
80 if ( $thumbnail_object->ID > 0 && $force_use_original_thumb ) {
81 $thumbnail_icon = wp_get_attachment_image_src( $thumbnail_object->ID, array( $width, $height ) );
82
83 return $thumbnail_icon[0];
84 }
85
86 //we can force the use of original WP thumbs by passing through args individually, or by saved settings
87 $use_original_thumbs = ( isset( $args['use_original_thumbs'] ) && true === $args['use_original_thumbs'] ) || 'on' === foogallery_get_setting( 'use_original_thumbs' );
88
89 if ( $use_original_thumbs ) {
90
91 $option_thumbnail_size_w = get_option( 'thumbnail_size_w' );
92 $option_thumbnail_size_h = get_option( 'thumbnail_size_h' );
93 $option_thumbnail_crop = get_option( 'thumbnail_crop' );
94
95 //check if we are trying to get back the default thumbnail that we already have
96 if ( $thumbnail_object->ID > 0 && $width == $option_thumbnail_size_w && $height == $option_thumbnail_size_h && $crop == $option_thumbnail_crop ) {
97 $thumbnail_attributes = wp_get_attachment_image_src( $thumbnail_object->ID );
98
99 return $thumbnail_attributes[0];
100 }
101 }
102
103 //remove invalid resize args
104 if ( array_key_exists( 'height', $args ) && 0 === $args['height'] ) {
105 unset( $args['height'] );
106 }
107
108 $force_resize = false;
109
110 //only worry about upscaling if we have supplied both a width and height for cropping
111 if ( array_key_exists( 'height', $args ) && $args['height'] > 0 &&
112 array_key_exists( 'width', $args ) && $args['width'] > 0 ) {
113 //check if we must upscale smaller images
114 if ( 'on' === foogallery_get_setting( 'thumb_resize_upscale_small' ) ) {
115 $force_resize = true;
116 if ( !isset( $args['background_fill'] ) ) {
117 $color = foogallery_get_setting( 'thumb_resize_upscale_small_color', '' );
118 if ( $color !== 'auto' && $color !== 'transparent' ) {
119 $colors = foogallery_rgb_to_color_array( $color );
120 $color = sprintf( "%03d%03d%03d000", $colors[0], $colors[1], $colors[2] );
121 }
122 $args['background_fill'] = $color;
123 }
124 }
125 }
126
127 //do some checks to see if the image is smaller
128 if ( $force_resize || $this->should_resize( $thumbnail_object, $args ) ) {
129 //save the generated thumb url to a global so that we can use it later if needed
130 $foogallery_last_generated_thumb_url = foogallery_thumb( $original_image_src, $args );
131 } else {
132 $foogallery_last_generated_thumb_url = apply_filters('foogallery_thumbnail_resize_small_image', $original_image_src, $args );
133 }
134
135 return $foogallery_last_generated_thumb_url;
136 }
137
138 function should_resize($thumbnail_object, $args) {
139 $original_width = $thumbnail_object->width;
140 $original_height = $thumbnail_object->height;
141
142 if ( $original_width === $original_height && $original_height === 0 ) {
143 //we do not have the original dimensions, so assume we must resize!
144 return true;
145 }
146
147 $new_width = isset( $args['width'] ) ? $args['width'] : 0;
148 $new_height = isset( $args['height'] ) ? $args['height'] : 0;
149
150 if ( $new_width > 0 && $new_height > 0 ) {
151 return $original_width > $new_width || $original_height > $new_height;
152 } else if ( $new_width > 0 ) {
153 return $original_width > $new_width;
154 }
155 return $original_height > $new_height;
156 }
157
158 function run_thumbnail_generation_tests() {
159 if ( !foogallery_thumb_active_engine()->requires_thumbnail_generation_tests() ) {
160 return array(
161 'success' => true
162 );
163 }
164
165 $test_image_url = foogallery_test_thumb_url();
166
167 //next, generate a thumbnail
168 $test_args = array(
169 'width' => 20,
170 'height' => 20,
171 'crop' => true,
172 'jpeg_quality' => foogallery_thumbnail_jpeg_quality()
173 );
174
175 //first, clear any previous cached files
176 $engine = foogallery_thumb_active_engine();
177 $engine->clear_local_cache_for_file( $test_image_url );
178
179 $generated_thumb = $engine->generate( $test_image_url, $test_args );
180
181 $success = $test_image_url !== $generated_thumb;
182 $file_info = wp_check_filetype( $test_image_url );
183
184 $test_results = array(
185 'success' => $success,
186 'thumb' => $generated_thumb,
187 'error' => $engine->get_last_error(),
188 'file_info' => $file_info
189 );
190
191 do_action( 'foogallery_thumbnail_generation_test', $test_results );
192
193 return $test_results;
194 }
195
196 function override_test_thumb_url( $test_thumb_url ) {
197 if ( 'on' !== foogallery_get_setting( 'override_thumb_test', false ) ) {
198 $image_url = $this->find_first_image_in_media_library();
199
200 if ( $image_url !== false ) {
201 return $image_url;
202 }
203 }
204
205 //if we get here, then either, we have set the override_thumb_test setting,
206 //or there are no good images to use from the media library
207 return $test_thumb_url;
208 }
209
210 static function find_first_image_in_media_library( $min_width = 50, $min_height = 50 ) {
211 static $cached = array();
212
213 $min_width = absint( $min_width );
214 $min_height = absint( $min_height );
215 $cache_key = $min_width . 'x' . $min_height;
216
217 if ( array_key_exists( $cache_key, $cached ) ) {
218 return $cached[ $cache_key ];
219 }
220
221 // Try a small set of recent images with minimal query overhead.
222 $args = array(
223 'post_type' => 'attachment',
224 'post_mime_type' => 'image',
225 'post_status' => 'inherit',
226 'posts_per_page' => 25,
227 'orderby' => 'date',
228 'order' => 'DESC',
229 'fields' => 'ids',
230 'no_found_rows' => true,
231 'update_post_meta_cache' => false,
232 'update_post_term_cache' => false,
233 );
234
235 $query_images = new WP_Query( $args );
236 if ( empty( $query_images->posts ) ) {
237 $cached[ $cache_key ] = false;
238 return $cached[ $cache_key ];
239 }
240 foreach ( $query_images->posts as $image_id ) {
241 $local_path = get_attached_file( $image_id );
242 $image_url = wp_get_attachment_url( $image_id );
243
244 if ( empty( $image_url ) || ! self::image_meets_minimum_test_dimensions( $image_id, $local_path, $min_width, $min_height ) ) {
245 continue;
246 }
247
248 if ( $local_path && file_exists( $local_path ) ) {
249 $cached[ $cache_key ] = $image_url;
250 return $cached[ $cache_key ];
251 }
252
253 if ( ! empty( $image_url ) ) {
254 if ( self::image_file_exists( $image_url ) || self::image_file_exists( $image_url, true ) ) {
255 $cached[ $cache_key ] = $image_url;
256 return $cached[ $cache_key ];
257 }
258 }
259 }
260
261 $cached[ $cache_key ] = false;
262 return $cached[ $cache_key ];
263 }
264
265 /**
266 * Checks whether an attachment is large enough to be useful for test pages.
267 *
268 * @param int $image_id Attachment ID.
269 * @param string|bool $local_path Local file path.
270 * @param int $min_width Minimum width.
271 * @param int $min_height Minimum height.
272 *
273 * @return bool
274 */
275 static function image_meets_minimum_test_dimensions( $image_id, $local_path, $min_width, $min_height ) {
276 if ( $min_width <= 0 && $min_height <= 0 ) {
277 return true;
278 }
279
280 $metadata = wp_get_attachment_metadata( $image_id );
281 if ( isset( $metadata['width'], $metadata['height'] ) ) {
282 return intval( $metadata['width'] ) >= $min_width && intval( $metadata['height'] ) >= $min_height;
283 }
284
285 if ( $local_path && file_exists( $local_path ) ) {
286 $size = function_exists( 'wp_getimagesize' ) ? wp_getimagesize( $local_path ) : @getimagesize( $local_path );
287
288 if ( isset( $size[0], $size[1] ) ) {
289 return intval( $size[0] ) >= $min_width && intval( $size[1] ) >= $min_height;
290 }
291 }
292
293 return true;
294 }
295
296 /**
297 * Check if a remote image file exists.
298 *
299 * @param string $url The url to the remote image.
300 * @param bool $force_https Whether to force the url to be https.
301 *
302 * @return bool Whether the remote image exists.
303 */
304 static function image_file_exists( $url, $force_https = false ) {
305 if ( $force_https ) {
306 $url = str_replace( 'http://', 'https://', $url );
307 }
308
309 $response = wp_remote_head( $url, array(
310 'timeout' => 2,
311 'redirection' => 0,
312 'reject_unsafe_urls' => true,
313 ) );
314
315 if ( is_wp_error( $response ) ) {
316 return false;
317 }
318
319 $code = wp_remote_retrieve_response_code( $response );
320 return ( $code >= 200 && $code < 400 ) || 403 === $code;
321 }
322 }
323 }
324