]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/includes/image.php
WordPress 3.9
[autoinstalls/wordpress.git] / wp-admin / includes / image.php
index 61f527a569581cbe99fe3ac254f8d590e8d2b82e..5799d28e6234c26ca42d38f10888404fe5185002 100644 (file)
@@ -97,11 +97,18 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
                        else
                                $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
                        if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
-                               $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
+                               $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes
                        else
                                $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
                }
 
+               /**
+                * Filter the image sizes automatically generated when uploading an image.
+                *
+                * @since 2.9.0
+                *
+                * @param array $sizes An associative array of image sizes.
+                */
                $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
 
                if ( $sizes ) {
@@ -120,34 +127,63 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
 
        } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) {
                $metadata = wp_read_video_metadata( $file );
-               $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' );
+               $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
        } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) {
                $metadata = wp_read_audio_metadata( $file );
-               $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' );
+               $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
        }
 
        if ( $support && ! empty( $metadata['image']['data'] ) ) {
-               $ext = '.jpg';
-               switch ( $metadata['image']['mime'] ) {
-               case 'image/gif':
-                       $ext = '.gif';
-                       break;
-               case 'image/png':
-                       $ext = '.png';
-                       break;
-               }
-               $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
-               $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
-               if ( false === $uploaded['error'] ) {
-                       $attachment = array(
-                               'post_mime_type' => $metadata['image']['mime'],
-                               'post_type' => 'attachment',
-                               'post_content' => '',
-                       );
-                       $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] );
-                       $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
-                       wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
-                       update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
+               // check for existing cover
+               $hash = md5( $metadata['image']['data'] );
+               $posts = get_posts( array(
+                       'fields' => 'ids',
+                       'post_type' => 'attachment',
+                       'post_mime_type' => $metadata['image']['mime'],
+                       'post_status' => 'inherit',
+                       'posts_per_page' => 1,
+                       'meta_key' => '_cover_hash',
+                       'meta_value' => $hash
+               ) );
+               $exists = reset( $posts );
+
+               if ( ! empty( $exists ) ) {
+                       update_post_meta( $attachment_id, '_thumbnail_id', $exists );
+               } else {
+                       $ext = '.jpg';
+                       switch ( $metadata['image']['mime'] ) {
+                       case 'image/gif':
+                               $ext = '.gif';
+                               break;
+                       case 'image/png':
+                               $ext = '.png';
+                               break;
+                       }
+                       $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
+                       $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
+                       if ( false === $uploaded['error'] ) {
+                               $image_attachment = array(
+                                       'post_mime_type' => $metadata['image']['mime'],
+                                       'post_type' => 'attachment',
+                                       'post_content' => '',
+                               );
+                               /**
+                                * Filter the parameters for the attachment thumbnail creation.
+                                *
+                                * @since 3.9.0
+                                *
+                                * @param array $image_attachment An array of parameters to create the thumbnail.
+                                * @param array $metadata         Current attachment metadata.
+                                * @param array $uploaded         An array containing the thumbnail path and url.
+                                */
+                               $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
+
+                               $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] );
+                               add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
+                               $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
+                               wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
+                               update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
+                       }
                }
        }
 
@@ -155,6 +191,14 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
        if ( isset( $metadata['image']['data'] ) )
                unset( $metadata['image']['data'] );
 
+       /**
+        * Filter the generated attachment meta data.
+        *
+        * @since 2.1.0
+        *
+        * @param array $metadata      An array of attachment meta data.
+        * @param int   $attachment_id Current attachment ID.
+        */
        return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
 }
 
@@ -268,7 +312,13 @@ function wp_read_image_metadata( $file ) {
                 }
        }
 
-       // fetch additional info from exif if available
+       /**
+        * Filter the image types to check for exif data.
+        *
+        * @since 2.5.0
+        *
+        * @param array $image_types Image types to check for exif data.
+        */
        if ( is_callable( 'exif_read_data' ) && in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) {
                $exif = @exif_read_data( $file );
 
@@ -316,6 +366,15 @@ function wp_read_image_metadata( $file ) {
                        $meta[ $key ] = utf8_encode( $meta[ $key ] );
        }
 
+       /**
+        * Filter the array of meta data read from an image's exif data.
+        *
+        * @since 2.5.0
+        *
+        * @param array  $meta            Image meta data.
+        * @param string $file            Path to image file.
+        * @param int    $sourceImageType Type of image.
+        */
        return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
 
 }
@@ -337,7 +396,6 @@ function file_is_valid_image($path) {
  * Validate that file is suitable for displaying within a web page.
  *
  * @since 2.5.0
- * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
  *
  * @param string $path File path to test.
  * @return bool True if suitable, false if not suitable.
@@ -351,7 +409,15 @@ function file_is_displayable_image($path) {
        else
                $result = true;
 
-       return apply_filters('file_is_displayable_image', $result, $path);
+       /**
+        * Filter whether the current image is displayable in the browser.
+        *
+        * @since 2.5.0
+        *
+        * @param bool   $result Whether the image can be displayed. Default true.
+        * @param string $path   Path to the image.
+        */
+       return apply_filters( 'file_is_displayable_image', $result, $path );
 }
 
 /**
@@ -384,7 +450,16 @@ function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
                        break;
        }
        if ( is_resource($image) ) {
-               $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
+               /**
+                * Filter the current image being loaded for editing.
+                *
+                * @since 2.9.0
+                *
+                * @param resource $image         Current image.
+                * @param string   $attachment_id Attachment ID.
+                * @param string   $size          Image size.
+                */
+               $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
                if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
                        imagealphablending($image, false);
                        imagesavealpha($image, true);
@@ -411,12 +486,43 @@ function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
 
        if ( $filepath && file_exists( $filepath ) ) {
                if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
+                       /**
+                        * Filter the path to the current image.
+                        *
+                        * The filter is evaluated for all image sizes except 'full'.
+                        *
+                        * @since 3.1.0
+                        *
+                        * @param string $path          Path to the current image.
+                        * @param string $attachment_id Attachment ID.
+                        * @param string $size          Size of the image.
+                        */
                        $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
                }
        } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
+               /**
+                * Filter the image URL if not in the local filesystem.
+                *
+                * The filter is only evaluated if fopen is enabled on the server.
+                *
+                * @since 3.1.0
+                *
+                * @param string $image_url     Current image URL.
+                * @param string $attachment_id Attachment ID.
+                * @param string $size          Size of the image.
+                */
                $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
        }
 
+       /**
+        * Filter the returned path or URL of the current image.
+        *
+        * @since 2.9.0
+        *
+        * @param string|bool $filepath      File path or URL to current image, or false.
+        * @param string      $attachment_id Attachment ID.
+        * @param string      $size          Size of the image.
+        */
        return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
 }