web/wp-includes/media.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    30  * @param int $height Height of the image
    30  * @param int $height Height of the image
    31  * @param string|array $size Size of what the result image should be.
    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.
    32  * @return array Width and height of what the result image should resize to.
    33  */
    33  */
    34 function image_constrain_size_for_editor($width, $height, $size = 'medium') {
    34 function image_constrain_size_for_editor($width, $height, $size = 'medium') {
    35 	global $content_width;
    35 	global $content_width, $_wp_additional_image_sizes;
    36 
    36 
    37 	if ( is_array($size) ) {
    37 	if ( is_array($size) ) {
    38 		$max_width = $size[0];
    38 		$max_width = $size[0];
    39 		$max_height = $size[1];
    39 		$max_height = $size[1];
    40 	}
    40 	}
    59 		// can resize it in the editor if they wish.
    59 		// can resize it in the editor if they wish.
    60 		$max_width = intval(get_option('large_size_w'));
    60 		$max_width = intval(get_option('large_size_w'));
    61 		$max_height = intval(get_option('large_size_h'));
    61 		$max_height = intval(get_option('large_size_h'));
    62 		if ( intval($content_width) > 0 )
    62 		if ( intval($content_width) > 0 )
    63 			$max_width = min( intval($content_width), $max_width );
    63 			$max_width = min( intval($content_width), $max_width );
       
    64 	} elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
       
    65 		$max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
       
    66 		$max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
       
    67 		if ( intval($content_width) > 0 )
       
    68 			$max_width = min( intval($content_width), $max_width );
    64 	}
    69 	}
    65 	// $size == 'full' has no constraint
    70 	// $size == 'full' has no constraint
    66 	else {
    71 	else {
    67 		$max_width = $width;
    72 		$max_width = $width;
    68 		$max_height = $height;
    73 		$max_height = $height;
   168 	return false;
   173 	return false;
   169 
   174 
   170 }
   175 }
   171 
   176 
   172 /**
   177 /**
       
   178  * Registers a new image size
       
   179  */
       
   180 function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) {
       
   181 	global $_wp_additional_image_sizes;
       
   182 	$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop );
       
   183 }
       
   184 
       
   185 /**
       
   186  * Registers an image size for the post thumbnail
       
   187  */
       
   188 function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) {
       
   189 	add_image_size( 'post-thumbnail', $width, $height, $crop );
       
   190 }
       
   191 
       
   192 /**
   173  * An <img src /> tag for an image attachment, scaling it down if requested.
   193  * An <img src /> tag for an image attachment, scaling it down if requested.
   174  *
   194  *
   175  * The filter 'get_image_tag_class' allows for changing the class name for the
   195  * 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
   196  * 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,
   197  * parameters are: what WordPress will use for the class, the Attachment ID,
   257  * @param int $dest_w New width.
   277  * @param int $dest_w New width.
   258  * @param int $dest_h New height.
   278  * @param int $dest_h New height.
   259  * @param bool $crop Optional, default is false. Whether to crop image or resize.
   279  * @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.
   280  * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function.
   261  */
   281  */
   262 function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
   282 function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
   263 
   283 
   264 	if ($orig_w <= 0 || $orig_h <= 0)
   284 	if ($orig_w <= 0 || $orig_h <= 0)
   265 		return false;
   285 		return false;
   266 	// at least one of dest_w or dest_h must be specific
   286 	// at least one of dest_w or dest_h must be specific
   267 	if ($dest_w <= 0 && $dest_h <= 0)
   287 	if ($dest_w <= 0 && $dest_h <= 0)
   270 	if ( $crop ) {
   290 	if ( $crop ) {
   271 		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
   291 		// 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;
   292 		$aspect_ratio = $orig_w / $orig_h;
   273 		$new_w = min($dest_w, $orig_w);
   293 		$new_w = min($dest_w, $orig_w);
   274 		$new_h = min($dest_h, $orig_h);
   294 		$new_h = min($dest_h, $orig_h);
   275 		if (!$new_w) {
   295 
       
   296 		if ( !$new_w ) {
   276 			$new_w = intval($new_h * $aspect_ratio);
   297 			$new_w = intval($new_h * $aspect_ratio);
   277 		}
   298 		}
   278 		if (!$new_h) {
   299 
       
   300 		if ( !$new_h ) {
   279 			$new_h = intval($new_w / $aspect_ratio);
   301 			$new_h = intval($new_w / $aspect_ratio);
   280 		}
   302 		}
   281 
   303 
   282 		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
   304 		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
   283 
   305 
   284 		$crop_w = ceil($new_w / $size_ratio);
   306 		$crop_w = round($new_w / $size_ratio);
   285 		$crop_h = ceil($new_h / $size_ratio);
   307 		$crop_h = round($new_h / $size_ratio);
   286 
   308 
   287 		$s_x = floor(($orig_w - $crop_w)/2);
   309 		$s_x = floor( ($orig_w - $crop_w) / 2 );
   288 		$s_y = floor(($orig_h - $crop_h)/2);
   310 		$s_y = floor( ($orig_h - $crop_h) / 2 );
   289 	}
   311 	} else {
   290 	else {
       
   291 		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
   312 		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
   292 		$crop_w = $orig_w;
   313 		$crop_w = $orig_w;
   293 		$crop_h = $orig_h;
   314 		$crop_h = $orig_h;
   294 
   315 
   295 		$s_x = 0;
   316 		$s_x = 0;
   297 
   318 
   298 		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
   319 		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
   299 	}
   320 	}
   300 
   321 
   301 	// if the resulting image would be the same size or larger we don't want to resize it
   322 	// 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)
   323 	if ( $new_w >= $orig_w && $new_h >= $orig_h )
   303 		return false;
   324 		return false;
   304 
   325 
   305 	// the return array matches the parameters to imagecopyresampled()
   326 	// 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
   327 	// 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);
   328 	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
   308 
   329 
   309 }
   330 }
   310 
   331 
   311 /**
   332 /**
   312  * Scale down an image to fit a particular size and save a new copy of the image.
   333  * Scale down an image to fit a particular size and save a new copy of the image.
   328  * @param string $suffix Optional. File Suffix.
   349  * @param string $suffix Optional. File Suffix.
   329  * @param string $dest_path Optional. New image file path.
   350  * @param string $dest_path Optional. New image file path.
   330  * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
   351  * @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()}
   352  * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
   332  */
   353  */
   333 function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
   354 function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
   334 
   355 
   335 	$image = wp_load_image( $file );
   356 	$image = wp_load_image( $file );
   336 	if ( !is_resource( $image ) )
   357 	if ( !is_resource( $image ) )
   337 		return new WP_Error('error_loading_image', $image);
   358 		return new WP_Error('error_loading_image', $image);
   338 
   359 
   339 	list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
   360 	$size = @getimagesize( $file );
       
   361 	if ( !$size )
       
   362 		return new WP_Error('invalid_image', __('Could not read image size'), $file);
       
   363 	list($orig_w, $orig_h, $orig_type) = $size;
       
   364 
   340 	$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
   365 	$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
   341 	if (!$dims)
   366 	if ( !$dims )
   342 		return $dims;
   367 		return $dims;
   343 	list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
   368 	list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
   344 
   369 
   345 	$newimage = imagecreatetruecolor( $dst_w, $dst_h);
   370 	$newimage = wp_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 
   371 
   353 	imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
   372 	imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
       
   373 
       
   374 	// convert from full colors to index colors, like original PNG.
       
   375 	if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) )
       
   376 		imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
   354 
   377 
   355 	// we don't need the original in memory anymore
   378 	// we don't need the original in memory anymore
   356 	imagedestroy( $image );
   379 	imagedestroy( $image );
   357 
   380 
   358 	// $suffix will be appended to the destination filename, just before the extension
   381 	// $suffix will be appended to the destination filename, just before the extension
   365 	$name = basename($file, ".{$ext}");
   388 	$name = basename($file, ".{$ext}");
   366 	if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
   389 	if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
   367 		$dir = $_dest_path;
   390 		$dir = $_dest_path;
   368 	$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
   391 	$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
   369 
   392 
   370 	if ( $orig_type == IMAGETYPE_GIF ) {
   393 	if ( IMAGETYPE_GIF == $orig_type ) {
   371 		if (!imagegif( $newimage, $destfilename ) )
   394 		if ( !imagegif( $newimage, $destfilename ) )
   372 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   395 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   373 	}
   396 	} elseif ( IMAGETYPE_PNG == $orig_type ) {
   374 	elseif ( $orig_type == IMAGETYPE_PNG ) {
   397 		if ( !imagepng( $newimage, $destfilename ) )
   375 		if (!imagepng( $newimage, $destfilename ) )
       
   376 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   398 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   377 	}
   399 	} else {
   378 	else {
       
   379 		// all other formats are converted to jpg
   400 		// all other formats are converted to jpg
   380 		$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
   401 		$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
   381 		if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
   402 		if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
   382 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   403 			return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
   383 	}
   404 	}
   384 
   405 
   385 	imagedestroy( $newimage );
   406 	imagedestroy( $newimage );
   386 
   407 
   525  * @param int $attachment_id Image attachment ID.
   546  * @param int $attachment_id Image attachment ID.
   526  * @param string $size Optional, default is 'thumbnail'.
   547  * @param string $size Optional, default is 'thumbnail'.
   527  * @param bool $icon Optional, default is false. Whether it is an icon.
   548  * @param bool $icon Optional, default is false. Whether it is an icon.
   528  * @return string HTML img element or empty string on failure.
   549  * @return string HTML img element or empty string on failure.
   529  */
   550  */
   530 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) {
   551 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
   531 
   552 
   532 	$html = '';
   553 	$html = '';
   533 	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
   554 	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
   534 	if ( $image ) {
   555 	if ( $image ) {
   535 		list($src, $width, $height) = $image;
   556 		list($src, $width, $height) = $image;
   536 		$hwstring = image_hwstring($width, $height);
   557 		$hwstring = image_hwstring($width, $height);
   537 		if ( is_array($size) )
   558 		if ( is_array($size) )
   538 			$size = join('x', $size);
   559 			$size = join('x', $size);
   539 		$attachment =& get_post($attachment_id);
   560 		$attachment =& get_post($attachment_id);
   540 		$attr = array(
   561 		$default_attr = array(
   541 			'src'	=> $src,
   562 			'src'	=> $src,
   542 			'class'	=> "attachment-$size",
   563 			'class'	=> "attachment-$size",
   543 			'alt'	=> trim(strip_tags( $attachment->post_excerpt )),
   564 			'alt'	=> trim(strip_tags( $attachment->post_excerpt )),
   544 			'title'	=> trim(strip_tags( $attachment->post_title )),
   565 			'title'	=> trim(strip_tags( $attachment->post_title )),
   545 			);
   566 		);
       
   567 		$attr = wp_parse_args($attr, $default_attr);
   546 		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
   568 		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
   547 		$attr = array_map( 'esc_attr', $attr );
   569 		$attr = array_map( 'esc_attr', $attr );
   548 		$html = rtrim("<img $hwstring");
   570 		$html = rtrim("<img $hwstring");
   549 		foreach ( $attr as $name => $value ) {
   571 		foreach ( $attr as $name => $value ) {
   550 			$html .= " $name=" . '"' . $value . '"';
   572 			$html .= " $name=" . '"' . $value . '"';
   551 		}
   573 		}
   552 		$html .= ' />';
   574 		$html .= ' />';
   553 	}
   575 	}
   554 
   576 
   555 	return $html;
   577 	return $html;
       
   578 }
       
   579 
       
   580 /**
       
   581  * Adds a 'wp-post-image' class to post thumbnail thumbnails
       
   582  * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to
       
   583  * dynamically add/remove itself so as to only filter post thumbnail thumbnails
       
   584  *
       
   585  * @author Mark Jaquith
       
   586  * @since 2.9.0
       
   587  * @param array $attr Attributes including src, class, alt, title
       
   588  * @return array
       
   589  */
       
   590 function _wp_post_thumbnail_class_filter( $attr ) {
       
   591 	$attr['class'] .= ' wp-post-image';
       
   592 	return $attr;
       
   593 }
       
   594 
       
   595 /**
       
   596  * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter
       
   597  *
       
   598  * @author Mark Jaquith
       
   599  * @since 2.9.0
       
   600  */
       
   601 function _wp_post_thumbnail_class_filter_add( $attr ) {
       
   602 	add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
       
   603 }
       
   604 
       
   605 /**
       
   606  * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter
       
   607  *
       
   608  * @author Mark Jaquith
       
   609  * @since 2.9.0
       
   610  */
       
   611 function _wp_post_thumbnail_class_filter_remove( $attr ) {
       
   612 	remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
   556 }
   613 }
   557 
   614 
   558 add_shortcode('wp_caption', 'img_caption_shortcode');
   615 add_shortcode('wp_caption', 'img_caption_shortcode');
   559 add_shortcode('caption', 'img_caption_shortcode');
   616 add_shortcode('caption', 'img_caption_shortcode');
   560 
   617 
   609  *
   666  *
   610  * @param array $attr Attributes attributed to the shortcode.
   667  * @param array $attr Attributes attributed to the shortcode.
   611  * @return string HTML content to display gallery.
   668  * @return string HTML content to display gallery.
   612  */
   669  */
   613 function gallery_shortcode($attr) {
   670 function gallery_shortcode($attr) {
   614 	global $post;
   671 	global $post, $wp_locale;
   615 
   672 
   616 	static $instance = 0;
   673 	static $instance = 0;
   617 	$instance++;
   674 	$instance++;
   618 
   675 
   619 	// Allow plugins/themes to override the default gallery template.
   676 	// Allow plugins/themes to override the default gallery template.
   634 		'id'         => $post->ID,
   691 		'id'         => $post->ID,
   635 		'itemtag'    => 'dl',
   692 		'itemtag'    => 'dl',
   636 		'icontag'    => 'dt',
   693 		'icontag'    => 'dt',
   637 		'captiontag' => 'dd',
   694 		'captiontag' => 'dd',
   638 		'columns'    => 3,
   695 		'columns'    => 3,
   639 		'size'       => 'thumbnail'
   696 		'size'       => 'thumbnail',
       
   697 		'include'    => '',
       
   698 		'exclude'    => ''
   640 	), $attr));
   699 	), $attr));
   641 
   700 
   642 	$id = intval($id);
   701 	$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) );
   702 	if ( 'RAND' == $order )
       
   703 		$orderby = 'none';
       
   704 
       
   705 	if ( !empty($include) ) {
       
   706 		$include = preg_replace( '/[^0-9,]+/', '', $include );
       
   707 		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
       
   708 
       
   709 		$attachments = array();
       
   710 		foreach ( $_attachments as $key => $val ) {
       
   711 			$attachments[$val->ID] = $_attachments[$key];
       
   712 		}
       
   713 	} elseif ( !empty($exclude) ) {
       
   714 		$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
       
   715 		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
       
   716 	} else {
       
   717 		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
       
   718 	}
   644 
   719 
   645 	if ( empty($attachments) )
   720 	if ( empty($attachments) )
   646 		return '';
   721 		return '';
   647 
   722 
   648 	if ( is_feed() ) {
   723 	if ( is_feed() ) {
   654 
   729 
   655 	$itemtag = tag_escape($itemtag);
   730 	$itemtag = tag_escape($itemtag);
   656 	$captiontag = tag_escape($captiontag);
   731 	$captiontag = tag_escape($captiontag);
   657 	$columns = intval($columns);
   732 	$columns = intval($columns);
   658 	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
   733 	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
   659 
   734 	$float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left'; 
       
   735 	
   660 	$selector = "gallery-{$instance}";
   736 	$selector = "gallery-{$instance}";
   661 
   737 
   662 	$output = apply_filters('gallery_style', "
   738 	$output = apply_filters('gallery_style', "
   663 		<style type='text/css'>
   739 		<style type='text/css'>
   664 			#{$selector} {
   740 			#{$selector} {
   665 				margin: auto;
   741 				margin: auto;
   666 			}
   742 			}
   667 			#{$selector} .gallery-item {
   743 			#{$selector} .gallery-item {
   668 				float: left;
   744 				float: {$float};
   669 				margin-top: 10px;
   745 				margin-top: 10px;
   670 				text-align: center;
   746 				text-align: center;
   671 				width: {$itemwidth}%;			}
   747 				width: {$itemwidth}%;			}
   672 			#{$selector} img {
   748 			#{$selector} img {
   673 				border: 2px solid #cfcfcf;
   749 				border: 2px solid #cfcfcf;
   791 			$taxonomies = array_merge($taxonomies, $taxes);
   867 			$taxonomies = array_merge($taxonomies, $taxes);
   792 
   868 
   793 	return array_unique($taxonomies);
   869 	return array_unique($taxonomies);
   794 }
   870 }
   795 
   871 
       
   872 /**
       
   873  * Check if the installed version of GD supports particular image type
       
   874  *
       
   875  * @since 2.9.0
       
   876  *
       
   877  * @param $mime_type string
       
   878  * @return bool
       
   879  */
       
   880 function gd_edit_image_support($mime_type) {
       
   881 	if ( function_exists('imagetypes') ) {
       
   882 		switch( $mime_type ) {
       
   883 			case 'image/jpeg':
       
   884 				return (imagetypes() & IMG_JPG) != 0;
       
   885 			case 'image/png':
       
   886 				return (imagetypes() & IMG_PNG) != 0;
       
   887 			case 'image/gif':
       
   888 				return (imagetypes() & IMG_GIF) != 0;
       
   889 		}
       
   890 	} else {
       
   891 		switch( $mime_type ) {
       
   892 			case 'image/jpeg':
       
   893 				return function_exists('imagecreatefromjpeg');
       
   894 			case 'image/png':
       
   895 				return function_exists('imagecreatefrompng');
       
   896 			case 'image/gif':
       
   897 				return function_exists('imagecreatefromgif');
       
   898 		}
       
   899 	}
       
   900 	return false;
       
   901 }
       
   902 
       
   903 /**
       
   904  * Create new GD image resource with transparency support
       
   905  *
       
   906  * @since 2.9.0
       
   907  *
       
   908  * @param $width
       
   909  * @param $height
       
   910  * @return image resource
       
   911  */
       
   912 function wp_imagecreatetruecolor($width, $height) {
       
   913 	$img = imagecreatetruecolor($width, $height);
       
   914 	if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
       
   915 		imagealphablending($img, false);
       
   916 		imagesavealpha($img, true);
       
   917 	}
       
   918 	return $img;
       
   919 }
       
   920 
       
   921 /**
       
   922  * API for easily embedding rich media such as videos and images into content.
       
   923  *
       
   924  * @package WordPress
       
   925  * @subpackage Embed
       
   926  * @since 2.9.0
       
   927  */
       
   928 class WP_Embed {
       
   929 	var $handlers = array();
       
   930 	var $post_ID;
       
   931 	var $usecache = true;
       
   932 	var $linkifunknown = true;
       
   933 
       
   934 	/**
       
   935 	 * PHP4 constructor
       
   936 	 */
       
   937 	function WP_Embed() {
       
   938 		return $this->__construct();
       
   939 	}
       
   940 
       
   941 	/**
       
   942 	 * PHP5 constructor
       
   943 	 */
       
   944 	function __construct() {
       
   945 		// Hack to get the [embed] shortcode to run before wpautop()
       
   946 		add_filter( 'the_content', array(&$this, 'run_shortcode'), 8 );
       
   947 
       
   948 		// Attempts to embed all URLs in a post
       
   949 		if ( get_option('embed_autourls') )
       
   950 			add_filter( 'the_content', array(&$this, 'autoembed'), 8 );
       
   951 
       
   952 		// After a post is saved, invalidate the oEmbed cache
       
   953 		add_action( 'save_post', array(&$this, 'delete_oembed_caches') );
       
   954 
       
   955 		// After a post is saved, cache oEmbed items via AJAX
       
   956 		add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') );
       
   957 	}
       
   958 
       
   959 	/**
       
   960 	 * Process the [embed] shortcode.
       
   961 	 *
       
   962 	 * Since the [embed] shortcode needs to be run earlier than other shortcodes,
       
   963 	 * this function removes all existing shortcodes, registers the [embed] shortcode,
       
   964 	 * calls {@link do_shortcode()}, and then re-registers the old shortcodes.
       
   965 	 *
       
   966 	 * @uses $shortcode_tags
       
   967 	 * @uses remove_all_shortcodes()
       
   968 	 * @uses add_shortcode()
       
   969 	 * @uses do_shortcode()
       
   970 	 *
       
   971 	 * @param string $content Content to parse
       
   972 	 * @return string Content with shortcode parsed
       
   973 	 */
       
   974 	function run_shortcode( $content ) {
       
   975 		global $shortcode_tags;
       
   976 
       
   977 		// Backup current registered shortcodes and clear them all out
       
   978 		$orig_shortcode_tags = $shortcode_tags;
       
   979 		remove_all_shortcodes();
       
   980 
       
   981 		add_shortcode( 'embed', array(&$this, 'shortcode') );
       
   982 
       
   983 		// Do the shortcode (only the [embed] one is registered)
       
   984 		$content = do_shortcode( $content );
       
   985 
       
   986 		// Put the original shortcodes back
       
   987 		$shortcode_tags = $orig_shortcode_tags;
       
   988 
       
   989 		return $content;
       
   990 	}
       
   991 
       
   992 	/**
       
   993 	 * If a post/page was saved, then output Javascript to make
       
   994 	 * an AJAX request that will call WP_Embed::cache_oembed().
       
   995 	 */
       
   996 	function maybe_run_ajax_cache() {
       
   997 		global $post_ID;
       
   998 
       
   999 		if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] )
       
  1000 			return;
       
  1001 
   796 ?>
  1002 ?>
       
  1003 <script type="text/javascript">
       
  1004 /* <![CDATA[ */
       
  1005 	jQuery(document).ready(function($){
       
  1006 		$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post_ID ); ?>");
       
  1007 	});
       
  1008 /* ]]> */
       
  1009 </script>
       
  1010 <?php
       
  1011 	}
       
  1012 
       
  1013 	/**
       
  1014 	 * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.
       
  1015 	 * This function should probably also only be used for sites that do not support oEmbed.
       
  1016 	 *
       
  1017 	 * @param string $id An internal ID/name for the handler. Needs to be unique.
       
  1018 	 * @param string $regex The regex that will be used to see if this handler should be used for a URL.
       
  1019 	 * @param callback $callback The callback function that will be called if the regex is matched.
       
  1020 	 * @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action.
       
  1021 	 */
       
  1022 	function register_handler( $id, $regex, $callback, $priority = 10 ) {
       
  1023 		$this->handlers[$priority][$id] = array(
       
  1024 			'regex'    => $regex,
       
  1025 			'callback' => $callback,
       
  1026 		);
       
  1027 	}
       
  1028 
       
  1029 	/**
       
  1030 	 * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
       
  1031 	 *
       
  1032 	 * @param string $id The handler ID that should be removed.
       
  1033 	 * @param int $priority Optional. The priority of the handler to be removed (default: 10).
       
  1034 	 */
       
  1035 	function unregister_handler( $id, $priority = 10 ) {
       
  1036 		if ( isset($this->handlers[$priority][$id]) )
       
  1037 			unset($this->handlers[$priority][$id]);
       
  1038 	}
       
  1039 
       
  1040 	/**
       
  1041 	 * The {@link do_shortcode()} callback function.
       
  1042 	 *
       
  1043 	 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
       
  1044 	 * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.
       
  1045 	 *
       
  1046 	 * @uses wp_oembed_get()
       
  1047 	 * @uses wp_parse_args()
       
  1048 	 * @uses wp_embed_defaults()
       
  1049 	 * @uses WP_Embed::maybe_make_link()
       
  1050 	 * @uses get_option()
       
  1051 	 * @uses current_user_can()
       
  1052 	 * @uses wp_cache_get()
       
  1053 	 * @uses wp_cache_set()
       
  1054 	 * @uses get_post_meta()
       
  1055 	 * @uses update_post_meta()
       
  1056 	 *
       
  1057 	 * @param array $attr Shortcode attributes.
       
  1058 	 * @param string $url The URL attempting to be embeded.
       
  1059 	 * @return string The embed HTML on success, otherwise the original URL.
       
  1060 	 */
       
  1061 	function shortcode( $attr, $url = '' ) {
       
  1062 		global $post;
       
  1063 
       
  1064 		if ( empty($url) )
       
  1065 			return '';
       
  1066 
       
  1067 		$rawattr = $attr;
       
  1068 		$attr = wp_parse_args( $attr, wp_embed_defaults() );
       
  1069 
       
  1070 		// Look for known internal handlers
       
  1071 		ksort( $this->handlers );
       
  1072 		foreach ( $this->handlers as $priority => $handlers ) {
       
  1073 			foreach ( $handlers as $id => $handler ) {
       
  1074 				if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
       
  1075 					if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
       
  1076 						return apply_filters( 'embed_handler_html', $return, $url, $attr );
       
  1077 				}
       
  1078 			}
       
  1079 		}
       
  1080 
       
  1081 		$post_ID = ( !empty($post->ID) ) ? $post->ID : null;
       
  1082 		if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed()
       
  1083 			$post_ID = $this->post_ID;
       
  1084 
       
  1085 		// Unknown URL format. Let oEmbed have a go.
       
  1086 		if ( $post_ID ) {
       
  1087 
       
  1088 			// Check for a cached result (stored in the post meta)
       
  1089 			$cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
       
  1090 			if ( $this->usecache ) {
       
  1091 				$cache = get_post_meta( $post_ID, $cachekey, true );
       
  1092 
       
  1093 				// Failures are cached
       
  1094 				if ( '{{unknown}}' === $cache )
       
  1095 					return $this->maybe_make_link( $url );
       
  1096 
       
  1097 				if ( !empty($cache) )
       
  1098 					return apply_filters( 'embed_oembed_html', $cache, $url, $attr );
       
  1099 			}
       
  1100 
       
  1101 			// Use oEmbed to get the HTML
       
  1102 			$attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) ) ? true : false;
       
  1103 			$html = wp_oembed_get( $url, $attr );
       
  1104 
       
  1105 			// Cache the result
       
  1106 			$cache = ( $html ) ? $html : '{{unknown}}';
       
  1107 			update_post_meta( $post_ID, $cachekey, $cache );
       
  1108 
       
  1109 			// If there was a result, return it
       
  1110 			if ( $html )
       
  1111 				return apply_filters( 'embed_oembed_html', $html, $url, $attr );
       
  1112 		}
       
  1113 
       
  1114 		// Still unknown
       
  1115 		return $this->maybe_make_link( $url );
       
  1116 	}
       
  1117 
       
  1118 	/**
       
  1119 	 * Delete all oEmbed caches.
       
  1120 	 *
       
  1121 	 * @param int $post_ID Post ID to delete the caches for.
       
  1122 	 */
       
  1123 	function delete_oembed_caches( $post_ID ) {
       
  1124 		$post_metas = get_post_custom_keys( $post_ID );
       
  1125 		if ( empty($post_metas) )
       
  1126 			return;
       
  1127 
       
  1128 		foreach( $post_metas as $post_meta_key ) {
       
  1129 			if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
       
  1130 				delete_post_meta( $post_ID, $post_meta_key );
       
  1131 		}
       
  1132 	}
       
  1133 
       
  1134 	/**
       
  1135 	 * Triggers a caching of all oEmbed results.
       
  1136 	 *
       
  1137 	 * @param int $post_ID Post ID to do the caching for.
       
  1138 	 */
       
  1139 	function cache_oembed( $post_ID ) {
       
  1140 		$post = get_post( $post_ID );
       
  1141 
       
  1142 		if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
       
  1143 			return;
       
  1144 
       
  1145 		// Trigger a caching
       
  1146 		if ( !empty($post->post_content) ) {
       
  1147 			$this->post_ID = $post->ID;
       
  1148 			$this->usecache = false;
       
  1149 
       
  1150 			$content = $this->run_shortcode( $post->post_content );
       
  1151 			if ( get_option('embed_autourls') )
       
  1152 				$this->autoembed( $content );
       
  1153 
       
  1154 			$this->usecache = true;
       
  1155 		}
       
  1156 	}
       
  1157 
       
  1158 	/**
       
  1159 	 * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
       
  1160 	 *
       
  1161 	 * @uses WP_Embed::autoembed_callback()
       
  1162 	 *
       
  1163 	 * @param string $content The content to be searched.
       
  1164 	 * @return string Potentially modified $content.
       
  1165 	 */
       
  1166 	function autoembed( $content ) {
       
  1167 		return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array(&$this, 'autoembed_callback'), $content );
       
  1168 	}
       
  1169 
       
  1170 	/**
       
  1171 	 * Callback function for {@link WP_Embed::autoembed()}.
       
  1172 	 *
       
  1173 	 * @uses WP_Embed::shortcode()
       
  1174 	 *
       
  1175 	 * @param array $match A regex match array.
       
  1176 	 * @return string The embed HTML on success, otherwise the original URL.
       
  1177 	 */
       
  1178 	function autoembed_callback( $match ) {
       
  1179 		$oldval = $this->linkifunknown;
       
  1180 		$this->linkifunknown = false;
       
  1181 		$return = $this->shortcode( array(), $match[1] );
       
  1182 		$this->linkifunknown = $oldval;
       
  1183 
       
  1184 		return "\n$return\n";
       
  1185 	}
       
  1186 
       
  1187 	/**
       
  1188 	 * Conditionally makes a hyperlink based on an internal class variable.
       
  1189 	 *
       
  1190 	 * @param string $url URL to potentially be linked.
       
  1191 	 * @return string Linked URL or the original URL.
       
  1192 	 */
       
  1193 	function maybe_make_link( $url ) {
       
  1194 		$output = ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
       
  1195 		return apply_filters( 'embed_maybe_make_link', $output, $url );
       
  1196 	}
       
  1197 }
       
  1198 $wp_embed = new WP_Embed();
       
  1199 
       
  1200 /**
       
  1201  * Register an embed handler. This function should probably only be used for sites that do not support oEmbed.
       
  1202  *
       
  1203  * @since 2.9.0
       
  1204  * @see WP_Embed::register_handler()
       
  1205  */
       
  1206 function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
       
  1207 	global $wp_embed;
       
  1208 	$wp_embed->register_handler( $id, $regex, $callback, $priority );
       
  1209 }
       
  1210 
       
  1211 /**
       
  1212  * Unregister a previously registered embed handler.
       
  1213  *
       
  1214  * @since 2.9.0
       
  1215  * @see WP_Embed::unregister_handler()
       
  1216  */
       
  1217 function wp_embed_unregister_handler( $id, $priority = 10 ) {
       
  1218 	global $wp_embed;
       
  1219 	$wp_embed->unregister_handler( $id, $priority );
       
  1220 }
       
  1221 
       
  1222 /**
       
  1223  * Create default array of embed parameters.
       
  1224  *
       
  1225  * @since 2.9.0
       
  1226  *
       
  1227  * @return array Default embed parameters.
       
  1228  */
       
  1229 function wp_embed_defaults() {
       
  1230 	if ( !empty($GLOBALS['content_width']) )
       
  1231 		$theme_width = (int) $GLOBALS['content_width'];
       
  1232 
       
  1233 	$width = get_option('embed_size_w');
       
  1234 
       
  1235 	if ( !$width && !empty($theme_width) )
       
  1236 		$width = $theme_width;
       
  1237 
       
  1238 	if ( !$width )
       
  1239 		$width = 500;
       
  1240 
       
  1241 	return apply_filters( 'embed_defaults', array(
       
  1242 		'width' => $width,
       
  1243 		'height' => 700,
       
  1244 	) );
       
  1245 }
       
  1246 
       
  1247 /**
       
  1248  * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
       
  1249  *
       
  1250  * @since 2.9.0
       
  1251  * @uses wp_constrain_dimensions() This function passes the widths and the heights.
       
  1252  *
       
  1253  * @param int $example_width The width of an example embed.
       
  1254  * @param int $example_height The height of an example embed.
       
  1255  * @param int $max_width The maximum allowed width.
       
  1256  * @param int $max_height The maximum allowed height.
       
  1257  * @return array The maximum possible width and height based on the example ratio.
       
  1258  */
       
  1259 function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
       
  1260 	$example_width  = (int) $example_width;
       
  1261 	$example_height = (int) $example_height;
       
  1262 	$max_width      = (int) $max_width;
       
  1263 	$max_height     = (int) $max_height;
       
  1264 
       
  1265 	return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
       
  1266 }
       
  1267 
       
  1268 /**
       
  1269  * Attempts to fetch the embed HTML for a provided URL using oEmbed.
       
  1270  *
       
  1271  * @since 2.9.0
       
  1272  * @see WP_oEmbed
       
  1273  *
       
  1274  * @uses _wp_oembed_get_object()
       
  1275  * @uses WP_oEmbed::get_html()
       
  1276  *
       
  1277  * @param string $url The URL that should be embeded.
       
  1278  * @param array $args Addtional arguments and parameters.
       
  1279  * @return string The original URL on failure or the embed HTML on success.
       
  1280  */
       
  1281 function wp_oembed_get( $url, $args = '' ) {
       
  1282 	require_once( 'class-oembed.php' );
       
  1283 	$oembed = _wp_oembed_get_object();
       
  1284 	return $oembed->get_html( $url, $args );
       
  1285 }
       
  1286 
       
  1287 /**
       
  1288  * Adds a URL format and oEmbed provider URL pair.
       
  1289  *
       
  1290  * @since 2.9.0
       
  1291  * @see WP_oEmbed
       
  1292  *
       
  1293  * @uses _wp_oembed_get_object()
       
  1294  *
       
  1295  * @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards.
       
  1296  * @param string $provider The URL to the oEmbed provider.
       
  1297  * @param boolean $regex Whether the $format parameter is in a regex format or not.
       
  1298  */
       
  1299 function wp_oembed_add_provider( $format, $provider, $regex = false ) {
       
  1300 	require_once( 'class-oembed.php' );
       
  1301 	$oembed = _wp_oembed_get_object();
       
  1302 	$oembed->providers[$format] = array( $provider, $regex );
       
  1303 }