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-thumbnails.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-thumbnails.php
249 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 $color = foogallery_get_setting( 'thumb_resize_upscale_small_color', '' );
117 if ( $color !== 'auto' && $color !== 'transparent' ) {
118 $colors = foogallery_rgb_to_color_array( $color );
119 $color = sprintf( "%03d%03d%03d000", $colors[0], $colors[1], $colors[2] );
120 }
121 $args['background_fill'] = $color;
122 }
123 }
124
125 //do some checks to see if the image is smaller
126 if ( $force_resize || $this->should_resize( $thumbnail_object, $args ) ) {
127 //save the generated thumb url to a global so that we can use it later if needed
128 $foogallery_last_generated_thumb_url = foogallery_thumb( $original_image_src, $args );
129 } else {
130 $foogallery_last_generated_thumb_url = apply_filters('foogallery_thumbnail_resize_small_image', $original_image_src, $args );
131 }
132
133 return $foogallery_last_generated_thumb_url;
134 }
135
136 function should_resize($thumbnail_object, $args) {
137 $original_width = $thumbnail_object->width;
138 $original_height = $thumbnail_object->height;
139
140 if ( $original_width === $original_height && $original_height === 0 ) {
141 //we do not have the original dimensions, so assume we must resize!
142 return true;
143 }
144
145 $new_width = isset( $args['width'] ) ? $args['width'] : 0;
146 $new_height = isset( $args['height'] ) ? $args['height'] : 0;
147
148 if ( $new_width > 0 && $new_height > 0 ) {
149 return $original_width > $new_width || $original_height > $new_height;
150 } else if ( $new_width > 0 ) {
151 return $original_width > $new_width;
152 }
153 return $original_height > $new_height;
154 }
155
156 function run_thumbnail_generation_tests() {
157 if ( !foogallery_thumb_active_engine()->requires_thumbnail_generation_tests() ) {
158 return array(
159 'success' => true
160 );
161 }
162
163 $test_image_url = foogallery_test_thumb_url();
164
165 //next, generate a thumbnail
166 $test_args = array(
167 'width' => 20,
168 'height' => 20,
169 'crop' => true,
170 'jpeg_quality' => foogallery_thumbnail_jpeg_quality()
171 );
172
173 //first, clear any previous cached files
174 $engine = foogallery_thumb_active_engine();
175 $engine->clear_local_cache_for_file( $test_image_url );
176
177 $generated_thumb = $engine->generate( $test_image_url, $test_args );
178
179 $success = $test_image_url !== $generated_thumb;
180 $file_info = wp_check_filetype( $test_image_url );
181
182 $test_results = array(
183 'success' => $success,
184 'thumb' => $generated_thumb,
185 'error' => $engine->get_last_error(),
186 'file_info' => $file_info
187 );
188
189 do_action( 'foogallery_thumbnail_generation_test', $test_results );
190
191 return $test_results;
192 }
193
194 function override_test_thumb_url( $test_thumb_url ) {
195 if ( 'on' !== foogallery_get_setting( 'override_thumb_test', false ) ) {
196 $image_url = $this->find_first_image_in_media_library();
197
198 if ( $image_url !== false ) {
199 return $image_url;
200 }
201 }
202
203 //if we get here, then either, we have set the override_thumb_test setting,
204 //or there are no good images to use from the media library
205 return $test_thumb_url;
206 }
207
208 static function find_first_image_in_media_library() {
209 //try the first 10 attachments from the media library
210 $args = array(
211 'post_type' => 'attachment',
212 'post_mime_type' => 'image',
213 'post_status' => 'any',
214 'numberposts' => 10,
215 'orderby' => 'date',
216 'order' => 'ASC'
217 );
218 $query_images = new WP_Query( $args );
219 foreach ( $query_images->posts as $image ) {
220 $image_url = wp_get_attachment_url( $image->ID );
221
222 if ( !empty( $image_url ) ) {
223 if ( self::image_file_exists( $image_url ) || self::image_file_exists( $image_url, true ) ) {
224 return $image_url;
225 }
226 }
227 }
228
229 return false;
230 }
231
232 /**
233 * Check if a remote image file exists.
234 *
235 * @param string $url The url to the remote image.
236 * @param bool $force_https Whether to force the url to be https.
237 *
238 * @return bool Whether the remote image exists.
239 */
240 static function image_file_exists( $url, $force_https = false ) {
241 if ( $force_https ) {
242 $url = str_replace( 'http://', 'https://', $url );
243 }
244 $response = wp_remote_head( $url );
245 return 200 === wp_remote_retrieve_response_code( $response );
246 }
247 }
248 }
249