~ubuntu-branches/debian/jessie/wordpress/jessie

« back to all changes in this revision

Viewing changes to wp-admin/includes/image.php

  • Committer: Package Import Robot
  • Author(s): Craig Small
  • Date: 2014-04-17 20:56:19 UTC
  • mfrom: (1.2.35)
  • Revision ID: package-import@ubuntu.com-20140417205619-nurbet6eho4yvwfv
Tags: 3.9+dfsg-1
* New upstream release
* 3.9 seems to handle different locations for plugins so the
  plugin directory handling patches have been cut back.

Show diffs side-by-side

added added

removed removed

Lines of Context:
97
97
                        else
98
98
                                $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
99
99
                        if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
100
 
                                $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
 
100
                                $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes
101
101
                        else
102
102
                                $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
103
103
                }
104
104
 
 
105
                /**
 
106
                 * Filter the image sizes automatically generated when uploading an image.
 
107
                 *
 
108
                 * @since 2.9.0
 
109
                 *
 
110
                 * @param array $sizes An associative array of image sizes.
 
111
                 */
105
112
                $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
106
113
 
107
114
                if ( $sizes ) {
120
127
 
121
128
        } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) {
122
129
                $metadata = wp_read_video_metadata( $file );
123
 
                $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' );
 
130
                $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
124
131
        } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) {
125
132
                $metadata = wp_read_audio_metadata( $file );
126
 
                $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' );
 
133
                $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
127
134
        }
128
135
 
129
136
        if ( $support && ! empty( $metadata['image']['data'] ) ) {
130
 
                $ext = '.jpg';
131
 
                switch ( $metadata['image']['mime'] ) {
132
 
                case 'image/gif':
133
 
                        $ext = '.gif';
134
 
                        break;
135
 
                case 'image/png':
136
 
                        $ext = '.png';
137
 
                        break;
138
 
                }
139
 
                $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
140
 
                $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
141
 
                if ( false === $uploaded['error'] ) {
142
 
                        $attachment = array(
143
 
                                'post_mime_type' => $metadata['image']['mime'],
144
 
                                'post_type' => 'attachment',
145
 
                                'post_content' => '',
146
 
                        );
147
 
                        $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] );
148
 
                        $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
149
 
                        wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
150
 
                        update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
 
137
                // check for existing cover
 
138
                $hash = md5( $metadata['image']['data'] );
 
139
                $posts = get_posts( array(
 
140
                        'fields' => 'ids',
 
141
                        'post_type' => 'attachment',
 
142
                        'post_mime_type' => $metadata['image']['mime'],
 
143
                        'post_status' => 'inherit',
 
144
                        'posts_per_page' => 1,
 
145
                        'meta_key' => '_cover_hash',
 
146
                        'meta_value' => $hash
 
147
                ) );
 
148
                $exists = reset( $posts );
 
149
 
 
150
                if ( ! empty( $exists ) ) {
 
151
                        update_post_meta( $attachment_id, '_thumbnail_id', $exists );
 
152
                } else {
 
153
                        $ext = '.jpg';
 
154
                        switch ( $metadata['image']['mime'] ) {
 
155
                        case 'image/gif':
 
156
                                $ext = '.gif';
 
157
                                break;
 
158
                        case 'image/png':
 
159
                                $ext = '.png';
 
160
                                break;
 
161
                        }
 
162
                        $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
 
163
                        $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
 
164
                        if ( false === $uploaded['error'] ) {
 
165
                                $image_attachment = array(
 
166
                                        'post_mime_type' => $metadata['image']['mime'],
 
167
                                        'post_type' => 'attachment',
 
168
                                        'post_content' => '',
 
169
                                );
 
170
                                /**
 
171
                                 * Filter the parameters for the attachment thumbnail creation.
 
172
                                 *
 
173
                                 * @since 3.9.0
 
174
                                 *
 
175
                                 * @param array $image_attachment An array of parameters to create the thumbnail.
 
176
                                 * @param array $metadata         Current attachment metadata.
 
177
                                 * @param array $uploaded         An array containing the thumbnail path and url.
 
178
                                 */
 
179
                                $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
 
180
 
 
181
                                $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] );
 
182
                                add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
 
183
                                $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
 
184
                                wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
 
185
                                update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
 
186
                        }
151
187
                }
152
188
        }
153
189
 
155
191
        if ( isset( $metadata['image']['data'] ) )
156
192
                unset( $metadata['image']['data'] );
157
193
 
 
194
        /**
 
195
         * Filter the generated attachment meta data.
 
196
         *
 
197
         * @since 2.1.0
 
198
         *
 
199
         * @param array $metadata      An array of attachment meta data.
 
200
         * @param int   $attachment_id Current attachment ID.
 
201
         */
158
202
        return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
159
203
}
160
204
 
268
312
                 }
269
313
        }
270
314
 
271
 
        // fetch additional info from exif if available
 
315
        /**
 
316
         * Filter the image types to check for exif data.
 
317
         *
 
318
         * @since 2.5.0
 
319
         *
 
320
         * @param array $image_types Image types to check for exif data.
 
321
         */
272
322
        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 ) ) ) ) {
273
323
                $exif = @exif_read_data( $file );
274
324
 
316
366
                        $meta[ $key ] = utf8_encode( $meta[ $key ] );
317
367
        }
318
368
 
 
369
        /**
 
370
         * Filter the array of meta data read from an image's exif data.
 
371
         *
 
372
         * @since 2.5.0
 
373
         *
 
374
         * @param array  $meta            Image meta data.
 
375
         * @param string $file            Path to image file.
 
376
         * @param int    $sourceImageType Type of image.
 
377
         */
319
378
        return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
320
379
 
321
380
}
337
396
 * Validate that file is suitable for displaying within a web page.
338
397
 *
339
398
 * @since 2.5.0
340
 
 * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
341
399
 *
342
400
 * @param string $path File path to test.
343
401
 * @return bool True if suitable, false if not suitable.
351
409
        else
352
410
                $result = true;
353
411
 
354
 
        return apply_filters('file_is_displayable_image', $result, $path);
 
412
        /**
 
413
         * Filter whether the current image is displayable in the browser.
 
414
         *
 
415
         * @since 2.5.0
 
416
         *
 
417
         * @param bool   $result Whether the image can be displayed. Default true.
 
418
         * @param string $path   Path to the image.
 
419
         */
 
420
        return apply_filters( 'file_is_displayable_image', $result, $path );
355
421
}
356
422
 
357
423
/**
384
450
                        break;
385
451
        }
386
452
        if ( is_resource($image) ) {
387
 
                $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
 
453
                /**
 
454
                 * Filter the current image being loaded for editing.
 
455
                 *
 
456
                 * @since 2.9.0
 
457
                 *
 
458
                 * @param resource $image         Current image.
 
459
                 * @param string   $attachment_id Attachment ID.
 
460
                 * @param string   $size          Image size.
 
461
                 */
 
462
                $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
388
463
                if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
389
464
                        imagealphablending($image, false);
390
465
                        imagesavealpha($image, true);
411
486
 
412
487
        if ( $filepath && file_exists( $filepath ) ) {
413
488
                if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
 
489
                        /**
 
490
                         * Filter the path to the current image.
 
491
                         *
 
492
                         * The filter is evaluated for all image sizes except 'full'.
 
493
                         *
 
494
                         * @since 3.1.0
 
495
                         *
 
496
                         * @param string $path          Path to the current image.
 
497
                         * @param string $attachment_id Attachment ID.
 
498
                         * @param string $size          Size of the image.
 
499
                         */
414
500
                        $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
415
501
                }
416
502
        } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
 
503
                /**
 
504
                 * Filter the image URL if not in the local filesystem.
 
505
                 *
 
506
                 * The filter is only evaluated if fopen is enabled on the server.
 
507
                 *
 
508
                 * @since 3.1.0
 
509
                 *
 
510
                 * @param string $image_url     Current image URL.
 
511
                 * @param string $attachment_id Attachment ID.
 
512
                 * @param string $size          Size of the image.
 
513
                 */
417
514
                $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
418
515
        }
419
516
 
 
517
        /**
 
518
         * Filter the returned path or URL of the current image.
 
519
         *
 
520
         * @since 2.9.0
 
521
         *
 
522
         * @param string|bool $filepath      File path or URL to current image, or false.
 
523
         * @param string      $attachment_id Attachment ID.
 
524
         * @param string      $size          Size of the image.
 
525
         */
420
526
        return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
421
527
}
422
528