Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUGGESTION: add img size in "Image Size Panel " #11

Open
tomhung opened this issue Sep 11, 2024 · 0 comments
Open

SUGGESTION: add img size in "Image Size Panel " #11

tomhung opened this issue Sep 11, 2024 · 0 comments

Comments

@tomhung
Copy link

tomhung commented Sep 11, 2024

Here is my changes. Please review and incorp if you see fit.

<?php

namespace ASENHA\Classes;

/**
 * Class for Image Sizes Panel module
 *
 * @since 6.9.5
 */
class Image_Sizes_Panel {
    /**
     * Add image sizes meta box in image view/edit screen
     * 
     * @since 6.3.0
     */
    public function add_image_sizes_meta_box() {
        global $post;
        // Only add meta box if the attachment is an image
        if ( is_object( $post ) && property_exists( $post, 'post_mime_type' ) && false !== strpos( $post->post_mime_type, 'image' ) ) {
            add_meta_box(
                'image_sizes',
                'Image Sizes',
                [$this, 'image_sizes_table'],
                'attachment',
                'side'
            );
        }
    }

    /**
     * Output table of image sizes
     * 
     * @link https://plugins.trac.wordpress.org/browser/image-sizes-panel/tags/0.4/admin/admin.php
     * @since 6.3.0
     */
    public function image_sizes_table( $post ) {
        global $_wp_additional_image_sizes;
        $image_sizes = get_intermediate_image_sizes();
        $metadata = wp_get_attachment_metadata( $post->ID );
        $generated_sizes = array();
        // Merge defined image sizes with generated image sizes
        if ( isset( $metadata['sizes'] ) && count( $metadata['sizes'] ) > 0 ) {
            $generated_sizes = array_keys( $metadata['sizes'] );
            $image_sizes = array_unique( array_merge( $image_sizes, $generated_sizes ) );
        }
        $image_sizes[] = 'full';
        $full = wp_get_attachment_image_src( $post->ID, 'full' );
        sort( $image_sizes );
        if ( count( $image_sizes ) > 0 ) {
            echo '<table>';
            foreach ( $image_sizes as $size ) {
                $src = wp_get_attachment_image_src( $post->ID, $size );
                $size_path = get_attached_file( $post->ID ); // Get the full file path
                $file_size = 'full' === $size ? filesize($size_path) : filesize(dirname($size_path) . '/' . $metadata['sizes'][$size]['file']);
                $file_size_formatted = size_format($file_size); // Format the file size

                if ( isset( $metadata['sizes'][$size] ) ) {
                    $width = $metadata['sizes'][$size]['width'];
                    $height = $metadata['sizes'][$size]['height'];
                } else {
                    $width = $full[1];
                    $height = $full[2];
                }

                if ( in_array( $size, $generated_sizes ) || 'full' == $size ) {
                    echo '<tr id="image-sizes-panel-' . sanitize_html_class( $size ) . '" class="image-size-row">';
                    echo '<td class="size"><span class="name"><a href="' . esc_url( $src[0] ) . '" target="_blank" class="image-url">' . esc_html( $size ) . '</a></span></td>';
                    echo '<td class="dim">' . esc_html( $width ) . '&times;' . esc_html( $height ) . '&nbsp;' . str_replace(' ', '', esc_html( $file_size_formatted )) . '</td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
        } else {
            echo '<p>No image sizes</p>';
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant