wp_get_attachment_metadata example

if ( isset( $changes['alt'] ) ) {
        $alt = wp_unslash( $changes['alt'] );
        if ( get_post_meta( $id, '_wp_attachment_image_alt', true ) !== $alt ) {
            $alt = wp_strip_all_tags( $alt, true );
            update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) );
        }
    }

    if ( wp_attachment_is( 'audio', $post['ID'] ) ) {
        $changed = false;
        $id3data = wp_get_attachment_metadata( $post['ID'] );

        if ( ! is_array( $id3data ) ) {
            $changed = true;
            $id3data = array();
        }

        foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) {
            if ( isset( $changes[ $key ] ) ) {
                $changed         = true;
                $id3data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) );
            }
        }

    $out = apply_filters( 'image_downsize', false, $id$size );

    if ( $out ) {
        return $out;
    }

    $img_url          = wp_get_attachment_url( $id );
    $meta             = wp_get_attachment_metadata( $id );
    $width            = 0;
    $height           = 0;
    $is_intermediate  = false;
    $img_url_basename = wp_basename( $img_url );

    /* * If the file isn't an image, attempt to replace its URL with a rendered image from its meta. * Otherwise, a non-image type could be returned. */
    if ( ! $is_image ) {
        if ( ! empty( $meta['sizes']['full'] ) ) {
            

function prepend_attachment( $content ) {
    $post = get_post();

    if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) {
        return $content;
    }

    if ( wp_attachment_is( 'video', $post ) ) {
        $meta = wp_get_attachment_metadata( get_the_ID() );
        $atts = array( 'src' => wp_get_attachment_url() );
        if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
            $atts['width']  = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if ( has_post_thumbnail() ) {
            $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
        }
        $p = wp_video_shortcode( $atts );
    } elseif ( wp_attachment_is( 'audio', $post ) ) {
        $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
    }
if ( $instance['alt'] ) {
                $image_attributes['alt'] = $instance['alt'];
            }

            $size = $instance['size'];

            if ( 'custom' === $size || ! in_array( $sizearray_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
                $size  = array( $instance['width']$instance['height'] );
                $width = $instance['width'];
            } else {
                $caption_size = _wp_get_image_size_from_meta( $instance['size']wp_get_attachment_metadata( $attachment->ID ) );
                $width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
            }

            $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? implode( 'x', $size ) : $size );

            $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );

        } else {
            if ( empty( $instance['url'] ) ) {
                return;
            }

            
 else {
                    continue;
                }
            }

            // Unserialize values after checking for post symbols, so they can be properly referenced.             $value = maybe_unserialize( $value );

            // Handle header image as special case since setting has a legacy format.             if ( 'header_image' === $name ) {
                $name     = 'header_image_data';
                $metadata = wp_get_attachment_metadata( $value );
                if ( empty( $metadata ) ) {
                    continue;
                }
                $value = array(
                    'attachment_id' => $value,
                    'url'           => wp_get_attachment_url( $value ),
                    'height'        => $metadata['height'],
                    'width'         => $metadata['width'],
                );
            } elseif ( 'background_image' === $name ) {
                $value = wp_get_attachment_url( $value );
            }
function wp_get_attachment_thumb_file( $post_id = 0 ) {
    _deprecated_function( __FUNCTION__, '6.1.0' );

    $post_id = (int) $post_id;
    $post    = get_post( $post_id );

    if ( ! $post ) {
        return false;
    }

    // Use $post->ID rather than $post_id as get_post() may have used the global $post object.     $imagedata = wp_get_attachment_metadata( $post->ID );

    if ( ! is_array( $imagedata ) ) {
        return false;
    }

    $file = get_attached_file( $post->ID );

    if ( ! empty( $imagedata['thumb'] ) ) {
        $thumbfile = str_replace( wp_basename( $file )$imagedata['thumb']$file );
        if ( file_exists( $thumbfile ) ) {
            /** * Filters the attachment thumbnail file path. * * @since 2.1.0 * * @param string $thumbfile File path to the attachment thumbnail. * @param int $post_id Attachment ID. */

    $check = apply_filters( 'pre_delete_attachment', null, $post$force_delete );
    if ( null !== $check ) {
        return $check;
    }

    delete_post_meta( $post_id, '_wp_trash_meta_status' );
    delete_post_meta( $post_id, '_wp_trash_meta_time' );

    $meta         = wp_get_attachment_metadata( $post_id );
    $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
    $file         = get_attached_file( $post_id );

    if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) {
        clean_dirsize_cache( $file );
    }

    /** * Fires before an attachment is deleted, at the start of wp_delete_attachment(). * * @since 2.0.0 * @since 5.5.0 Added the `$post` parameter. * * @param int $post_id Attachment ID. * @param WP_Post $post Post object. */

    public function edit_media_item( $request ) {
        require_once ABSPATH . 'wp-admin/includes/image.php';

        $attachment_id = $request['id'];

        // This also confirms the attachment is an image.         $image_file = wp_get_original_image_path( $attachment_id );
        $image_meta = wp_get_attachment_metadata( $attachment_id );

        if (
            ! $image_meta ||
            ! $image_file ||
            ! wp_image_file_matches_image_meta( $request['src']$image_meta$attachment_id )
        ) {
            return new WP_Error(
                'rest_unknown_attachment',
                __( 'Unable to get meta information for file.' ),
                array( 'status' => 404 )
            );
        }
$keyed = '_format_' . $key;
        if ( isset( $post_data[ $keyed ] ) ) {
            if ( current_user_can( 'unfiltered_html' ) ) {
                update_post_meta( $post_id$keyed$post_data[ $keyed ] );
            } else {
                update_post_meta( $post_id$keyedwp_filter_post_kses( $post_data[ $keyed ] ) );
            }
        }
    }

    if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
        $id3data = wp_get_attachment_metadata( $post_id );
        if ( ! is_array( $id3data ) ) {
            $id3data = array();
        }

        foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
            if ( isset( $post_data[ 'id3_' . $key ] ) ) {
                $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
            }
        }
        wp_update_attachment_metadata( $post_id$id3data );
    }

    
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' /></div>";
            } else {
                $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
            }

            unset( $form_fields['menu_order'] );
            break;
        }
    }

    $media_dims = '';
    $meta       = wp_get_attachment_metadata( $post->ID );

    if ( isset( $meta['width']$meta['height'] ) ) {
        $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
    }

    /** * Filters the media metadata. * * @since 2.5.0 * * @param string $media_dims The HTML markup containing the media dimensions. * @param WP_Post $post The WP_Post attachment object. */

function wp_get_missing_image_subsizes( $attachment_id ) {
    if ( ! wp_attachment_is_image( $attachment_id ) ) {
        return array();
    }

    $registered_sizes = wp_get_registered_image_subsizes();
    $image_meta       = wp_get_attachment_metadata( $attachment_id );

    // Meta error?     if ( empty( $image_meta ) ) {
        return $registered_sizes;
    }

    // Use the originally uploaded image dimensions as full_width and full_height.     if ( ! empty( $image_meta['original_image'] ) ) {
        $image_file = wp_get_original_image_path( $attachment_id );
        $imagesize  = wp_getimagesize( $image_file );
    }

    

    protected function _prepare_media_item( $media_item$thumbnail_size = 'thumbnail' ) {
        $_media_item = array(
            'attachment_id'    => (string) $media_item->ID,
            'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
            'parent'           => $media_item->post_parent,
            'link'             => wp_get_attachment_url( $media_item->ID ),
            'title'            => $media_item->post_title,
            'caption'          => $media_item->post_excerpt,
            'description'      => $media_item->post_content,
            'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
            'type'             => $media_item->post_mime_type,
        );

        $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
        if ( $thumbnail_src ) {
            $_media_item['thumbnail'] = $thumbnail_src[0];
        } else {
            $_media_item['thumbnail'] = $_media_item['link'];
        }

        /** * Filters XML-RPC-prepared data for the given media item. * * @since 3.4.0 * * @param array $_media_item An array of media item data. * @param WP_Post $media_item Media item object. * @param string $thumbnail_size Image size. */
sprintf(
                        /* translators: %s: Post title. Only visible to screen readers. */
                        esc_html__( 'Edit %s', 'twentytwentyone' ),
                        '<span class="screen-reader-text">' . get_the_title() . '</span>'
                    ),
                    '<span class="edit-link">',
                    '</span>'
                );
            }

            // Retrieve attachment metadata.             $metadata = wp_get_attachment_metadata();
            if ( $metadata ) {
                printf(
                    '<span class="full-size-link"><span class="screen-reader-text">%1$s</span><a href="%2$s">%3$s &times; %4$s</a></span>',
                    /* translators: Hidden accessibility text. */
                    esc_html_x( 'Full size', 'Used before full size attachment link.', 'twentytwentyone' ), // phpcs:ignore WordPress.Security.EscapeOutput                     esc_url( wp_get_attachment_url() ),
                    absint( $metadata['width'] ),
                    absint( $metadata['height'] )
                );
            }

            
if ( isset( $attr['class'] ) && str_contains( $attr['class'], 'custom-logo' ) ) {
        return $attr;
    }

    $width  = false;
    $height = false;

    if ( is_array( $size ) ) {
        $width  = (int) $size[0];
        $height = (int) $size[1];
    } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
        $meta = wp_get_attachment_metadata( $attachment->ID );
        if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
            $width  = (int) $meta['width'];
            $height = (int) $meta['height'];
        }
    }

    if ( $width && $height ) {

        // Add style.         $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
        $attr['style'] = 'width:100%;height:' . round( 100 * $height / $width, 2 ) . '%;max-width:' . $width . 'px;' . $attr['style'];
    }

        $thumbnail_id = apply_filters( 'embed_thumbnail_id', $thumbnail_id );

        if ( $thumbnail_id ) {
            $aspect_ratio = 1;
            $measurements = array( 1, 1 );
            $image_size   = 'full'; // Fallback.
            $meta = wp_get_attachment_metadata( $thumbnail_id );
            if ( ! empty( $meta['sizes'] ) ) {
                foreach ( $meta['sizes'] as $size => $data ) {
                    if ( $data['height'] > 0 && $data['width'] / $data['height'] > $aspect_ratio ) {
                        $aspect_ratio = $data['width'] / $data['height'];
                        $measurements = array( $data['width']$data['height'] );
                        $image_size   = $size;
                    }
                }
            }

            /** * Filters the thumbnail image size for use in the embed template. * * @since 4.4.0 * @since 4.5.0 Added `$thumbnail_id` parameter. * * @param string $image_size Thumbnail image size. * @param int $thumbnail_id Attachment ID. */
Home | Imprint | This part of the site doesn't use cookies.