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' );
129
136
if ( $support && ! empty( $metadata['image']['data'] ) ) {
131
switch ( $metadata['image']['mime'] ) {
139
$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
140
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
141
if ( false === $uploaded['error'] ) {
143
'post_mime_type' => $metadata['image']['mime'],
144
'post_type' => 'attachment',
145
'post_content' => '',
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(
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
148
$exists = reset( $posts );
150
if ( ! empty( $exists ) ) {
151
update_post_meta( $attachment_id, '_thumbnail_id', $exists );
154
switch ( $metadata['image']['mime'] ) {
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' => '',
171
* Filter the parameters for the attachment thumbnail creation.
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.
179
$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
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 );
386
452
if ( is_resource($image) ) {
387
$image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
454
* Filter the current image being loaded for editing.
458
* @param resource $image Current image.
459
* @param string $attachment_id Attachment ID.
460
* @param string $size Image size.
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);
412
487
if ( $filepath && file_exists( $filepath ) ) {
413
488
if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
490
* Filter the path to the current image.
492
* The filter is evaluated for all image sizes except 'full'.
496
* @param string $path Path to the current image.
497
* @param string $attachment_id Attachment ID.
498
* @param string $size Size of the image.
414
500
$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
416
502
} elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
504
* Filter the image URL if not in the local filesystem.
506
* The filter is only evaluated if fopen is enabled on the server.
510
* @param string $image_url Current image URL.
511
* @param string $attachment_id Attachment ID.
512
* @param string $size Size of the image.
417
514
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
518
* Filter the returned path or URL of the current image.
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.
420
526
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );