Add width and height attributes to data stored by imagefield for an image to save getimagesize calls.

Nels - May 16, 2008 - 16:43
Project:ImageField
Version:6.x-3.0
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I'm trying to extract from Imagefield the width of an image uploaded.
The purpose of this is to theme it's surrounding div based on the image size.

I found this post: http://drupal.org/node/124537 and tried that, as well as a few other additions to the imagefield.module, but haven't been able to get that to work.
I also tried using imagesx() using the 'filepath' variable, but it pitched a fit and threw warnings all over.

Can anyone give me some directions how to make the following 'body variable' available?
$node->field_image_foo[0]['width']

Thanks!

#1

dopry - May 19, 2008 - 23:33
Title:Extracting width from attached file for theme.» Add width and height attributes to data stored by imagefield for an image to save getimagesize calls.

You could take 5 seconds and look at either theme_imagefield_image or theme_image and discover..
if (!$getsize || (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath']))) {

now leave people alone and rtfm ;)....

but this is a kind of cool idea.... so I'll re-title it to something that makes sense.

#2

Nels - May 20, 2008 - 15:47

Right, I RTFM. Oops no I didn't; there isn't one (if there is, a link somewhere or inclusion in the "readme" file would be quite helpful)

I have tried adding that, and playing with that, but all I can ever get that to do is declare the width and/or height twice for an image. I want to pull that information to theme the view (i.e. If picture width = X pixels, the div up a level should be X+20; if picture width X < Y, then the div would be floated). This is to work-around my web-editors not uploading the same size (pixel dimension) photo everytime.

I think this should be a core feature because you can output the less-than-useful-for-theming-purposes file size with: $node->field_image_foo[0]['filesize']

Please forgive me for not being a PHP master, but I just don't see how to expose this to ConTemplate. I suppose I can start reading the API docs, but really thought this would be an easy addition.

Currently, I can see the following from Imagefield when editing the ConTemplate:

$node->field_image_foo[0]['fid']
$node->field_image_foo[0]['title']
$node->field_image_foo[0]['alt']
$node->field_image_foo[0]['nid']
$node->field_image_foo[0]['filename']
$node->field_image_foo[0]['filepath']
$node->field_image_foo[0]['filemime']
$node->field_image_foo[0]['filesize']

#3

jpetso - May 21, 2008 - 08:18
Category:feature request» support request
Status:active» fixed

Ok, so far so good. Now if you assume that $node->field_image_foo[0] equals the $file variable that dopry mentioned above - both have a 'filepath' member, at least - then you can still get width, height, and all that stuff, by replacing $file with $node->field_image_foo[0]:

<?php
 
list($width, $height, $type, $image_attributes) = @getimagesize($node->field_image_foo[0]['filepath']);
?>

And shwoops, you have all attributes you would ever want, ready to be used for whatever purpose.

Hope that helps! Assuming that it does, I'll close this as support request.

#4

Nels - May 21, 2008 - 18:39
Category:support request» feature request
Priority:normal» minor
Status:fixed» closed

Thank you both for your help; I was able to figure my problem out. My biggest hurdle was realizing that I needed to put this code in the Body Template of ConTemplate, not in the imagefield.module.

What I was trying to do is make the width a variable (like $node->field_image_foo[0]['width'] ) that ConTemplate could use. While I still don't understand why it's not there already, I was at least able theme the content I needed to. It's still a feature request, but apparently not one that will be implemented.

For posterity: I added jpetso's line from #3 to the top of the Body Template, then used the $width variable as needed. In my case, the code is:

<div class="field-items field-full-story-picture" style="<?php if ($width >= 480) print "width:520px"; if ($width <= 479) print "width:"; print $width; print "px; float: right"; ?>;">

As I said, I'm a rather terrible hack at PHP. I'm sure my code could be cleaned up, but it works.

Thanks again!

#5

Garrett Albright - February 26, 2009 - 17:50

Using getimagesize() every time an image field is viewed is not such a great idea. PHP will have to open the image and determine the dimensions of the image each time, which will add up to a significant slow-down in page loading and unnecessary disk-thrashing. A better solution would be to use getimagesize() once, then cache the result for future reference.

I'm kind of surprised (and disappointed) that ImageField doesn't do this already, and would love to hack together a patch to implement it… but it's crunch time, so in the interest of time I'm going to end up implementing something faster/hackier. But please consider this another voice in support for this idea.

#6

ecksley - April 12, 2009 - 07:17

Consider this another voice in support of adding width, height (and might I suggest thumbnail path/width/height as well) to the image array.

In my case the images are getting used in Flash with node array data passed using AMF PHP and Services module. Thus the solutions noted above or here aren't of much use unless I somehow knew how to use them to change the output of the field array. Something clearly over my head as proven here.

That said, thank you to those who maintain the module for all their hard work. I'm not unappreciative. Just want to make another case for this change.

#7

Garrett Albright - May 13, 2009 - 22:05
Priority:minor» normal
Status:closed» active

I'm going to re-open this issue because I'm running into it again and still think it's a total bummer for PHP to expend cycles and RAM when it has to open an image and read its dimensions every time it's viewed (Imagecache notwithstanding). This is an intensive process which really should be cached, just like many other intensive process in Drupal such as the output of input filters.

I tried to work it out myself, but unfortunately I wasn't smart enough to figure out how CCK works by myself and don't have time at the moment to learn it properly. Someone smarter than me, please consider taking this issue up and working it out. Stressed-out web servers around the world will thank you.

#8

quicksketch - May 14, 2009 - 01:21
Version:5.x-2.1» 6.x-3.0

This won't be added to the Drupal 5 version, which is now maintenance only. In Drupal 6, the height/width tags are already stored if the filefield_meta.module is enabled, though it doesn't use this data even though it's available when displaying the image.

#9

dereine - May 14, 2009 - 20:05
Status:active» needs review

here is a small patch which does this.

AttachmentSize
imagefield-259351-filefield_meta-widht_height.patch 1002 bytes

#10

quicksketch - May 14, 2009 - 22:14
Status:needs review» needs work

It'd probably make more sense to check $file['data']['width'] && $file['data']['height'] rather than checking if the module exists. FileField Meta is a "lazy" module which only affects files on upload, so if a file is uploaded before FileField Meta is turned on, width and height will be empty despite the module being available.

#11

dereine - May 15, 2009 - 13:07
Status:needs work» needs review

thanks for the review! its definitve true, this was not the right way to do it.

here is the new patch for this.

At the same time i realized, that its not possible to set the width/height parameter with $attributes, what about adding the possibilty to be able to set it with the $attributes

AttachmentSize
imagefield-259351-filefield_meta-widht_height.patch 923 bytes

#12

quicksketch - May 16, 2009 - 22:59

You can currently specify a height and width manually as long as you set $get_size = FALSE. How does this patch look for maintaining this ability while adding the check from your patch?

AttachmentSize
imagefield_cached_width_height.patch 1.12 KB

#13

Garrett Albright - May 21, 2009 - 23:55

It's kinda funky because Filefield Meta requires Getid3, which feels like a "heavy" requirement when you just want to get dimensions of images and don't care about any other sort of file. But since the results are cached, it (along with this patch) should provide the features I'm looking for and with less stress on the server in the long run, so I'll quit whining and use it and see how it goes.

#14

quicksketch - May 22, 2009 - 00:23

I think it might make a good amount of sense to have ImageField auto-add height and width properties of it's own to the "data" column, making FileField Meta unnecessary for this feature. FileField Meta is intended to work with audio and video files more than images, populating image data with height and width is just a bonus (it's really intended to get height and width properties of videos).

We can improve the imagefield_file_insert() function (currently it's empty) to add this cached data rather easily, which is the same approach filefield_meta uses.

#15

quicksketch - July 3, 2009 - 23:11
Status:needs review» fixed

Well I went ahead and committed #12, we can add the ability to save image height/width on upload in a separate issue.

#16

System Message - July 17, 2009 - 23:20
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.