phpwoocommercedimensionsgetimagesize

woocommerce get image size


I'm trying to get the dimensions (size) of all images related to a product on the single product page in woocommerce. I need the value width and height for photoswipes "data-size" values.

This is what I have now

<ul class="slides">
<?php
    $attachment_ids = $product->get_gallery_attachment_ids();
    $attachment_first[0] = get_post_thumbnail_id( $product->id );
    $attachment = wp_get_attachment_image_src( $attachment_first[0], 'full' );
    $img_size = wc_get_image_size( $attachment_ids, 'full' );
    $w = $img_size['width'];
    $h = $img_size['height'];
    $size = $w .'x'. $h;
?>
    <li class="picture">
        <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
            <a href="<?php echo $attachment[0]; ?>" itemprop="contentUrl" data-size="<?php echo $size; ?>">
                <img src="<?php echo $attachment[0]; ?>" itemprop="image" />
                <?php echo $size; ?>
            </a>
        </figure>
    </li>
<?php
    foreach( $attachment_ids as $attachment_id ){
        $src_url = wp_get_attachment_url( $attachment_id );
        echo '<li class="picture"><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="' . $src_url . '" itemprop="contentUrl" data-size="' . $size . '"><img src="' . $src_url . '" itemprop="image" />'.$size .'</a></figure></li>';
    }
?>
</ul>

It gets all the images correctly, but the width and height value are off. I know that the full images for the product I'm testing on are 800x800. If I remove "$attachment_ids" in

wc_get_image_size( $attachment_ids, 'full' );

Then the value becomes 300x300.

I really need to find a simple way to get the actual dimensions of the full sized images from woocommerce. I'd thought that this task would have been simpler :P


Solution

  • Nwm, solved it with this (for anyone else searching on this)

    <ul class="slides">
    <?php
        $attachment_ids = $product->get_gallery_attachment_ids();
        $attachment_first[0] = get_post_thumbnail_id( $product->id );
        $attachment = wp_get_attachment_image_src( $attachment_first[0], 'full' );
        $w = $attachment[1];
        $h = $attachment[2];
        $size = $w .'x'. $h;
    ?>
        <li class="picture">
            <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
                <a href="<?php echo $attachment[0]; ?>" itemprop="contentUrl" data-size="<?php echo $size; ?>">
                    <img src="<?php echo $attachment[0]; ?>" itemprop="image" />
                </a>
            </figure>
        </li>
    <?php
        foreach( $attachment_ids as $attachment_id ){
            $src_url = wp_get_attachment_url( $attachment_id );
            $attachments = wp_get_attachment_image_src( $attachment_id, 'full' );
            $wp = $attachments[1];
            $hp = $attachments[2];
            $sizes = $wp .'x'. $hp;
            echo '<li class="picture"><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="' . $src_url . '" itemprop="contentUrl" data-size="' . $sizes . '"><img src="' . $src_url . '" itemprop="image" /></a></figure></li>';
        }
    ?>
    </ul>