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
|
|
|
//generate thumbs using WPThumb |
12
|
|
|
add_filter( 'foogallery_attachment_resize_thumbnail', array( $this, 'resize' ), 10, 3 ); |
13
|
|
|
|
14
|
|
|
add_filter( 'foogallery_test_thumb_url', array( $this, 'find_first_image_in_media_library' ) ); |
15
|
|
|
|
16
|
|
|
add_filter( 'foogallery_thumbnail_resize_args', array( $this, 'check_for_force_original_thumb') ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function check_for_force_original_thumb( $args ){ |
|
|
|
|
20
|
|
|
global $current_foogallery; |
|
|
|
|
21
|
|
|
|
22
|
|
|
if ( isset( $current_foogallery ) ) { |
23
|
|
|
$args['force_use_original_thumb'] = $current_foogallery->force_use_original_thumbs; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $args; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function resize( $original_image_src, $args, $thumbnail_object ) { |
30
|
|
|
global $current_foogallery; |
|
|
|
|
31
|
|
|
global $foogallery_last_generated_thumb_url; |
|
|
|
|
32
|
|
|
|
33
|
|
|
$arg_defaults = array( |
34
|
|
|
'width' => 0, |
35
|
|
|
'height' => 0, |
36
|
|
|
'crop' => true, |
37
|
|
|
'jpeg_quality' => foogallery_thumbnail_jpeg_quality(), |
38
|
|
|
'thumb_resize_animations' => foogallery_get_setting( 'thumb_resize_animations' ), |
39
|
|
|
'foogallery_attachment_id'=> $thumbnail_object->ID |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
if ( isset( $current_foogallery ) ) { |
43
|
|
|
$arg_defaults['foogallery_id'] = $current_foogallery->ID; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$args = wp_parse_args( $args, $arg_defaults ); |
47
|
|
|
|
48
|
|
|
//allow for plugins to change the thumbnail creation args |
49
|
|
|
$args = apply_filters( 'foogallery_thumbnail_resize_args', $args, $original_image_src, $thumbnail_object ); |
50
|
|
|
|
51
|
|
|
//check the current arguments passed in by the shortcode |
52
|
|
|
global $current_foogallery_arguments; |
53
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) { |
54
|
|
|
$thumbnail_args = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], $args, $current_foogallery_arguments ); |
55
|
|
|
$args = wp_parse_args( $thumbnail_args, $args ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$width = (int)$args['width']; |
59
|
|
|
$height = (int)$args['height']; |
60
|
|
|
$crop = (bool)$args['crop']; |
61
|
|
|
|
62
|
|
|
if ( 0 === $width && 0 === $height ) { |
63
|
|
|
return $original_image_src; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
//we can force the use of the originally uploaded full-size image |
67
|
|
|
$force_use_original_image = isset( $args['force_use_original_image'] ) && true === $args['force_use_original_image']; |
68
|
|
|
|
69
|
|
|
if ( $force_use_original_image ) { |
70
|
|
|
$fullsize = wp_get_attachment_image_src( $thumbnail_object->ID, 'fullsize' ); |
71
|
|
|
|
72
|
|
|
return $fullsize[0]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
//we can force the use of the original WP icon or WP-generated thumb by passing through args individually |
76
|
|
|
$force_use_original_thumb = isset( $args['force_use_original_thumb'] ) && true === $args['force_use_original_thumb']; |
77
|
|
|
|
78
|
|
|
if ( $force_use_original_thumb ) { |
79
|
|
|
$thumbnail_icon = wp_get_attachment_image_src( $thumbnail_object->ID, array( $width, $height ) ); |
80
|
|
|
|
81
|
|
|
return $thumbnail_icon[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//we can force the use of original WP thumbs by passing through args individually, or by saved settings |
85
|
|
|
$use_original_thumbs = ( isset( $args['use_original_thumbs'] ) && true === $args['use_original_thumbs'] ) || 'on' === foogallery_get_setting( 'use_original_thumbs' ); |
86
|
|
|
|
87
|
|
|
if ( $use_original_thumbs ) { |
88
|
|
|
|
89
|
|
|
$option_thumbnail_size_w = get_option( 'thumbnail_size_w' ); |
90
|
|
|
$option_thumbnail_size_h = get_option( 'thumbnail_size_h' ); |
91
|
|
|
$option_thumbnail_crop = get_option( 'thumbnail_crop' ); |
92
|
|
|
|
93
|
|
|
//check if we are trying to get back the default thumbnail that we already have |
94
|
|
|
if ( $thumbnail_object->ID > 0 && $width == $option_thumbnail_size_w && $height == $option_thumbnail_size_h && $crop == $option_thumbnail_crop ) { |
95
|
|
|
$thumbnail_attributes = wp_get_attachment_image_src( $thumbnail_object->ID ); |
96
|
|
|
|
97
|
|
|
return $thumbnail_attributes[0]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ( $thumbnail_object->ID > 0 ) { |
102
|
|
|
$crop_from_position = get_post_meta( $thumbnail_object->ID, 'wpthumb_crop_pos', true ); |
103
|
|
|
|
104
|
|
|
if ( !empty( $crop_from_position ) ) { |
105
|
|
|
$args['crop_from_position'] = $crop_from_position; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//remove invalid resize args |
110
|
|
|
if ( array_key_exists( 'height', $args ) && 0 === $args['height'] ) { |
111
|
|
|
unset( $args['height'] ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//do some checks to see if the image is smaller |
115
|
|
|
if ( $this->should_resize( $thumbnail_object, $args ) ) { |
116
|
|
|
//save the generated thumb url to a global so that we can use it later if needed |
117
|
|
|
$foogallery_last_generated_thumb_url = wpthumb( $original_image_src, $args ); |
118
|
|
|
} else { |
119
|
|
|
$foogallery_last_generated_thumb_url = $original_image_src; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $foogallery_last_generated_thumb_url; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function should_resize($thumbnail_object, $args) { |
|
|
|
|
126
|
|
|
$original_width = $thumbnail_object->width; |
127
|
|
|
$original_height = $thumbnail_object->height; |
128
|
|
|
$new_width = isset( $args['width'] ) ? $args['width'] : 0; |
129
|
|
|
$new_height = isset( $args['height'] ) ? $args['height'] : 0; |
130
|
|
|
|
131
|
|
|
if ( $new_width > 0 && $new_height > 0 ) { |
132
|
|
|
return $original_width > $new_width || $original_height > $new_height; |
133
|
|
|
} else if ( $new_width > 0 ) { |
134
|
|
|
return $original_width > $new_width; |
135
|
|
|
} |
136
|
|
|
return $original_height > $new_height; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function run_thumbnail_generation_tests() { |
|
|
|
|
140
|
|
|
$test_image_url = foogallery_test_thumb_url(); |
141
|
|
|
|
142
|
|
|
//next, generate a thumbnail |
143
|
|
|
$test_args = array( |
144
|
|
|
'width' => 20, |
145
|
|
|
'height' => 20, |
146
|
|
|
'crop' => true, |
147
|
|
|
'jpeg_quality' => foogallery_thumbnail_jpeg_quality() |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
//first, clear any previous cached files |
151
|
|
|
$thumb = new WP_Thumb( $test_image_url, $test_args ); |
152
|
|
|
wpthumb_rmdir_recursive( $thumb->getCacheFileDirectory() ); |
153
|
|
|
|
154
|
|
|
$test_thumb = new WP_Thumb( $test_image_url, $test_args ); |
155
|
|
|
$generated_thumb = $test_thumb->returnImage(); |
156
|
|
|
$success = $test_image_url !== $generated_thumb; |
157
|
|
|
$file_info = wp_check_filetype( $test_image_url ); |
158
|
|
|
|
159
|
|
|
$test_results = array( |
160
|
|
|
'success' => $success, |
161
|
|
|
'thumb' => $generated_thumb, |
162
|
|
|
'error' => $test_thumb->errored() ? $test_thumb->error : '', |
163
|
|
|
'file_info' => $file_info |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
do_action( 'foogallery_thumbnail_generation_test', $test_results ); |
167
|
|
|
|
168
|
|
|
return $test_results; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function find_first_image_in_media_library( $test_thumb_url ) { |
|
|
|
|
172
|
|
|
$args = array( |
173
|
|
|
'post_type' => 'attachment', |
174
|
|
|
'post_mime_type' =>'image', |
175
|
|
|
'post_status' => 'inherit', |
176
|
|
|
'posts_per_page' => 1 |
177
|
|
|
); |
178
|
|
|
$query_images = new WP_Query( $args ); |
179
|
|
|
foreach ( $query_images->posts as $image) { |
180
|
|
|
return $image->guid; |
181
|
|
|
} |
182
|
|
|
return $test_thumb_url; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
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.