-
Notifications
You must be signed in to change notification settings - Fork 3
/
nova-wp-image-resizer.php
228 lines (177 loc) · 5.69 KB
/
nova-wp-image-resizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Resize images dinamically
*
* @param string $attach_id
* @param string|array $size
* @param bool $retina
* @return array
* @since 1.0
*
*/
function nova_resize_thumbnail( $attach_id, $size = '', $retina = false ) {
if ( ! $attach_id ) {
return;
}
// Check retina support
$retina_support = false;
// Check if image size is custom or defined
$is_intermediate = false;
// Size is an array
$int_size = isset( $size['size'] ) ? $size['size'] : '';
$width = isset( $size['width'] ) ? $size['width'] : '';
$height = isset( $size['height'] ) ? $size['height'] : '';
$crop = isset( $size['crop'] ) ? $size['crop'] : 'center-center';
// Check if size is intermediate or not
if ( $int_size ) {
$is_intermediate = true;
}
// Sanitize dimensions
$width = intval( $width );
$height = intval( $height );
$crop = is_array( $crop ) ? implode( '-', $crop ) : $crop;
// Get attachment data
$src = wp_get_attachment_image_src( $attach_id, 'full' );
$path = get_attached_file( $attach_id );
if ( empty( $path ) ) {
return;
}
// Get attachment details
$info = pathinfo( $path );
$extension = '.' . $info['extension'];
$path_no_ext = $info['dirname'] . '/' . $info['filename'];
// Array of valid crop locations
$crop_locations = array(
'left-top',
'right-top',
'center-top',
'left-center',
'right-center',
'center-center',
'left-bottom',
'right-bottom',
'center-bottom',
);
// Define crop suffix if custom crop is set and valid
$crop_suffix = ( is_string( $crop ) && array_key_exists( $crop, $crop_locations ) ) ? $crop : '';
// Turn crop value into array
$crop_array = $crop_suffix ? explode( '-', $crop ) : $crop;
// Calculate output dimensions after resize
$cropped_dims = image_resize_dimensions( $src[1], $src[2], $width, $height, $crop_array );
// Target image size dims
$dst_w = isset( $cropped_dims[4] ) ? $cropped_dims[4] : '';
$dst_h = isset( $cropped_dims[5] ) ? $cropped_dims[5] : '';
// Suffix width and height values
$s_width = $width ? $width : $dst_w;
$s_height = $height ? $height : $dst_h;
// Define suffix
if ( $retina ) {
$suffix = $s_width / 2 . 'x' . $s_height / 2;
} else {
$suffix = $s_width . 'x' . $s_height;
}
$suffix = $crop_suffix ? $suffix . '-' . $crop_suffix : $suffix;
$suffix = $retina ? $suffix . '@2x' : $suffix;
// Define custom intermediate_size based on suffix
$int_size = $int_size ? $int_size : 'nova_' . $suffix;
// If current image size is smaller or equal to target size return full image
if ( empty( $cropped_dims )
|| $dst_w > $src[1]
|| $dst_h > $src[2]
|| ( $dst_w == $src[1] && $dst_h == $src[2] )
) {
if ( $retina ) return; // don't return original for retina images
return array(
'url' => $src[0],
'width' => $src[1],
'height' => $src[2],
);
}
// Retina can't be cropped to exactly 2x
if ( $retina && ( $dst_w !== $width || $dst_h !== $height ) ) {
return;
}
// Get cropped path
$cropped_path = $path_no_ext . '-' . $suffix . $extension;
// Return chached image
// And try and generate retina if not created already
if ( file_exists( $cropped_path ) ) {
$new_path = str_replace( basename( $src[0] ), basename( $cropped_path ), $src[0] );
if ( ! $retina && $retina_support ) {
$retina_dims = array(
'width' => $dst_w*2,
'height' => $dst_h*2,
'crop' => $crop
);
$retina_src = nova_resize_thumbnail( $attach_id, $retina_dims, true );
}
return array(
'url' => $new_path,
'width' => $dst_w,
'height' => $dst_h,
'retina' => ! empty( $retina_src['url'] ) ? $retina_src['url'] : '',
);
}
// Crop image
$editor = wp_get_image_editor( $path );
if ( ! is_wp_error( $editor ) && ! is_wp_error( $editor->resize( $width, $height, $crop_array ) ) ) {
// Get resized file
$new_path = $editor->generate_filename( $suffix );
$editor = $editor->save( $new_path );
// Set new image url from resized image
if ( ! is_wp_error( $editor ) ) {
// Cropped image
$cropped_img = str_replace( basename( $src[0] ), basename( $new_path ), $src[0] );
// Generate retina version
if ( ! $retina && $retina_support ) {
$retina_dims = array(
'width' => $dst_w*2,
'height' => $dst_h*2,
'crop' => $crop
);
$retina_src = nova_resize_thumbnail( $attach_id, $retina_dims, true );
}
// Get thumbnail meta
$meta = wp_get_attachment_metadata( $attach_id );
// Update meta
if ( is_array( $meta ) ) {
$meta['sizes'] = isset( $meta['sizes'] ) ? $meta['sizes'] : array();
if ( ! array_key_exists( $int_size, $meta['sizes'] )
|| ( $dst_w != $meta['sizes'][$int_size]['width'] || $dst_h != $meta['sizes'][$int_size]['height'] )
) {
// Check correct mime type
$mime_type = wp_check_filetype( $cropped_img );
$mime_type = isset( $mime_type['type'] ) ? $mime_type['type'] : '';
// Cropped image file name
$dst_filename = $info['filename'] . '-' . $suffix . $extension;
// Add cropped image to image meta
$meta['sizes'][$int_size] = array(
'file' => $dst_filename,
'width' => $dst_w,
'height' => $dst_h,
'mime-type' => $mime_type,
'nova-wp' => true,
);
// Update meta
update_post_meta( $attach_id, '_wp_attachment_metadata', $meta );
}
}
// Return cropped image
return array(
'url' => $cropped_img,
'width' => $dst_w,
'height' => $dst_h,
'retina' => ! empty( $retina_src['url'] ) ? $retina_src['url'] : '',
'intermediate_size' => $int_size,
);
}
}
// Couldn't dynamically create image so return original
else {
return array(
'url' => $src[0],
'width' => $src[1],
'height' => $src[2],
);
}
}