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