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