I need to calculate the dimensions of an image that is created through CCK and then displayed in a view. The objective is to allow a user to upload an image and then display the image width and height along with the caption and other image details. Like you might see on a stock photo site.

Any suggestions how to do this? Thanks!

Comments

horizens’s picture

I've tried looking at Computed Field, but I'm not great with PHP yet and I couldn't get this to work:

$image = open_image($node->field_image[0]['filepath']);
$node_field[0]['value'] = imagesx($image);

mpare’s picture

If you use imagefield then this information is already available to you. Take a look at this...

<?php
function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  
  $file = (array)$file;
  if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
    $attributes = drupal_attributes($attributes);
    
    $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];
    $alt = empty($alt) ? $file['alt'] : $alt;
    $title = empty($title) ? $file['title'] : $title;
    
    $url = file_create_url($path);
    return '<img src="'. check_url($url) .'" alt="'.
        check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
  }
}
?>

See up in like the second line of the function? You have all these values available to you and already in variables. Sweet!

BTW, this code is right out of the imagefield module

Well hope that helps, if not hollar back.

Peace,
-mpare
www.paretech.com

Did you figure out how to do something? Did you find documentation on Drupal.org inadequate? Well now it's your turn. Document your Success!

horizens’s picture

That is very helpful. Thanks for pointing me in the right direction. I'm still a little confused as to how to display the variable on the view page though. Would you do this with computed fields? Or is there an easier way?

Thanks!

horizens’s picture

Does anyone have an idea how to implement this? Thanks.

webastien’s picture

Hi horizens,

I hope you've already found the solution (8 March...). Otherwise, simply add :

  if ($width)  $attributes['width']  = $width;
  if ($height) $attributes['height'] = $height;

before "$attributes = drupal_attributes($attributes);"

Kem7s’s picture

Hi,

Need some help for a similar case... I need to display image's original dimension (like "1024x768") under thumbnail in a view. I use imagefield and I've tried using computed field without success to implement this.

Is computed field the good solution? what is the right computed code to add?

Webastien, what is the effect of your code in the imagefield module? How display the value in the view with your method?

thanks

Kem7s’s picture

Solved.
Finally the line given by jpetso (http://drupal.org/node/259351) works too when put at top of the computed code :
list($width, $height, $type, $image_attributes) = @getimagesize($node->field_mycontenttype[0]['filepath']);

Thanks