web/wp-admin/includes/image.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    47 	$src = wp_load_image( $src_file );
    47 	$src = wp_load_image( $src_file );
    48 
    48 
    49 	if ( !is_resource( $src ))
    49 	if ( !is_resource( $src ))
    50 		return $src;
    50 		return $src;
    51 
    51 
    52 	$dst = imagecreatetruecolor( $dst_w, $dst_h );
    52 	$dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
    53 
    53 
    54 	if ( $src_abs ) {
    54 	if ( $src_abs ) {
    55 		$src_w -= $src_x;
    55 		$src_w -= $src_x;
    56 		$src_h -= $src_y;
    56 		$src_h -= $src_y;
    57 	}
    57 	}
    73 	else
    73 	else
    74 		return false;
    74 		return false;
    75 }
    75 }
    76 
    76 
    77 /**
    77 /**
    78  * Generate post image attachment meta data.
    78  * Generate post thumbnail attachment meta data.
    79  *
    79  *
    80  * @since 2.1.0
    80  * @since 2.1.0
    81  *
    81  *
    82  * @param int $attachment_id Attachment Id to process.
    82  * @param int $attachment_id Attachment Id to process.
    83  * @param string $file Filepath of the Attached image.
    83  * @param string $file Filepath of the Attached image.
    86 function wp_generate_attachment_metadata( $attachment_id, $file ) {
    86 function wp_generate_attachment_metadata( $attachment_id, $file ) {
    87 	$attachment = get_post( $attachment_id );
    87 	$attachment = get_post( $attachment_id );
    88 
    88 
    89 	$metadata = array();
    89 	$metadata = array();
    90 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    90 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    91 		$full_path_file = $file;
    91 		$imagesize = getimagesize( $file );
    92 		$imagesize = getimagesize( $full_path_file );
       
    93 		$metadata['width'] = $imagesize[0];
    92 		$metadata['width'] = $imagesize[0];
    94 		$metadata['height'] = $imagesize[1];
    93 		$metadata['height'] = $imagesize[1];
    95 		list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
    94 		list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
    96 		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    95 		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    97 
    96 
    98 		// Make the file path relative to the upload dir
    97 		// Make the file path relative to the upload dir
    99 		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
    98 		$metadata['file'] = _wp_relative_upload_path($file);
   100 			if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
    99 
   101 				$file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
   100 		// make thumbnails and other intermediate sizes
   102 				$file = ltrim($file, '/');
   101 		global $_wp_additional_image_sizes;
   103 			}
   102 		$temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
       
   103 		if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
       
   104 			$temp_sizes = array_merge( $temp_sizes, array_keys( $_wp_additional_image_sizes ) );
       
   105 
       
   106 		$temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes );
       
   107 
       
   108 		foreach ( $temp_sizes as $s ) {
       
   109 			$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
       
   110 			if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
       
   111 				$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
       
   112 			else
       
   113 				$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
       
   114 			if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
       
   115 				$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
       
   116 			else
       
   117 				$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
       
   118 			if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
       
   119 				$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
       
   120 			else
       
   121 				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
   104 		}
   122 		}
   105 		$metadata['file'] = $file;
   123 
   106 
   124 		$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
   107 		// make thumbnails and other intermediate sizes
   125 
   108 		$sizes = array('thumbnail', 'medium', 'large');
   126 		foreach ($sizes as $size => $size_data ) {
   109 		$sizes = apply_filters('intermediate_image_sizes', $sizes);
   127 			$resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
   110 
       
   111 		foreach ($sizes as $size) {
       
   112 			$resized = image_make_intermediate_size( $full_path_file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
       
   113 			if ( $resized )
   128 			if ( $resized )
   114 				$metadata['sizes'][$size] = $resized;
   129 				$metadata['sizes'][$size] = $resized;
   115 		}
   130 		}
   116 
   131 
   117 		// fetch additional metadata from exif/iptc
   132 		// fetch additional metadata from exif/iptc
   118 		$image_meta = wp_read_image_metadata( $full_path_file );
   133 		$image_meta = wp_read_image_metadata( $file );
   119 		if ($image_meta)
   134 		if ( $image_meta )
   120 			$metadata['image_meta'] = $image_meta;
   135 			$metadata['image_meta'] = $image_meta;
   121 
   136 
   122 	}
   137 	}
   123 
   138 
   124 	return apply_filters( 'wp_generate_attachment_metadata', $metadata );
   139 	return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
   125 }
   140 }
   126 
   141 
   127 /**
   142 /**
   128  * Load an image from a string, if PHP supports it.
   143  * Load an image from a string, if PHP supports it.
   129  *
   144  *
   324 	else
   339 	else
   325 		$result = true;
   340 		$result = true;
   326 
   341 
   327 	return apply_filters('file_is_displayable_image', $result, $path);
   342 	return apply_filters('file_is_displayable_image', $result, $path);
   328 }
   343 }
   329 
       
   330 ?>