Support » Plugins and Hacks » Getting attachment width for medium size

Viewing 3 replies - 1 through 3 (of 3 total)
  • Matthew

    (@existentialmedia)

    There is a plugin for Indexhibit that I came across that works like this. In Indexhibit the image sizes are stored in the database when you upload them. The plugin Horizontal Format grabs the sizes from the db and adds them up.

    $pages = $OBJ->db->fetchArray("SELECT *
    	FROM ".PX."media, ".PX."objects_prefs
    	WHERE media_ref_id = '$rs[id]'
    	AND obj_ref_type = 'exhibit'
    	AND obj_ref_type = media_obj_type
    	ORDER BY media_order ASC, media_id ASC");
    foreach ($pages as $go)
    {
    	$title = ($go['media_title'] == '') ? '' : "<div class='title'>" . $go['media_title'] . "</div>";
    	$title .= ($go['media_caption'] == '') ? '' : "<div class='caption'>" . $go['media_caption'] . "</div>";
    
    	$temp_x = $go['media_x'] + $this->picture_block_padding_right;
    	$this->final_img_container += ($go['media_x'] + $this->picture_block_padding_right);
    
    	$a .= "<div class='picture_holder' style='width: {$temp_x}px;'>\n";
    	$a .= "<div class='picture' style='width: {$go[media_x]}px;'>\n";
    	$a .= "<img src='" . BASEURL . GIMGS . "/$go[media_file]' width='$go[media_x]' height='$go[media_y]' alt='" . BASEURL . GIMGS . "/$go[media_file]' />\n";
    	$a .= "<div class='captioning'>$title</div>\n";
    	$a .= "</div>\n";
    	$a .= "</div>\n\n";
    }
    
    $s .= "<div id='img-container'>\n";
    if ($rs['content'] != '') $s .= "<div id='text'>" . $rs['content'] . "</div>\n";
    $s .= $a;
    $s .= "<div style='clear: left;'><!-- --></div>";
    $s .= "</div>\n";
    
    return $s;
    }

    Maybe is there a way to do this with WP? I imagine that the image sizes are stored in the DB as well. This is maybe a better route than using getimagesize.

    Matthew

    (@existentialmedia)

    OKAY I FIGURED IT OUT.

    For those interested in this problem:

    // Scroll shortcode
    function scroll_shortcode(){
    	$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'menu_order') );
    	foreach ( $attachments as $attachment_id => $attachment ) {
    		$attachment_meta = wp_get_attachment_metadata($attachment_id);
    		$attachment_width = $attachment_meta['sizes']['medium']['width'] + 30;
    		$width += $attachment_width;
    	};
    	echo '<div class="scroll" style="width:'.$width.'px !important;">';
    
    foreach ( $attachments as $attachment_id => $attachment ) {
    	?><?php echo wp_get_attachment_link_canoe($attachment_id,$size='medium'); ?><?php
    }
    echo '</div>';
    }
    add_shortcode('scroll','scroll_shortcode');

    Matthew

    (@existentialmedia)

    Whoops, it needed a fallback if there is no medium size:

    // Scroll shortcode
    function scroll_shortcode(){
    	$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'menu_order') );
    	foreach ( $attachments as $attachment_id => $attachment ) {
    		$attachment_meta = wp_get_attachment_metadata($attachment_id);
    		if (isset($attachment_meta['sizes']['medium']['width'])) {
    			$attachment_width = $attachment_meta['sizes']['medium']['width'] + 30;
    			$scroll_width += $attachment_width;
    		} else {
    			$attachment_width = $attachment_meta['width'] + 30;
    			$scroll_width += $attachment_width;
    		}
    	};
    	echo '<div class="scroll" style="width:'.$scroll_width.'px !important;">';
    
    foreach ( $attachments as $attachment_id => $attachment ) {
    	?><?php echo wp_get_attachment_link_canoe($attachment_id,$size='medium'); ?><?php
    }
    echo '</div>';
    }
    add_shortcode('scroll','scroll_shortcode');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting attachment width for medium size’ is closed to new replies.