dlanger/simple-slideshow — Issue #4: Fix for missing dimensions
Author: kitchin
Created: 2012-01-06 13:29:01+00:00
State: open

The code at line 104 in simpleslider.php ver. 1.2 gets the image width and height from the metadata. But the chosen $size, such as 'medium', may not exist in the metadata. It is better to get the dimensions from wp_get_attachment_image_src(), which is called later anyway.

For instance, on my site I uploaded images 180x233, 174x233, 147x233. With default WP settings, those numbers are stored at the top level of the meta, and there is only one specific $size, 'thumbnail'. So the existing code at line 104 results in the div having style="width: 0px; height: 0px;". This makes for confusing formatting.

Simple fix:

```php
$image_data ) {
-	$info = wp_get_attachment_metadata( $image_id );
-	$thumb_w = max ( $thumb_w, $info[ 'sizes' ][ $size ][ 'width' ] );
-	$thumb_h = max ( $thumb_h, $info[ 'sizes' ][ $size ][ 'height' ] );
$captions[ $image_id ] = $image_data->post_excerpt;
+
+	// Metadata may not have the chosen size $size
+	// (missing $info[ 'sizes' ][ $size ][ 'width' ] and [ 'height' ]),
+	// which means the main image will be used
+	// (with dimensions $info[ 'width' ] and [ 'height' ]).
+
+	// Rather than check both parts of the metadata,
+	// the dimension of the image that will be used
+	// can be found by using the call to wp_get_attachment_image_src().
+	// Cache the call in $image_props.
+
+	$image_props[ $image_id ] = wp_get_attachment_image_src( $image_id, $size );
+	$thumb_w = max ( $thumb_w, $image_props[ $image_id ][ 1 ] );
+	$thumb_h = max ( $thumb_h, $image_props[ $image_id ][ 2 ] );
}

$slider_show_id = 'simpleslider_show_' . get_the_ID();
$slider_show_number = get_the_ID();

............................. line 127

$first = true;
foreach ( $images as $image_id => $image_data ) {
$opacity = $first ? '1' : '0';
$first = false;

-	$image_prop = wp_get_attachment_image_src( $image_id, $size );
+	$image_prop = $image_props[ $image_id ];

$image_tag = "";
.............................
?>
```