Wordpress 3.6
[autoinstalls/wordpress.git] / wp-admin / includes / image.php
index c0fdfd9c231af8ea6f136f199c8119356a4dd283..f2e636bbb868ba787313551830240053c8b7ff88 100644 (file)
@@ -20,7 +20,7 @@
  * @param int $dst_h The destination height.
  * @param int $src_abs Optional. If the source crop points are absolute.
  * @param string $dst_file Optional. The destination file to write to.
- * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
+ * @return string|WP_Error New filepath on success, WP_Error on failure.
  */
 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
        $src_file = $src;
@@ -54,6 +54,9 @@ function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $s
        $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
 
        $result = $editor->save( $dst_file );
+       if ( is_wp_error( $result ) )
+               return $result;
+
        return $dst_file;
 }
 
@@ -70,6 +73,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
        $attachment = get_post( $attachment_id );
 
        $metadata = array();
+       $support = false;
        if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
                $imagesize = getimagesize( $file );
                $metadata['width'] = $imagesize[0];
@@ -81,6 +85,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
                // make thumbnails and other intermediate sizes
                global $_wp_additional_image_sizes;
 
+               $sizes = array();
                foreach ( get_intermediate_image_sizes() as $s ) {
                        $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
                        if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
@@ -113,7 +118,40 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
                if ( $image_meta )
                        $metadata['image_meta'] = $image_meta;
 
+       } 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' );
+       } 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' );
+       }
+
+       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 );
+               }
        }
+       // remove the blob of binary data from the array
+       unset( $metadata['image']['data'] );
 
        return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
 }