1 | <?php
|
---|
2 | /**
|
---|
3 | * WordPress API for media display.
|
---|
4 | *
|
---|
5 | * @package WordPress
|
---|
6 | */
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Scale down the default size of an image.
|
---|
10 | *
|
---|
11 | * This is so that the image is a better fit for the editor and theme.
|
---|
12 | *
|
---|
13 | * The $size parameter accepts either an array or a string. The supported string
|
---|
14 | * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
|
---|
15 | * 128 width and 96 height in pixels. Also supported for the string value is
|
---|
16 | * 'medium' and 'full'. The 'full' isn't actually supported, but any value other
|
---|
17 | * than the supported will result in the content_width size or 500 if that is
|
---|
18 | * not set.
|
---|
19 | *
|
---|
20 | * Finally, there is a filter named, 'editor_max_image_size' that will be called
|
---|
21 | * on the calculated array for width and height, respectively. The second
|
---|
22 | * parameter will be the value that was in the $size parameter. The returned
|
---|
23 | * type for the hook is an array with the width as the first element and the
|
---|
24 | * height as the second element.
|
---|
25 | *
|
---|
26 | * @since 2.5.0
|
---|
27 | * @uses wp_constrain_dimensions() This function passes the widths and the heights.
|
---|
28 | *
|
---|
29 | * @param int $width Width of the image
|
---|
30 | * @param int $height Height of the image
|
---|
31 | * @param string|array $size Size of what the result image should be.
|
---|
32 | * @return array Width and height of what the result image should resize to.
|
---|
33 | */
|
---|
34 | function image_constrain_size_for_editor($width, $height, $size = 'medium') {
|
---|
35 | global $content_width;
|
---|
36 |
|
---|
37 | if ( is_array($size) ) {
|
---|
38 | $max_width = $size[0];
|
---|
39 | $max_height = $size[1];
|
---|
40 | }
|
---|
41 | elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
|
---|
42 | $max_width = intval(get_option('thumbnail_size_w'));
|
---|
43 | $max_height = intval(get_option('thumbnail_size_h'));
|
---|
44 | // last chance thumbnail size defaults
|
---|
45 | if ( !$max_width && !$max_height ) {
|
---|
46 | $max_width = 128;
|
---|
47 | $max_height = 96;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | elseif ( $size == 'medium' ) {
|
---|
51 | $max_width = intval(get_option('medium_size_w'));
|
---|
52 | $max_height = intval(get_option('medium_size_h'));
|
---|
53 | // if no width is set, default to the theme content width if available
|
---|
54 | }
|
---|
55 | elseif ( $size == 'large' ) {
|
---|
56 | // we're inserting a large size image into the editor. if it's a really
|
---|
57 | // big image we'll scale it down to fit reasonably within the editor
|
---|
58 | // itself, and within the theme's content width if it's known. the user
|
---|
59 | // can resize it in the editor if they wish.
|
---|
60 | $max_width = intval(get_option('large_size_w'));
|
---|
61 | $max_height = intval(get_option('large_size_h'));
|
---|
62 | if ( intval($content_width) > 0 )
|
---|
63 | $max_width = min( intval($content_width), $max_width );
|
---|
64 | }
|
---|
65 | // $size == 'full' has no constraint
|
---|
66 | else {
|
---|
67 | $max_width = $width;
|
---|
68 | $max_height = $height;
|
---|
69 | }
|
---|
70 |
|
---|
71 | list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );
|
---|
72 |
|
---|
73 | return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Retrieve width and height attributes using given width and height values.
|
---|
78 | *
|
---|
79 | * Both attributes are required in the sense that both parameters must have a
|
---|
80 | * value, but are optional in that if you set them to false or null, then they
|
---|
81 | * will not be added to the returned string.
|
---|
82 | *
|
---|
83 | * You can set the value using a string, but it will only take numeric values.
|
---|
84 | * If you wish to put 'px' after the numbers, then it will be stripped out of
|
---|
85 | * the return.
|
---|
86 | *
|
---|
87 | * @since 2.5.0
|
---|
88 | *
|
---|
89 | * @param int|string $width Optional. Width attribute value.
|
---|
90 | * @param int|string $height Optional. Height attribute value.
|
---|
91 | * @return string HTML attributes for width and, or height.
|
---|
92 | */
|
---|
93 | function image_hwstring($width, $height) {
|
---|
94 | $out = '';
|
---|
95 | if ($width)
|
---|
96 | $out .= 'width="'.intval($width).'" ';
|
---|
97 | if ($height)
|
---|
98 | $out .= 'height="'.intval($height).'" ';
|
---|
99 | return $out;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Scale an image to fit a particular size (such as 'thumb' or 'medium').
|
---|
104 | *
|
---|
105 | * Array with image url, width, height, and whether is intermediate size, in
|
---|
106 | * that order is returned on success is returned. $is_intermediate is true if
|
---|
107 | * $url is a resized image, false if it is the original.
|
---|
108 | *
|
---|
109 | * The URL might be the original image, or it might be a resized version. This
|
---|
110 | * function won't create a new resized copy, it will just return an already
|
---|
111 | * resized one if it exists.
|
---|
112 | *
|
---|
113 | * A plugin may use the 'image_downsize' filter to hook into and offer image
|
---|
114 | * resizing services for images. The hook must return an array with the same
|
---|
115 | * elements that are returned in the function. The first element being the URL
|
---|
116 | * to the new image that was resized.
|
---|
117 | *
|
---|
118 | * @since 2.5.0
|
---|
119 | * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
|
---|
120 | * resize services.
|
---|
121 | *
|
---|
122 | * @param int $id Attachment ID for image.
|
---|
123 | * @param string $size Optional, default is 'medium'. Size of image, can be 'thumbnail'.
|
---|
124 | * @return bool|array False on failure, array on success.
|
---|
125 | */
|
---|
126 | function image_downsize($id, $size = 'medium') {
|
---|
127 |
|
---|
128 | if ( !wp_attachment_is_image($id) )
|
---|
129 | return false;
|
---|
130 |
|
---|
131 | $img_url = wp_get_attachment_url($id);
|
---|
132 | $meta = wp_get_attachment_metadata($id);
|
---|
133 | $width = $height = 0;
|
---|
134 | $is_intermediate = false;
|
---|
135 |
|
---|
136 | // plugins can use this to provide resize services
|
---|
137 | if ( $out = apply_filters('image_downsize', false, $id, $size) )
|
---|
138 | return $out;
|
---|
139 |
|
---|
140 | // try for a new style intermediate size
|
---|
141 | if ( $intermediate = image_get_intermediate_size($id, $size) ) {
|
---|
142 | $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
|
---|
143 | $width = $intermediate['width'];
|
---|
144 | $height = $intermediate['height'];
|
---|
145 | $is_intermediate = true;
|
---|
146 | }
|
---|
147 | elseif ( $size == 'thumbnail' ) {
|
---|
148 | // fall back to the old thumbnail
|
---|
149 | if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
|
---|
150 | $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
|
---|
151 | $width = $info[0];
|
---|
152 | $height = $info[1];
|
---|
153 | $is_intermediate = true;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if ( !$width && !$height && isset($meta['width'], $meta['height']) ) {
|
---|
157 | // any other type: use the real image
|
---|
158 | $width = $meta['width'];
|
---|
159 | $height = $meta['height'];
|
---|
160 | }
|
---|
161 |
|
---|
162 | if ( $img_url) {
|
---|
163 | // we have the actual image size, but might need to further constrain it if content_width is narrower
|
---|
164 | list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
|
---|
165 |
|
---|
166 | return array( $img_url, $width, $height, $is_intermediate );
|
---|
167 | }
|
---|
168 | return false;
|
---|
169 |
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * An <img src /> tag for an image attachment, scaling it down if requested.
|
---|
174 | *
|
---|
175 | * The filter 'get_image_tag_class' allows for changing the class name for the
|
---|
176 | * image without having to use regular expressions on the HTML content. The
|
---|
177 | * parameters are: what WordPress will use for the class, the Attachment ID,
|
---|
178 | * image align value, and the size the image should be.
|
---|
179 | *
|
---|
180 | * The second filter 'get_image_tag' has the HTML content, which can then be
|
---|
181 | * further manipulated by a plugin to change all attribute values and even HTML
|
---|
182 | * content.
|
---|
183 | *
|
---|
184 | * @since 2.5.0
|
---|
185 | *
|
---|
186 | * @uses apply_filters() The 'get_image_tag_class' filter is the IMG element
|
---|
187 | * class attribute.
|
---|
188 | * @uses apply_filters() The 'get_image_tag' filter is the full IMG element with
|
---|
189 | * all attributes.
|
---|
190 | *
|
---|
191 | * @param int $id Attachment ID.
|
---|
192 | * @param string $alt Image Description for the alt attribute.
|
---|
193 | * @param string $title Image Description for the title attribute.
|
---|
194 | * @param string $align Part of the class name for aligning the image.
|
---|
195 | * @param string $size Optional. Default is 'medium'.
|
---|
196 | * @return string HTML IMG element for given image attachment
|
---|
197 | */
|
---|
198 | function get_image_tag($id, $alt, $title, $align, $size='medium') {
|
---|
199 |
|
---|
200 | list( $img_src, $width, $height ) = image_downsize($id, $size);
|
---|
201 | $hwstring = image_hwstring($width, $height);
|
---|
202 |
|
---|
203 | $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id;
|
---|
204 | $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
|
---|
205 |
|
---|
206 | $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />';
|
---|
207 |
|
---|
208 | $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
|
---|
209 |
|
---|
210 | return $html;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Calculates the new dimentions for a downsampled image.
|
---|
215 | *
|
---|
216 | * Same as {@link wp_shrink_dimensions()}, except the max parameters are
|
---|
217 | * optional. If either width or height are empty, no constraint is applied on
|
---|
218 | * that dimension.
|
---|
219 | *
|
---|
220 | * @since 2.5.0
|
---|
221 | *
|
---|
222 | * @param int $current_width Current width of the image.
|
---|
223 | * @param int $current_height Current height of the image.
|
---|
224 | * @param int $max_width Optional. Maximum wanted width.
|
---|
225 | * @param int $max_height Optional. Maximum wanted height.
|
---|
226 | * @return array First item is the width, the second item is the height.
|
---|
227 | */
|
---|
228 | function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
|
---|
229 | if ( !$max_width and !$max_height )
|
---|
230 | return array( $current_width, $current_height );
|
---|
231 |
|
---|
232 | $width_ratio = $height_ratio = 1.0;
|
---|
233 |
|
---|
234 | if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width )
|
---|
235 | $width_ratio = $max_width / $current_width;
|
---|
236 |
|
---|
237 | if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height )
|
---|
238 | $height_ratio = $max_height / $current_height;
|
---|
239 |
|
---|
240 | // the smaller ratio is the one we need to fit it to the constraining box
|
---|
241 | $ratio = min( $width_ratio, $height_ratio );
|
---|
242 |
|
---|
243 | return array( intval($current_width * $ratio), intval($current_height * $ratio) );
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Retrieve calculated resized dimensions for use in imagecopyresampled().
|
---|
248 | *
|
---|
249 | * Calculate dimensions and coordinates for a resized image that fits within a
|
---|
250 | * specified width and height. If $crop is true, the largest matching central
|
---|
251 | * portion of the image will be cropped out and resized to the required size.
|
---|
252 | *
|
---|
253 | * @since 2.5.0
|
---|
254 | *
|
---|
255 | * @param int $orig_w Original width.
|
---|
256 | * @param int $orig_h Original height.
|
---|
257 | * @param int $dest_w New width.
|
---|
258 | * @param int $dest_h New height.
|
---|
259 | * @param bool $crop Optional, default is false. Whether to crop image or resize.
|
---|
260 | * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function.
|
---|
261 | */
|
---|
262 | function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
|
---|
263 |
|
---|
264 | if ($orig_w <= 0 || $orig_h <= 0)
|
---|
265 | return false;
|
---|
266 | // at least one of dest_w or dest_h must be specific
|
---|
267 | if ($dest_w <= 0 && $dest_h <= 0)
|
---|
268 | return false;
|
---|
269 |
|
---|
270 | if ( $crop ) {
|
---|
271 | // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
|
---|
272 | $aspect_ratio = $orig_w / $orig_h;
|
---|
273 | $new_w = min($dest_w, $orig_w);
|
---|
274 | $new_h = min($dest_h, $orig_h);
|
---|
275 | if (!$new_w) {
|
---|
276 | $new_w = intval($new_h * $aspect_ratio);
|
---|
277 | }
|
---|
278 | if (!$new_h) {
|
---|
279 | $new_h = intval($new_w / $aspect_ratio);
|
---|
280 | }
|
---|
281 |
|
---|
282 | $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
|
---|
283 |
|
---|
284 | $crop_w = ceil($new_w / $size_ratio);
|
---|
285 | $crop_h = ceil($new_h / $size_ratio);
|
---|
286 |
|
---|
287 | $s_x = floor(($orig_w - $crop_w)/2);
|
---|
288 | $s_y = floor(($orig_h - $crop_h)/2);
|
---|
289 | }
|
---|
290 | else {
|
---|
291 | // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
|
---|
292 | $crop_w = $orig_w;
|
---|
293 | $crop_h = $orig_h;
|
---|
294 |
|
---|
295 | $s_x = 0;
|
---|
296 | $s_y = 0;
|
---|
297 |
|
---|
298 | list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
|
---|
299 | }
|
---|
300 |
|
---|
301 | // if the resulting image would be the same size or larger we don't want to resize it
|
---|
302 | if ($new_w >= $orig_w && $new_h >= $orig_h)
|
---|
303 | return false;
|
---|
304 |
|
---|
305 | // the return array matches the parameters to imagecopyresampled()
|
---|
306 | // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
|
---|
307 | return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h);
|
---|
308 |
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Scale down an image to fit a particular size and save a new copy of the image.
|
---|
313 | *
|
---|
314 | * The PNG transparency will be preserved using the function, as well as the
|
---|
315 | * image type. If the file going in is PNG, then the resized image is going to
|
---|
316 | * be PNG. The only supported image types are PNG, GIF, and JPEG.
|
---|
317 | *
|
---|
318 | * Some functionality requires API to exist, so some PHP version may lose out
|
---|
319 | * support. This is not the fault of WordPress (where functionality is
|
---|
320 | * downgraded, not actual defects), but of your PHP version.
|
---|
321 | *
|
---|
322 | * @since 2.5.0
|
---|
323 | *
|
---|
324 | * @param string $file Image file path.
|
---|
325 | * @param int $max_w Maximum width to resize to.
|
---|
326 | * @param int $max_h Maximum height to resize to.
|
---|
327 | * @param bool $crop Optional. Whether to crop image or resize.
|
---|
328 | * @param string $suffix Optional. File Suffix.
|
---|
329 | * @param string $dest_path Optional. New image file path.
|
---|
330 | * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
|
---|
331 | * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
|
---|
332 | */
|
---|
333 | function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
|
---|
334 |
|
---|
335 | $image = wp_load_image( $file );
|
---|
336 | if ( !is_resource( $image ) )
|
---|
337 | return new WP_Error('error_loading_image', $image);
|
---|
338 |
|
---|
339 | list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
|
---|
340 | $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
|
---|
341 | if (!$dims)
|
---|
342 | return $dims;
|
---|
343 | list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
|
---|
344 |
|
---|
345 | $newimage = imagecreatetruecolor( $dst_w, $dst_h);
|
---|
346 |
|
---|
347 | // preserve PNG transparency
|
---|
348 | if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
|
---|
349 | imagealphablending( $newimage, false);
|
---|
350 | imagesavealpha( $newimage, true);
|
---|
351 | }
|
---|
352 |
|
---|
353 | imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
|
---|
354 |
|
---|
355 | // we don't need the original in memory anymore
|
---|
356 | imagedestroy( $image );
|
---|
357 |
|
---|
358 | // $suffix will be appended to the destination filename, just before the extension
|
---|
359 | if ( !$suffix )
|
---|
360 | $suffix = "{$dst_w}x{$dst_h}";
|
---|
361 |
|
---|
362 | $info = pathinfo($file);
|
---|
363 | $dir = $info['dirname'];
|
---|
364 | $ext = $info['extension'];
|
---|
365 | $name = basename($file, ".{$ext}");
|
---|
366 | if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
|
---|
367 | $dir = $_dest_path;
|
---|
368 | $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
|
---|
369 |
|
---|
370 | if ( $orig_type == IMAGETYPE_GIF ) {
|
---|
371 | if (!imagegif( $newimage, $destfilename ) )
|
---|
372 | return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
|
---|
373 | }
|
---|
374 | elseif ( $orig_type == IMAGETYPE_PNG ) {
|
---|
375 | if (!imagepng( $newimage, $destfilename ) )
|
---|
376 | return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
|
---|
377 | }
|
---|
378 | else {
|
---|
379 | // all other formats are converted to jpg
|
---|
380 | $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
|
---|
381 | if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
|
---|
382 | return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
|
---|
383 | }
|
---|
384 |
|
---|
385 | imagedestroy( $newimage );
|
---|
386 |
|
---|
387 | // Set correct file permissions
|
---|
388 | $stat = stat( dirname( $destfilename ));
|
---|
389 | $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
|
---|
390 | @ chmod( $destfilename, $perms );
|
---|
391 |
|
---|
392 | return $destfilename;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Resize an image to make a thumbnail or intermediate size.
|
---|
397 | *
|
---|
398 | * The returned array has the file size, the image width, and image height. The
|
---|
399 | * filter 'image_make_intermediate_size' can be used to hook in and change the
|
---|
400 | * values of the returned array. The only parameter is the resized file path.
|
---|
401 | *
|
---|
402 | * @since 2.5.0
|
---|
403 | *
|
---|
404 | * @param string $file File path.
|
---|
405 | * @param int $width Image width.
|
---|
406 | * @param int $height Image height.
|
---|
407 | * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
|
---|
408 | * @return bool|array False, if no image was created. Metadata array on success.
|
---|
409 | */
|
---|
410 | function image_make_intermediate_size($file, $width, $height, $crop=false) {
|
---|
411 | if ( $width || $height ) {
|
---|
412 | $resized_file = image_resize($file, $width, $height, $crop);
|
---|
413 | if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) {
|
---|
414 | $resized_file = apply_filters('image_make_intermediate_size', $resized_file);
|
---|
415 | return array(
|
---|
416 | 'file' => basename( $resized_file ),
|
---|
417 | 'width' => $info[0],
|
---|
418 | 'height' => $info[1],
|
---|
419 | );
|
---|
420 | }
|
---|
421 | }
|
---|
422 | return false;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Retrieve the image's intermediate size (resized) path, width, and height.
|
---|
427 | *
|
---|
428 | * The $size parameter can be an array with the width and height respectively.
|
---|
429 | * If the size matches the 'sizes' metadata array for width and height, then it
|
---|
430 | * will be used. If there is no direct match, then the nearest image size larger
|
---|
431 | * than the specified size will be used. If nothing is found, then the function
|
---|
432 | * will break out and return false.
|
---|
433 | *
|
---|
434 | * The metadata 'sizes' is used for compatible sizes that can be used for the
|
---|
435 | * parameter $size value.
|
---|
436 | *
|
---|
437 | * The url path will be given, when the $size parameter is a string.
|
---|
438 | *
|
---|
439 | * @since 2.5.0
|
---|
440 | *
|
---|
441 | * @param int $post_id Attachment ID for image.
|
---|
442 | * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
|
---|
443 | * @return bool|array False on failure or array of file path, width, and height on success.
|
---|
444 | */
|
---|
445 | function image_get_intermediate_size($post_id, $size='thumbnail') {
|
---|
446 | if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
|
---|
447 | return false;
|
---|
448 |
|
---|
449 | // get the best one for a specified set of dimensions
|
---|
450 | if ( is_array($size) && !empty($imagedata['sizes']) ) {
|
---|
451 | foreach ( $imagedata['sizes'] as $_size => $data ) {
|
---|
452 | // already cropped to width or height; so use this size
|
---|
453 | if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
|
---|
454 | $file = $data['file'];
|
---|
455 | list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
|
---|
456 | return compact( 'file', 'width', 'height' );
|
---|
457 | }
|
---|
458 | // add to lookup table: area => size
|
---|
459 | $areas[$data['width'] * $data['height']] = $_size;
|
---|
460 | }
|
---|
461 | if ( !$size || !empty($areas) ) {
|
---|
462 | // find for the smallest image not smaller than the desired size
|
---|
463 | ksort($areas);
|
---|
464 | foreach ( $areas as $_size ) {
|
---|
465 | $data = $imagedata['sizes'][$_size];
|
---|
466 | if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
|
---|
467 | $file = $data['file'];
|
---|
468 | list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
|
---|
469 | return compact( 'file', 'width', 'height' );
|
---|
470 | }
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
|
---|
476 | return false;
|
---|
477 |
|
---|
478 | $data = $imagedata['sizes'][$size];
|
---|
479 | // include the full filesystem path of the intermediate file
|
---|
480 | if ( empty($data['path']) && !empty($data['file']) ) {
|
---|
481 | $file_url = wp_get_attachment_url($post_id);
|
---|
482 | $data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
|
---|
483 | $data['url'] = path_join( dirname($file_url), $data['file'] );
|
---|
484 | }
|
---|
485 | return $data;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Retrieve an image to represent an attachment.
|
---|
490 | *
|
---|
491 | * A mime icon for files, thumbnail or intermediate size for images.
|
---|
492 | *
|
---|
493 | * @since 2.5.0
|
---|
494 | *
|
---|
495 | * @param int $attachment_id Image attachment ID.
|
---|
496 | * @param string $size Optional, default is 'thumbnail'.
|
---|
497 | * @param bool $icon Optional, default is false. Whether it is an icon.
|
---|
498 | * @return bool|array Returns an array (url, width, height), or false, if no image is available.
|
---|
499 | */
|
---|
500 | function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
|
---|
501 |
|
---|
502 | // get a thumbnail or intermediate image if there is one
|
---|
503 | if ( $image = image_downsize($attachment_id, $size) )
|
---|
504 | return $image;
|
---|
505 |
|
---|
506 | $src = false;
|
---|
507 |
|
---|
508 | if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
|
---|
509 | $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
|
---|
510 | $src_file = $icon_dir . '/' . basename($src);
|
---|
511 | @list($width, $height) = getimagesize($src_file);
|
---|
512 | }
|
---|
513 | if ( $src && $width && $height )
|
---|
514 | return array( $src, $width, $height );
|
---|
515 | return false;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Get an HTML img element representing an image attachment
|
---|
520 | *
|
---|
521 | * @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array
|
---|
522 | * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions
|
---|
523 | * @since 2.5.0
|
---|
524 | *
|
---|
525 | * @param int $attachment_id Image attachment ID.
|
---|
526 | * @param string $size Optional, default is 'thumbnail'.
|
---|
527 | * @param bool $icon Optional, default is false. Whether it is an icon.
|
---|
528 | * @return string HTML img element or empty string on failure.
|
---|
529 | */
|
---|
530 | function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) {
|
---|
531 |
|
---|
532 | $html = '';
|
---|
533 | $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
|
---|
534 | if ( $image ) {
|
---|
535 | list($src, $width, $height) = $image;
|
---|
536 | $hwstring = image_hwstring($width, $height);
|
---|
537 | if ( is_array($size) )
|
---|
538 | $size = join('x', $size);
|
---|
539 | $attachment =& get_post($attachment_id);
|
---|
540 | $attr = array(
|
---|
541 | 'src' => $src,
|
---|
542 | 'class' => "attachment-$size",
|
---|
543 | 'alt' => trim(strip_tags( $attachment->post_excerpt )),
|
---|
544 | 'title' => trim(strip_tags( $attachment->post_title )),
|
---|
545 | );
|
---|
546 | $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
|
---|
547 | $attr = array_map( 'esc_attr', $attr );
|
---|
548 | $html = rtrim("<img $hwstring");
|
---|
549 | foreach ( $attr as $name => $value ) {
|
---|
550 | $html .= " $name=" . '"' . $value . '"';
|
---|
551 | }
|
---|
552 | $html .= ' />';
|
---|
553 | }
|
---|
554 |
|
---|
555 | return $html;
|
---|
556 | }
|
---|
557 |
|
---|
558 | add_shortcode('wp_caption', 'img_caption_shortcode');
|
---|
559 | add_shortcode('caption', 'img_caption_shortcode');
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * The Caption shortcode.
|
---|
563 | *
|
---|
564 | * Allows a plugin to replace the content that would otherwise be returned. The
|
---|
565 | * filter is 'img_caption_shortcode' and passes an empty string, the attr
|
---|
566 | * parameter and the content parameter values.
|
---|
567 | *
|
---|
568 | * The supported attributes for the shortcode are 'id', 'align', 'width', and
|
---|
569 | * 'caption'.
|
---|
570 | *
|
---|
571 | * @since 2.6.0
|
---|
572 | *
|
---|
573 | * @param array $attr Attributes attributed to the shortcode.
|
---|
574 | * @param string $content Optional. Shortcode content.
|
---|
575 | * @return string
|
---|
576 | */
|
---|
577 | function img_caption_shortcode($attr, $content = null) {
|
---|
578 |
|
---|
579 | // Allow plugins/themes to override the default caption template.
|
---|
580 | $output = apply_filters('img_caption_shortcode', '', $attr, $content);
|
---|
581 | if ( $output != '' )
|
---|
582 | return $output;
|
---|
583 |
|
---|
584 | extract(shortcode_atts(array(
|
---|
585 | 'id' => '',
|
---|
586 | 'align' => 'alignnone',
|
---|
587 | 'width' => '',
|
---|
588 | 'caption' => ''
|
---|
589 | ), $attr));
|
---|
590 |
|
---|
591 | if ( 1 > (int) $width || empty($caption) )
|
---|
592 | return $content;
|
---|
593 |
|
---|
594 | if ( $id ) $id = 'id="' . $id . '" ';
|
---|
595 |
|
---|
596 | return '<div ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px">'
|
---|
597 | . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
|
---|
598 | }
|
---|
599 |
|
---|
600 | add_shortcode('gallery', 'gallery_shortcode');
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * The Gallery shortcode.
|
---|
604 | *
|
---|
605 | * This implements the functionality of the Gallery Shortcode for displaying
|
---|
606 | * WordPress images on a post.
|
---|
607 | *
|
---|
608 | * @since 2.5.0
|
---|
609 | *
|
---|
610 | * @param array $attr Attributes attributed to the shortcode.
|
---|
611 | * @return string HTML content to display gallery.
|
---|
612 | */
|
---|
613 | function gallery_shortcode($attr) {
|
---|
614 | global $post;
|
---|
615 |
|
---|
616 | static $instance = 0;
|
---|
617 | $instance++;
|
---|
618 |
|
---|
619 | // Allow plugins/themes to override the default gallery template.
|
---|
620 | $output = apply_filters('post_gallery', '', $attr);
|
---|
621 | if ( $output != '' )
|
---|
622 | return $output;
|
---|
623 |
|
---|
624 | // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
|
---|
625 | if ( isset( $attr['orderby'] ) ) {
|
---|
626 | $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
|
---|
627 | if ( !$attr['orderby'] )
|
---|
628 | unset( $attr['orderby'] );
|
---|
629 | }
|
---|
630 |
|
---|
631 | extract(shortcode_atts(array(
|
---|
632 | 'order' => 'ASC',
|
---|
633 | 'orderby' => 'menu_order ID',
|
---|
634 | 'id' => $post->ID,
|
---|
635 | 'itemtag' => 'dl',
|
---|
636 | 'icontag' => 'dt',
|
---|
637 | 'captiontag' => 'dd',
|
---|
638 | 'columns' => 3,
|
---|
639 | 'size' => 'thumbnail'
|
---|
640 | ), $attr));
|
---|
641 |
|
---|
642 | $id = intval($id);
|
---|
643 | $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
|
---|
644 |
|
---|
645 | if ( empty($attachments) )
|
---|
646 | return '';
|
---|
647 |
|
---|
648 | if ( is_feed() ) {
|
---|
649 | $output = "\n";
|
---|
650 | foreach ( $attachments as $att_id => $attachment )
|
---|
651 | $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
|
---|
652 | return $output;
|
---|
653 | }
|
---|
654 |
|
---|
655 | $itemtag = tag_escape($itemtag);
|
---|
656 | $captiontag = tag_escape($captiontag);
|
---|
657 | $columns = intval($columns);
|
---|
658 | $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
|
---|
659 |
|
---|
660 | $selector = "gallery-{$instance}";
|
---|
661 |
|
---|
662 | $output = apply_filters('gallery_style', "
|
---|
663 | <style type='text/css'>
|
---|
664 | #{$selector} {
|
---|
665 | margin: auto;
|
---|
666 | }
|
---|
667 | #{$selector} .gallery-item {
|
---|
668 | float: left;
|
---|
669 | margin-top: 10px;
|
---|
670 | text-align: center;
|
---|
671 | width: {$itemwidth}%; }
|
---|
672 | #{$selector} img {
|
---|
673 | border: 2px solid #cfcfcf;
|
---|
674 | }
|
---|
675 | #{$selector} .gallery-caption {
|
---|
676 | margin-left: 0;
|
---|
677 | }
|
---|
678 | </style>
|
---|
679 | <!-- see gallery_shortcode() in wp-includes/media.php -->
|
---|
680 | <div id='$selector' class='gallery galleryid-{$id}'>");
|
---|
681 |
|
---|
682 | $i = 0;
|
---|
683 | foreach ( $attachments as $id => $attachment ) {
|
---|
684 | $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
|
---|
685 |
|
---|
686 | $output .= "<{$itemtag} class='gallery-item'>";
|
---|
687 | $output .= "
|
---|
688 | <{$icontag} class='gallery-icon'>
|
---|
689 | $link
|
---|
690 | </{$icontag}>";
|
---|
691 | if ( $captiontag && trim($attachment->post_excerpt) ) {
|
---|
692 | $output .= "
|
---|
693 | <{$captiontag} class='gallery-caption'>
|
---|
694 | " . wptexturize($attachment->post_excerpt) . "
|
---|
695 | </{$captiontag}>";
|
---|
696 | }
|
---|
697 | $output .= "</{$itemtag}>";
|
---|
698 | if ( $columns > 0 && ++$i % $columns == 0 )
|
---|
699 | $output .= '<br style="clear: both" />';
|
---|
700 | }
|
---|
701 |
|
---|
702 | $output .= "
|
---|
703 | <br style='clear: both;' />
|
---|
704 | </div>\n";
|
---|
705 |
|
---|
706 | return $output;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Display previous image link that has the same post parent.
|
---|
711 | *
|
---|
712 | * @since 2.5.0
|
---|
713 | * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
|
---|
714 | * @param string $text Optional, default is false. If included, link will reflect $text variable.
|
---|
715 | * @return string HTML content.
|
---|
716 | */
|
---|
717 | function previous_image_link($size = 'thumbnail', $text = false) {
|
---|
718 | adjacent_image_link(true, $size, $text);
|
---|
719 | }
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Display next image link that has the same post parent.
|
---|
723 | *
|
---|
724 | * @since 2.5.0
|
---|
725 | * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
|
---|
726 | * @param string $text Optional, default is false. If included, link will reflect $text variable.
|
---|
727 | * @return string HTML content.
|
---|
728 | */
|
---|
729 | function next_image_link($size = 'thumbnail', $text = false) {
|
---|
730 | adjacent_image_link(false, $size, $text);
|
---|
731 | }
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * Display next or previous image link that has the same post parent.
|
---|
735 | *
|
---|
736 | * Retrieves the current attachment object from the $post global.
|
---|
737 | *
|
---|
738 | * @since 2.5.0
|
---|
739 | *
|
---|
740 | * @param bool $prev Optional. Default is true to display previous link, true for next.
|
---|
741 | */
|
---|
742 | function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
|
---|
743 | global $post;
|
---|
744 | $post = get_post($post);
|
---|
745 | $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
|
---|
746 |
|
---|
747 | foreach ( $attachments as $k => $attachment )
|
---|
748 | if ( $attachment->ID == $post->ID )
|
---|
749 | break;
|
---|
750 |
|
---|
751 | $k = $prev ? $k - 1 : $k + 1;
|
---|
752 |
|
---|
753 | if ( isset($attachments[$k]) )
|
---|
754 | echo wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text);
|
---|
755 | }
|
---|
756 |
|
---|
757 | /**
|
---|
758 | * Retrieve taxonomies attached to the attachment.
|
---|
759 | *
|
---|
760 | * @since 2.5.0
|
---|
761 | *
|
---|
762 | * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
|
---|
763 | * @return array Empty array on failure. List of taxonomies on success.
|
---|
764 | */
|
---|
765 | function get_attachment_taxonomies($attachment) {
|
---|
766 | if ( is_int( $attachment ) )
|
---|
767 | $attachment = get_post($attachment);
|
---|
768 | else if ( is_array($attachment) )
|
---|
769 | $attachment = (object) $attachment;
|
---|
770 |
|
---|
771 | if ( ! is_object($attachment) )
|
---|
772 | return array();
|
---|
773 |
|
---|
774 | $filename = basename($attachment->guid);
|
---|
775 |
|
---|
776 | $objects = array('attachment');
|
---|
777 |
|
---|
778 | if ( false !== strpos($filename, '.') )
|
---|
779 | $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
|
---|
780 | if ( !empty($attachment->post_mime_type) ) {
|
---|
781 | $objects[] = 'attachment:' . $attachment->post_mime_type;
|
---|
782 | if ( false !== strpos($attachment->post_mime_type, '/') )
|
---|
783 | foreach ( explode('/', $attachment->post_mime_type) as $token )
|
---|
784 | if ( !empty($token) )
|
---|
785 | $objects[] = "attachment:$token";
|
---|
786 | }
|
---|
787 |
|
---|
788 | $taxonomies = array();
|
---|
789 | foreach ( $objects as $object )
|
---|
790 | if ( $taxes = get_object_taxonomies($object) )
|
---|
791 | $taxonomies = array_merge($taxonomies, $taxes);
|
---|
792 |
|
---|
793 | return array_unique($taxonomies);
|
---|
794 | }
|
---|
795 |
|
---|
796 | ?>
|
---|